Get Properties from reader in Java Batch

683 Views Asked by At

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?

2

There are 2 best solutions below

1
adesai On BEST ANSWER

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());

0
cheng On

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.