Exchanging data between steps in spring batch

stepexecutioncontext spring batch example
spring batch:step parameters
spring batch conditional step
spring batch multiple steps
spring batch tutorial
spring batch exception handling
spring batch documentation
spring batch flow example

I have a job that is built of the following components

  1. Processing Step - activates an external processing that logs it's result in the DB and returns an internal id so I can take this id and process it further
  2. Logging Step - built of a tasklet, that contains a chunk

The chunk is built of an item reader - that I plan will use #{stepExecutionContext['job.id']} as part of the sql written in the xml file so it will get the relevant logging info

I'm trying to work with the solution suggested here - 11.8 Passing Data to Future Steps but i get this error when i try to add a property on the step execution context or the job execution context

chunkContext.getStepContext().getStepExecutionContext().put("job.id", jobId);

And I get this error:

java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableMap.put(Collections.java:1342)
at  ...
at  org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)

Did i miss something?

StepContext available from ChunkContext is a read-only object; use a StepExecutionListener and save the step execution context passed as parameter in method StepExecutionListener.afterStep(StepExecution stepExecution)

Passing info between steps in Spring?, Spring Batch supports pushing data to future job steps, and this can be done through the ExecutionContext , more precisely the  A Step is a domain object that encapsulates an independent, sequential phase of a batch job and contains all of the information necessary to define and control the actual batch processing. Steps can be processed in either of the following two ways.

chunkContext.getStepContext().getStepExecutionContext() retrun copy use chunkContext.getStepContext().getStepExecution().getExecutionContext()

Data sharing - Spring Batch Essentials, We can use this context to share data between steps. The key-value-based data is maintained by org.springframework.batch.item.ExecutionContext in its usage. Passing values between Spring batch steps When we work with Spring batch we job, we may need to pass the values between steps. I had a scenario where I has to post some data to an API and then based on the response, in the next step I need to check whether response has some id then I can skip some steps.

I always follow this path:

    .put("", "");

I also had problems doing the put in StepExecution. Maybe it loses data when it goes to the next step. However, I leave the last sentence to the most expert.

Passing values between Spring batch steps, To share the data between steps, you need to put into the job context which is like a global variable space and all the steps have the access to it. 1.2. Partitioning in Spring batch. Partitioning uses multiple threads to process a range of data sets. The range of data sets can be defined programmatically. It’s on use case, how many threads we want to create to be used in a partition(s). The number of threads is purely based on the need/requirement. Spring Batch is single threaded by default.

Chapter 10. Controlling execution, Controlling a job's execution path; Sharing data between job steps at runtime; Externalizing job definitions for reuse; Choosing how to stop a job after executing​  That is because Line will act as a DTO to transfer data between steps. According to Spring Batch, objects that are transferred between steps must be serializable . On the other hand, we can start thinking about reading and writing lines.

Flow control, Passing data between steps. In Spring Batch, ExecutionContext of execution context that can be used in the scope of  Spring Batch - Table Of Contents. Spring Batch Hello World example-Write data from csv to xml file Spring Boot Batch Simple example Spring Batch - Difference between Step, Chunk and Tasklet Spring Batch Tasklet - Hello World example Spring Boot + Batch + Task Scheduler Example

Common Batch Patterns - Project Metadata API Guide, Most simple batch jobs can use off-the-shelf input from a Spring Batch A chunk-​oriented Step (created from the step factory beans) lets users To make the data available to future Steps , it must be "promoted" to the Job  We will understand Spring Batch - Difference between Step, Chunk and Tasklet. Spring Batch Tasklet - Hello World example In this post we execute a simple tasklet for deleting a file.