Digging into Spring Batch, I'd like to know as to How can we share data between the different steps of a Job?
Can we use JobRepository for this? If yes, how can we do that?
Is there any other way of doing/achieving the same?
Passing data between steps. In Spring Batch, ExecutionContext of execution context that can be used in the scope of each step and job is provided. By using the execution context, data can be shared between the components in the step.
In your Reader and Writer you need to implement ItemStream interface and use ExecutionContext as member variable. Here i have given example with Processor instead of Writer but same is applicable for Writer as well . Its working fine for me and i am able to take values from reader to processor.
Multiple jobs can be run simultaneously. There are two main types of Spring Batch Parallel Processing: Single Process, Multi-threaded, or Multi-process.
An ExecutionContext is a set of key-value pairs containing information that is scoped to either StepExecution or JobExecution . Spring Batch persists the ExecutionContext , which helps in cases where you want to restart a batch run (e.g., when a fatal error has occurred, etc.).
the job repository is used indirectly for passing data between steps (Jean-Philippe is right that the best way to do that is to put data into the StepExecutionContext
and then use the verbosely named ExecutionContextPromotionListener
to promote the step execution context keys to the JobExecutionContext
.
It's helpful to note that there is a listener for promoting JobParameter
keys to a StepExecutionContext
as well (the even more verbosely named JobParameterExecutionContextCopyListener
); you will find that you use these a lot if your job steps aren't completely independent of one another.
Otherwise you're left passing data between steps using even more elaborate schemes, like JMS queues or (heaven forbid) hard-coded file locations.
As to the size of data that is passed in the context, I would also suggest that you keep it small (but I haven't any specifics on the
From a step, you can put data into the StepExecutionContext
. Then, with a listener, you can promote data from StepExecutionContext
to JobExecutionContext
.
This JobExecutionContext
is available in all the following steps.
Becareful : data must be short. These contexts are saved in the JobRepository
by serialization and the length is limited (2500 chars if I remember well).
So these contexts are good to share strings or simple values, but not for sharing collections or huge amounts of data.
Sharing huge amounts of data is not the philosophy of Spring Batch. Spring Batch is a set of distinct actions, not a huge Business processing unit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With