How can we share data between the different steps of a Job in Spring Batch?

How can we share data between the different steps of a Job in Spring Batch?

In Spring Batch, you can share data between different steps of a job using the JobExecutionContext. The JobExecutionContext provides a mechanism to share data within the scope of a job.

Here's an example of how you can set and retrieve data in the JobExecutionContext within different steps of a job:

import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionListener; import org.springframework.batch.core.Step; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.annotation.BeforeStep; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepScope; import org.springframework.batch.core.scope.context.JobContext; import org.springframework.batch.core.scope.context.StepContext; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableBatchProcessing public class BatchConfiguration { @Bean public Job myJob(JobBuilderFactory jobBuilderFactory, Step myStep) { return jobBuilderFactory.get("myJob") .listener(new MyJobExecutionListener()) .start(myStep) .build(); } @Bean public Step myStep(StepBuilderFactory stepBuilderFactory, ItemReader<String> reader, ItemProcessor<String, String> processor, ItemWriter<String> writer) { return stepBuilderFactory.get("myStep") .<String, String>chunk(10) .reader(reader) .processor(processor) .writer(writer) .build(); } @Bean @StepScope public ItemReader<String> myItemReader(@Value("#{jobParameters['inputFile']}") String inputFile) { // Your custom item reader logic here return null; } @Bean @StepScope public ItemProcessor<String, String> myItemProcessor() { // Your custom item processor logic here return item -> { // You can access job or step context here if needed JobContext jobContext = JobContext.getJobContext(); StepContext stepContext = StepContext.getStepContext(); // Example: Setting data in job context jobContext.getJobExecutionContext().put("myKey", "myValue"); // Your custom item processing logic here return item.toUpperCase(); }; } @Bean @StepScope public ItemWriter<String> myItemWriter() { // Your custom item writer logic here return items -> { // You can access job or step context here if needed JobContext jobContext = JobContext.getJobContext(); StepContext stepContext = StepContext.getStepContext(); // Example: Retrieving data from job context String myValue = (String) jobContext.getJobExecutionContext().get("myKey"); System.out.println("Value from job context: " + myValue); // Your custom item writing logic here items.forEach(System.out::println); }; } private static class MyJobExecutionListener implements JobExecutionListener { @Override public void beforeJob(JobExecution jobExecution) { // You can perform actions before the job starts } @Override public void afterJob(JobExecution jobExecution) { // You can perform actions after the job completes } } } 

In this example:

  • The JobExecutionContext is accessed in the ItemProcessor and ItemWriter to set and retrieve data.
  • The JobExecutionListener is used to perform actions before and after the entire job.

Remember to customize the reader, processor, and writer beans according to your specific business logic. The @StepScope annotation ensures that the beans are created with step scope, allowing them to access the JobExecutionContext and StepExecutionContext.

Examples

  1. "Spring Batch ExecutionContext example"

    Code Implementation:

    @StepScope @Bean public ItemReader<String> reader(@Value("#{jobParameters['inputFile']}") String inputFile) { ExecutionContext executionContext = new ExecutionContext(); executionContext.putString("inputFile", inputFile); FlatFileItemReader<String> reader = new FlatFileItemReader<>(); // Set up reader configuration reader.open(executionContext); return reader; } 

    Description: This example demonstrates using ExecutionContext to share data between different steps in a Spring Batch job, specifically initializing a reader with a job parameter.

  2. "Spring Batch StepExecution getExecutionContext example"

    Code Implementation:

    @Autowired private StepExecution stepExecution; public void someMethod() { ExecutionContext stepExecutionContext = this.stepExecution.getExecutionContext(); String sharedData = stepExecutionContext.getString("sharedDataKey"); // Use sharedData in the current step } 

    Description: Shows how to access the StepExecution in a component and retrieve shared data from the ExecutionContext.

  3. "Spring Batch JobExecutionContext example"

    Code Implementation:

    @BeforeStep public void beforeStep(StepExecution stepExecution) { JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); jobContext.putString("sharedDataKey", "sharedData"); } 

    Description: Illustrates using JobExecutionContext in a beforeStep method to set shared data that can be accessed by different steps within the job.

  4. "Spring Batch ExecutionContextPromotionListener example"

    Code Implementation:

    public class MyExecutionContextPromotionListener implements ExecutionContextPromotionListener { @Override public void beforeStep(StepExecution stepExecution) { // Set shared data in the job context ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext(); jobContext.putString("sharedDataKey", "sharedData"); } @Override public ExitStatus afterStep(StepExecution stepExecution) { return null; } } 

    Description: Demonstrates using ExecutionContextPromotionListener to promote data from the step's ExecutionContext to the job's ExecutionContext.

  5. "Spring Batch late binding example"

    Code Implementation:

    @StepScope @Bean public ItemProcessor<String, String> processor(@Value("#{jobExecutionContext['sharedDataKey']}") String sharedData) { // Use sharedData in the processor } 

    Description: Shows how to perform late binding by accessing job-level shared data in a processor using jobExecutionContext.

  6. "Spring Batch ExecutionContext in Tasklet example"

    Code Implementation:

    public class MyTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { ExecutionContext stepContext = chunkContext.getStepContext().getStepExecution().getExecutionContext(); String sharedData = stepContext.getString("sharedDataKey"); // Use sharedData in the tasklet return RepeatStatus.FINISHED; } } 

    Description: Shows how to access and use ExecutionContext within a custom Tasklet in a Spring Batch job.

  7. "Spring Batch JobParameter in ExecutionContext example"

    Code Implementation:

    @BeforeStep public void beforeStep(StepExecution stepExecution) { ExecutionContext stepContext = stepExecution.getExecutionContext(); String jobParameter = (String) stepExecution.getJobParameters().getParameters().get("jobParameter").getValue(); stepContext.putString("sharedDataKey", jobParameter); } 

    Description: Demonstrates using a job parameter to set shared data in the step's ExecutionContext during the beforeStep phase.

  8. "Spring Batch ExecutionContext in ItemWriter example"

    Code Implementation:

    public class MyItemWriter implements ItemWriter<String> { @Override public void write(List<? extends String> items) throws Exception { ExecutionContext stepContext = ExecutionContextPromotionListener.currentStepExecution().getExecutionContext(); String sharedData = stepContext.getString("sharedDataKey"); // Use sharedData in the item writer } } 

    Description: Shows how to access and use ExecutionContext within an ItemWriter using ExecutionContextPromotionListener.

  9. "Spring Batch ExecutionContext get from StepContext example"

    Code Implementation:

    @StepScope @Bean public ItemReader<String> reader() { ExecutionContext stepContext = StepSynchronizationManager.getContext(); String sharedData = stepContext.getString("sharedDataKey"); // Use sharedData in the reader } 

    Description: Demonstrates using StepSynchronizationManager to obtain the ExecutionContext in a step-scoped bean like an ItemReader.

  10. "Spring Batch ExecutionContext in ItemReader and ItemWriter example"

    Code Implementation:

    public class MyItemReader implements ItemReader<String> { @Override public String read() throws Exception { ExecutionContext stepContext = StepSynchronizationManager.getContext(); String sharedData = stepContext.getString("sharedDataKey"); // Use sharedData in the item reader return "someData"; } } 

    Description: Shows how to access and use ExecutionContext within both ItemReader and ItemWriter implementations.


More Tags

http virtualization rlang registry apache-commons-fileupload botocore axios cloudfiles biopython linear-gradients

More Programming Questions

More Financial Calculators

More Livestock Calculators

More Everyday Utility Calculators

More Fitness-Health Calculators