Java Reference
In-Depth Information
<batch-artifacts xmlns="
http://jcp.org.batch/jsl
">
<ref id="acmeReader" class="org.javaee7.chapter11.AcmeReader"/>
<ref id="acmeProcessor" class="org.javaee7.chapter11.AcmeProcessor"/>
<ref id="acmeWriter" class="org.javaee7.chapter11.AcmeWriter"/>
</batch-artifacts>
The implementation classes should either extend abstract classes (reader and writer), or implement an interface
(processor). The
ItemReader
implementation class (
AcmeReader
in the example) extends the
AbstractItemReader
, and accepts an object into which the read items will be stored. In the example, that object class is named
WidgetReportItem
. As such, the class should implement the
readItem
method, which is responsible for performing
the reading. The method should return the object to which the items are read, or return a
null
when there are no
more items to read.
public class AcmeReader extends AbstractItemReader<WidgetReportItem> {
...
@Override
public WidgetReportItem readItem() throws Exception {
Path file = Paths.get("widgetFile.txt");
List<String> fileLines;
Charset charset = Charset.forName("US-ASCII");
fileLines = Files.readAllLines(file, charset);
for(String line:fileLines){
return new WidgetReportItem(line);
}
return null;
}
...
The
ItemProcessor
class implementation (
AcmeProcessor
in the example) is responsible for performing
processing for the chunk, and it should implement the
ItemProcessor
interface, accepting both the object containing
the read items, and an object to which the processed items will be stored. The
ItemProcessor
implementation class
should implement a
processItem
method, which is responsible for performing the processing.
The
ItemWriter
class implementation (
AcmeWriter
in the example) is responsible for performing the writing
for the chunk. The class implementation should extend the
AbstractItemWriter
class, and accept the object to which
the processed items will be written. This implementation must contain the
writeItems
method, which is responsible
for performing the writing.
Example of Item-Oriented Processing
In this example, a batch process is created to read text from a file, process that text accordingly, and then write
out the processed text. To begin, construct an XML file to define the job. The XML file for this example will be
called
acmeFileProcessor.xml
. Let's take a look at what a job process looks like. The lines below are from the
acmeFileProcessor.xml
file.
<?xml version="1.0" encoding="UTF-8"?>
<job id="acmeFileProcessor" xmlns="
http://batch.jsr352/jsl
"
>
<step id="readingStep" >
<chunk item-count="2">
<reader ref="acmeReader"></reader>