<div dir="ltr" style="text-align: justify;">
<h2><b>ItemReader-</b></h2>
<p>Although a simple concept, an <i><b>ItemReader </b></i>is the means for providing data from many different types of input. The most general examples include:</p>
<div class="itemizedlist">
<ul >
<li>Flat File- Flat File Item Readers read lines of data from a flat file that typically describe records with fields of data defined by fixed positions in the file or delimited by some special character (e.g. Comma).</li>
<li>XML &#8211; XML <i><b>ItemReaders </b></i>process XML independently of technologies used for parsing, mapping and validating objects. Input data allows for the validation of an XML file against an XSD schema.</li>
<li>Database &#8211; A database resource is accessed to return resultsets which can be mapped to objects for processing. The default SQL <i><b>ItemReaders </b></i>invoke a <b><i>RowMapper </i></b>to return objects, keep track of the current row if restart is required, store basic statistics, and provide some transaction enhancements that will be explained later.</li>
</ul>
</div>
<p>There are many more possibilities, but we&#8217;ll focus on the basic ones for this chapter. A complete list of all available <i><b>ItemReaders </b></i>can be found in Appendix A.<br />
<i><b>ItemReader </b></i>is a basic interface for generic input operations:</p>
<pre class="highlight">public interface ItemReader<;T>; { 
 
 T read() throws Exception, UnexpectedInputException, ParseException; 
 
} 
</pre>
<div id="ads-id" align="center"></div>
<p>The read method defines the most essential contract of the <i><b>ItemReader</b></i>; calling it returns one Item or null if no more items are left. An item might represent a line in a file, a row in a database, or an element in an XML file. It is generally expected that these will be mapped to a usable domain object (i.e. Trade, Foo, etc) but there is no requirement in the contract to do so.</p>
<p>It is expected that implementations of the <i><b>ItemReader </b></i>interface will be forward only. However, if the underlying resource is transactional (such as a JMS queue) then calling read may return the same logical item on subsequent calls in a rollback scenario. It is also worth noting that a lack of items to process by an <i><b>ItemReader </b></i>will not cause an exception to be thrown. For example, a database <i><b>ItemReader </b></i>that is configured with a query that returns 0 results will simply return null on the first invocation of read.</p>
<h2><b>ItemWriter-</b></h2>
<p><i><b>ItemWriter </b></i>is similar in functionality to an <i><b>ItemReader</b></i>, but with inverse operations. Resources still need to be located, opened and closed but they differ in that an <i><b>ItemWriter </b></i>writes out, rather than reading in. In the case of databases or queues these may be inserts, updates, or sends. The format of the serialization of the output is specific to each batch job.</p>
<p>As with <i><b>ItemReader</b></i>, <b><i>ItemWriter </i></b>is a fairly generic interface:</p>
<pre class="highlight">public interface ItemWriter<;T>; { 
 
 void write(List<;? extends T>; items) throws Exception; 
 
} 
</pre>
<p>As with read on <i><b>ItemReader</b></i>, write provides the basic contract of <i><b>ItemWriter</b></i>; it will attempt to write out the list of items passed in as long as it is open. Because it is generally expected that items will be &#8216;batched&#8217; together into a chunk and then output, the interface accepts a list of items, rather than an item by itself. After writing out the list, any flushing that may be necessary can be performed before returning from the write method. For example, if writing to a Hibernate DAO, multiple calls to write can be made, one for each item. The writer can then call close on the hibernate Session before returning.</p>
<h2><b>ItemProcessor-</b></h2>
<p>The <i><b>ItemReader </b></i>and <i><b>ItemWriter </b></i>interfaces are both very useful for their specific tasks, but what if you want to insert business logic before writing? One option for both reading and writing is to use the composite pattern: create an <i><b>ItemWriter </b></i>that contains another <b><i>ItemWriter</i></b>, or an <i><b>ItemReader </b></i>that contains another <i><b>ItemReader</b></i>. For example:</p>
<pre class="highlight">public class CompositeItemWriter<;T>; implements ItemWriter<;T>; { 
 
 ItemWriter<;T>; itemWriter; 
 
 public CompositeItemWriter(ItemWriter<;T>; itemWriter) { 
 this.itemWriter = itemWriter; 
 } 
 
 public void write(List<;? extends T>; items) throws Exception { 
 //Add business logic here 
 itemWriter.write(item); 
 } 
 
 public void setDelegate(ItemWriter<;T>; itemWriter){ 
 this.itemWriter = itemWriter; 
 } 
} 
</pre>
<p>The class above contains another <b><i>ItemWriter </i></b>to which it <i><b>delegates</b></i> after having provided some business logic. This pattern could easily be used for an <i><b>ItemReader </b></i>as well, perhaps to obtain more reference data based upon the input that was provided by the main <i><b>ItemReader</b></i>. It is also useful if you need to control the call to write yourself. However, if you only want to &#8216;transform&#8217; the item passed in for writing before it is actually written, there isn&#8217;t much need to call write yourself: you just want to modify the item. For this scenario, Spring Batch provides the <b><i>ItemProcessor </i></b>interface:</p>
<pre class="highlight">public interface ItemProcessor<;I, O>; { 
 
 O process(I item) throws Exception; 
} 
</pre>
<p>An <b><i>ItemProcessor </i></b>is very simple; given one object, transform it and return another. The provided object may or may not be of the same type. The point is that business logic may be applied within process, and is completely up to the developer to create. An <i><b>ItemProcessor </b></i>can be wired directly into a step, For example, assuming an <i><b>ItemReader </b></i>provides a class of type Foo, and it needs to be converted to type Bar before being written out. An <i><b>ItemProcessor </b></i>can be written that performs the conversion:</p>
<pre class="highlight">public class Foo {} 
 
public class Bar { 
 public Bar(Foo foo) {} 
} 
 
public class FooProcessor implements ItemProcessor<;Foo,Bar>;{ 
 public Bar process(Foo foo) throws Exception { 
 //Perform simple transformation, convert a Foo to a Bar 
 return new Bar(foo); 
 } 
} 
 
public class BarWriter implements ItemWriter<;Bar>;{ 
 public void write(List<;? extends Bar>; bars) throws Exception { 
 //write bars 
 } 
} 
</pre>
<p><i><b>I</b></i>n the very simple example above, there is a class Foo, a class Bar, and a class <i><b>FooProcessor </b></i>that adheres to the <i><b>ItemProcessor </b></i>interface. The transformation is simple, but any type of transformation could be done here. The <i><b>BarWriter </b></i>will be used to write out Bar objects, throwing an exception if any other type is provided. Similarly, the <i><b>FooProcessor </b></i>will throw an exception if anything but a Foo is provided. The <i><b>FooProcessor </b></i>can then be injected into a Step:</p>
<pre class="highlight"><;job id="ioSampleJob">; 
 <;step name="step1">; 
 <;tasklet>; 
 &;lt;chunk reader="fooReader" processor="fooProcessor" writer="barWriter" 
 commit-interval="2"/&;gt; 
 <;/tasklet>; 
 <;/step>; 
<;/job>; 
</pre>
<p><b><;<;<a href="https://dineshonjava.com/configuring-step-in-spring-batch-2/">Configuring Step in Spring Batch</a><;<; <a href="https://dineshonjava.com/spring-tutorial/">Index </a>>;>; <a href="https://dineshonjava.com/scaling-and-parallel-processing-in/">Scaling and Parallel Processing</a>>;>;</b></p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/the-domain-language-of-batch/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/configuring-and-running-job-in-spring/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '609'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…