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

3

9

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 ????

  }

}
Finzer answered 31/7, 2015 at 2:34 Comment(0)
C
25

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> {
Cid answered 31/7, 2015 at 3:50 Comment(3)
if your processor is configured in an XML, you should add the scope there, like: <bean id="myProcessor" class="my.package.MyProcessor" scope="step"/>Lustig
is there any way to set custom type list to job parameters and get that list in item processor ?Finzer
Shouldn't it be @StepScope rather than @Scope(value = "step")?Headless
P
18

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.

Peterus answered 13/12, 2018 at 17:46 Comment(1)
Upvoted - this is a really clean approach in batch 1.5.10Divorcement
S
0

I have written the in the process itself, rather then creating separate file using the lambda expression.

@Bean
    @StepScope
    public ItemProcessor<SampleTable, SampleTable> processor(@Value("#{jobParameters['eventName']}") String eventName) {
        //return new RandomNumberProcessor();
        
        return item -> {
            SampleTable dataSample = new SampleTable();
            if(data.contains(item)) {
                return null;
            }
            else {
                dataSample.setMobileNo(item.getMobileNo());
                dataSample.setEventId(eventName);
                return dataSample;
            }
        };
    }
Sharolynsharon answered 10/10, 2022 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.