{"id":5554,"date":"2025-09-11T08:08:48","date_gmt":"2025-09-11T08:08:48","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5554"},"modified":"2025-09-11T08:50:48","modified_gmt":"2025-09-11T08:50:48","slug":"using-simple-itemreader-itemprocessor-and-itemwriter","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/using-simple-itemreader-itemprocessor-and-itemwriter\/","title":{"rendered":"Using Simple ItemReader, ItemProcessor and ItemWriter"},"content":{"rendered":"<p><strong>How it Works<\/strong><\/p>\n<ol>\n<li>We can define the Size of Jobreader and Job Processor. In other words the no of records to be processed could be set using Jobreader and job processor<\/li>\n<li>We dont have control over Jobwriter. Jobreader and JobProcessor honors chunk size<\/li>\n<\/ol>\n<p><strong>SimpleJobReader.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.batch.item.ItemReader;\r\nimport org.springframework.batch.item.NonTransientResourceException;\r\nimport org.springframework.batch.item.ParseException;\r\nimport org.springframework.batch.item.UnexpectedInputException;\r\nimport org.springframework.stereotype.Component;\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n\r\n@Component\r\npublic class SimpleJobReader implements ItemReader&lt;Integer&gt; {\r\n    List&lt;Integer&gt; arrNum = Arrays.asList(1,2,3,4,5,6,7,8,9);\r\n    int chunkSize = 3;\r\n    int index = 0;\r\n\r\n    @Override\r\n    public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {\r\n        System.out.println(&quot;Inside Job Reader&quot;);\r\n        if(index &lt; arrNum.size()){\r\n            int num = arrNum.get(index);\r\n            index++;\r\n            return num;\r\n        }\r\n        index = 0;\r\n\r\n        return null;\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>SimpleJobProcessor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.batch.item.ItemProcessor;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class SimpleJobProcessor implements ItemProcessor&lt;Integer, Long&gt; {\r\n    @Override\r\n    public Long process(Integer item) throws Exception {\r\n        System.out.println(&quot;Inside Job Processor&quot;);\r\n        return Long.valueOf(item);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SimpleJobWriter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.batch.item.Chunk;\r\nimport org.springframework.batch.item.ItemWriter;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class SimpleJobWriter implements ItemWriter&lt;Long&gt; {\r\n    @Override\r\n    public void write(Chunk&lt;? extends Long&gt; items) throws Exception {\r\n        System.out.println(&quot;Inside Job Writer&quot;);\r\n        items.getItems().forEach(System.out::println);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SimpleJobWriter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\npublic class BatchConfig {\r\n    @Autowired\r\n    ItemReader simpleJobReader;\r\n\r\n    @Autowired\r\n    ItemWriter simpleJobWriter;\r\n\r\n    @Autowired\r\n    ItemProcessor simpleJobProcessor;\r\n\r\n    @Bean\r\n    public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {\r\n        return new JobBuilder(&quot;job&quot;, jobRepository)\r\n                .incrementer(new RunIdIncrementer())\r\n                .start(simpleTaskletStep(jobRepository, transactionManager))\r\n                .next(simpleChunkStep(jobRepository, transactionManager))\r\n                .build();\r\n    }\r\n\r\n    @Bean\r\n    public Step simpleChunkStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {\r\n        StepBuilder stepBuilderOne = new StepBuilder(&quot;Chunk Oriented Step&quot;, jobRepository);\r\n        return stepBuilderOne\r\n                .chunk(3, transactionManager)\r\n                .reader(simpleJobReader)\r\n                .processor(simpleJobProcessor)\r\n                .writer(simpleJobWriter)\r\n                .build();\r\n    }\r\n\r\n    @Bean\r\n    public Step simpleTaskletStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {\r\n        StepBuilder stepBuilderOne = new StepBuilder(&quot;Tasklet Oriented Step&quot;, jobRepository);\r\n        return stepBuilderOne\r\n                .tasklet(simpleTaskLet(), transactionManager)\r\n                .build();\r\n    }\r\n\r\n    @Bean\r\n    public Tasklet simpleTaskLet(){\r\n        return (StepContribution contribution, ChunkContext chunkContext) -&gt; {\r\n            return RepeatStatus.FINISHED;\r\n        };\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Writer\r\n1\r\n2\r\n3\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Writer\r\n4\r\n5\r\n6\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Reader\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Processor\r\nInside Job Writer\r\n7\r\n8\r\n9\r\nInside Job Reader\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How it Works We can define the Size of Jobreader and Job Processor. In other words the no of records to be processed could be set using Jobreader and job processor We dont have control over Jobwriter. Jobreader and JobProcessor honors chunk size SimpleJobReader.java import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.NonTransientResourceException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.stereotype.Component; import&hellip; <a href=\"https:\/\/codethataint.com\/blog\/using-simple-itemreader-itemprocessor-and-itemwriter\/\">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-5554","post","type-post","status-publish","format-standard","hentry","category-spring-batch"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5554","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=5554"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5554\/revisions"}],"predecessor-version":[{"id":5557,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5554\/revisions\/5557"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}