Passing Data to Future Steps in Spring Batch
- On September 20, 2020
- By Pradeep Mishra
- In Spring Batch
- No comments
Recent Posts
Categories
- Design (2)
- Elasticsearch (3)
- Golang (5)
- hadoop (2)
- Java (12)
- Kubernetes (1)
- linux (1)
- Maven (1)
- Openshift (1)
- Performance (1)
- Scala (2)
- Security (3)
- Spark (2)
- Spring (3)
- Spring Batch (1)
- Spring Boot (2)
- sqoop (1)
- UI (1)
- unix (1)
- Vim (2)
Tags
annotation
apache hive 3
build
cdp
cloudera
commands
CORS
design
design pattern
DNS
elastic
elasticsearch
go
golang
hadoop
hdfs
hive
http
ip
java
jms
junit
maven
mq
mysql
nginx
oracle
proxy
proxy_pass
queue
rdbms
Repository
resolution
resolver
reverseproxy
scala
spark
spring
spring boot
springboot
sqoop
string
time zone
upstream
vim
Recent Posts
Categories
- Design (2)
- Elasticsearch (3)
- Golang (5)
- hadoop (2)
- Java (12)
- Kubernetes (1)
- linux (1)
- Maven (1)
- Openshift (1)
- Performance (1)
- Scala (2)
- Security (3)
- Spark (2)
- Spring (3)
- Spring Batch (1)
- Spring Boot (2)
- sqoop (1)
- UI (1)
- unix (1)
- Vim (2)
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
To find out more, including how to control cookies, see here: Cookie Policy
Tags
annotation
apache hive 3
build
cdp
cloudera
commands
CORS
design
design pattern
DNS
elastic
elasticsearch
go
golang
hadoop
hdfs
hive
http
ip
java
jms
junit
maven
mq
mysql
nginx
oracle
proxy
proxy_pass
queue
rdbms
Repository
resolution
resolver
reverseproxy
scala
spark
spring
spring boot
springboot
sqoop
string
time zone
upstream
vim
Find Us
Address
123 Main Street
New York, NY 10001
Hours
Monday–Friday: 9:00AM–5:00PM
Saturday & Sunday: 11:00AM–3:00PM
%d
Pradeep Mishra
Share post:
It is often useful to pass information from one step to another. This can be done through the
ExecutionContext. The catch is that there are twoExecutionContexts: one at theSteplevel and one at theJoblevel. TheStepExecutionContextremains only as long as the step, while theJobExecutionContextremains through the wholeJob. On the other hand, theStepExecutionContextis updated every time theStepcommits a chunk, while theJobExecutionContextis updated only at the end of eachStep.The consequence of this separation is that all data must be placed in the
StepExecutionContextwhile theStepis executing. Doing so ensures that the data is stored properly while theStepruns. If data is stored to theJobExecutionContext, then it is not persisted duringStepexecution. If theStepfails, that data is lost.public class SavingItemWriter implements ItemWriter<Object> { private StepExecution stepExecution; public void write(List<? extends Object> items) throws Exception { // ... ExecutionContext stepContext = this.stepExecution.getExecutionContext(); stepContext.put("someKey", someObject); } @BeforeStep public void saveStepExecution(StepExecution stepExecution) { this.stepExecution = stepExecution; } }To make the data available to future
Steps, it must be “promoted” to theJobExecutionContextafter the step has finished. Spring Batch provides theExecutionContextPromotionListenerfor this purpose. The listener must be configured with the keys related to the data in theExecutionContextthat must be promoted. It can also, optionally, be configured with a list of exit code patterns for which the promotion should occur (COMPLETEDis the default). As with all listeners, it must be registered on theStepas shown in the following example:Java Configuration
@Bean public Job job1() { return this.jobBuilderFactory.get("job1") .start(step1()) .next(step1()) .build(); } @Bean public Step step1() { return this.stepBuilderFactory.get("step1") .<String, String>chunk(10) .reader(reader()) .writer(savingWriter()) .listener(promotionListener()) .build(); } @Bean public ExecutionContextPromotionListener promotionListener() { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); listener.setKeys(new String[] {"someKey" }); return listener; }Finally, the saved values must be retrieved from the
JobExecutionContext, as shown in the following example:public class RetrievingItemWriter implements ItemWriter<Object> { private Object someObject; public void write(List<? extends Object> items) throws Exception { // ... } @BeforeStep public void retrieveInterstepData(StepExecution stepExecution) { JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); this.someObject = jobContext.get("someKey"); } }In similar way, @AfterStep annotated method can be used to read/write the ExecutionContext and can access in future Step.
@AfterStep public ExitStatus afterStep(StepExecution execution) { this.stepExecution = stepExecution; return execution.getExitStatus(); }I hope you have enjoyed this post and it helped you to Passing Data to Future Steps in Spring Batch. Please like and share and feel free to comment if you have any suggestions or feedback.
Share this:
Like this:
Migrating the project from Java 8 to 17 and Spring Boot 2.x to 3.x
In this blog, I will highlight the changes and challenges that I have faced while doing the migration of services from java 8 to 17 and Spring Boot 2.x to. read more…
Share this:
Like this:
Continue Reading
Build a module with its dependencies in a multi-module Maven project
INTRODUCTION The mechanism in Maven 4 that handles multi-module projects is referred to as the reactor. This part of the Maven core does the following: Module collection starts from one aggregator. read more…
Share this:
Like this:
Continue Reading
Schedule Jobs/Tasks Using Cron Expression in Spring with Example
When we have a requirement to run a task/job repeatedly after a particular time interval, we achieve this functionality by implementing Scheduling. To schedule jobs in the spring boot application to run. read more…
Share this:
Like this:
Continue Reading
Accessing Data with JPA
JPA is a specification which specifies how to access, manage and persist information/data between java objects and relational databases. It provides a standard approach for ORM, Object Relational Mapping. Spring. read more…
Share this:
Like this:
Continue Reading