简体   繁体   中英

Get Properties from reader in Java Batch

I have a Java Batch (JSR-352) application running on WildFly. The application is exposing a rest-api to trigger the job execution. I want to supply some values coming from the HTTP REST request to the Reader Class. What is the best way to implement this?

REST API where job is getting started:

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handleFileReady(MyNotification notification) {
   final Properties jobParams = new Properties();
   jobParams.setProperty("filename", notification.getFileName());

   BatchRuntime.getJobOperator().start("filetransfer", jobParams);
   return Response.status(Response.Status.NO_CONTENT).build();
}

Reader where I would like to read the values from:

public class MyJobReader extends AbstractItemReader {

    @Override
    public Integer readItem() throws Exception {
       // Get Values here
       ...

Also, at the moment I am setting the String values in the properties by reading the notification object, is there a better way to supply the entire object?

By injecting JobContext I can get the execution id now:

public class MyJobReader extends AbstractItemReader {

    @Inject
    private JobContext jobContext;

    @Override
    public Integer readItem() throws Exception {

        Properties pros = BatchRuntime.getJobOperator().getParameters(jobContext.getExecutionId());

You can pass them as query strings in your http request, and your REST resource class then pass them as batch job parameters. In your job xml files, you pass job parameters to your reader as reader's batch properties. Your reader class then inject these batch properties as class fields.

This is how it is done in jberet-rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM