{"id":5549,"date":"2025-09-05T06:11:07","date_gmt":"2025-09-05T06:11:07","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5549"},"modified":"2025-09-05T06:47:39","modified_gmt":"2025-09-05T06:47:39","slug":"execution-context-in-spring-batch","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/execution-context-in-spring-batch\/","title":{"rendered":"Execution Context in Spring Batch"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">Job Execution Context and Step Execution Context<\/strong><br \/>\n<strong>Job Execution Context<\/strong> &#8211; Available throughout the entire job execution across various steps. Stores data that needs to be shared across multiple steps or retrieved after a job restart. it survives restarts and failures.<\/p>\n<p>I.E.<br \/>\nIf you download a file in Step 1 and need its path in Step 3, store the path in the Job Execution Context.<\/p>\n<p><strong>Step Execution Context<\/strong> &#8211; Limited to the specific step execution.Stores data relevant only to that step, such as reader\/writer state or counters.<br \/>\nI.E.<br \/>\nA reader might store the last read line number here so it can resume from that point if the step fails. We can pass values between steps and jobs using ExecutionContextPromotionListener.<\/p>\n<p><strong>BatchConfig.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\n@EnableBatchProcessing\r\npublic class BatchConfig {\r\n\r\n    @Autowired\r\n    private JobBuilderFactory jobBuilderFactory;\r\n\r\n    @Autowired\r\n    private StepBuilderFactory stepBuilderFactory;\r\n\r\n    @Bean\r\n    public Job sampleJob() {\r\n        return jobBuilderFactory.get(&quot;sampleJob&quot;)\r\n            .start(step1())\r\n            .next(step2())\r\n            .listener(promotionListener()) \/\/ Promote data from step to job context\r\n            .build();\r\n    }\r\n\r\n    @Bean\r\n    public Step step1() {\r\n        return stepBuilderFactory.get(&quot;step1&quot;)\r\n            .tasklet((contribution, chunkContext) -&gt; {\r\n                StepExecution stepExecution = contribution.getStepExecution();\r\n                stepExecution.getExecutionContext().putString(&quot;stepData&quot;, &quot;Hello from Step 1&quot;);\r\n                return RepeatStatus.FINISHED;\r\n            })\r\n            .build();\r\n    }\r\n\r\n    @Bean\r\n    public Step step2() {\r\n        return stepBuilderFactory.get(&quot;step2&quot;)\r\n            .tasklet((contribution, chunkContext) -&gt; {\r\n                JobExecution jobExecution = contribution.getStepExecution().getJobExecution();\r\n                String data = jobExecution.getExecutionContext().getString(&quot;stepData&quot;);\r\n                System.out.println(&quot;Retrieved from Job Execution Context: &quot; + data);\r\n                return RepeatStatus.FINISHED;\r\n            })\r\n            .build();\r\n    }\r\n\r\n    @Bean\r\n    public ExecutionContextPromotionListener promotionListener() {\r\n        ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();\r\n        listener.setKeys(new String&#x5B;] { &quot;stepData&quot; }); \/\/ Promote this key from step to job\r\n        return listener;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nRetrieved from Job Execution Context: Hello from Step 1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Job Execution Context and Step Execution Context Job Execution Context &#8211; Available throughout the entire job execution across various steps. Stores data that needs to be shared across multiple steps or retrieved after a job restart. it survives restarts and failures. I.E. If you download a file in Step 1 and need its path in&hellip; <a href=\"https:\/\/codethataint.com\/blog\/execution-context-in-spring-batch\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[371],"tags":[],"class_list":["post-5549","post","type-post","status-publish","format-standard","hentry","category-spring-batch"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5549"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5549\/revisions"}],"predecessor-version":[{"id":5550,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5549\/revisions\/5550"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}