Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Job parameteres in to item processor using spring Batch annotation

I am using spring MVC. From my controller, I am calling jobLauncher and in jobLauncher I am passing job parameters like below and I'm using annotations to enable configuration as below:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
        // read, write ,process and invoke job
} 

JobParameters jobParameters = new JobParametersBuilder().addString("fileName", "xxxx.txt").toJobParameters();
stasrtjob = jobLauncher.run(job, jobParameters);                              

and here is my itemprocessor                                                         
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {

  public OutPutData process(final InputData inputData) throws Exception {

        // i want to get job Parameters here ????

  }

}
like image 920
Mare Avatar asked Jul 31 '15 02:07

Mare


People also ask

How do you access job parameters in Spring Batch?

JobParameters can be used for identification or even as reference data during the job run. They have reserved names, so to access them we can use Spring Expression Language. For example to access a property 'abc' on job parameters: we can access it using the syntax #{jobParameters[abc]} .

What is JobExplorer in Spring Batch?

public interface JobExplorer. Entry point for browsing executions of running or historical jobs and steps.

What is StepExecution in Spring Batch?

public class StepExecution extends Entity. Batch domain object representation the execution of a step. Unlike JobExecution , there are additional properties related the processing of items such as commit count, etc.


Video Answer


2 Answers

1) Put a scope annotation on your data processor i.e.

@Scope(value = "step") 

2) Make a class instance in your data processor and inject the job parameter value by using value annotation :

@Value("#{jobParameters['fileName']}")
private String fileName;

Your final Data processor class will look like:

@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {

@Value("#{jobParameters['fileName']}")
private String fileName;

  public OutPutData process(final InputData inputData) throws Exception {

        // i want to get job Parameters here ????
      System.out.println("Job parameter:"+fileName);

  }

  public void setFileName(String fileName) {
        this.fileName = fileName;
    }


}

In case your data processor is not initialized as a bean, put a @Component annotation on it:

@Component("dataItemProcessor")
@Scope(value = "step")
public class DataItemProcessor implements ItemProcessor<InputData, OutPutData> {
like image 158
Amit Bhati Avatar answered Nov 10 '22 02:11

Amit Bhati


A better solution (in my opinion) that avoids using Spring's hacky expression language (SpEL) is to autowire the StepExecution context into your processor using @BeforeStep.

In your processor, add something like:

@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
    JobParameters jobParameters = stepExecution.getJobParameters();
    // Do stuff with job parameters, e.g. set class-scoped variables, etc.
}

The @BeforeStep annotation

Marks a method to be called before a Step is executed, which comes after a StepExecution is created and persisted, but before the first item is read.

like image 33
heez Avatar answered Nov 10 '22 02:11

heez