Java Reference
In-Depth Information
}
}
Our
ItemWriter
class starts by defining an
open
method, which gets a file for writing.
The name of the newly created logfile is taken from the job properties. Our source of in-
formation about the current batch job is the injected
JobContext
class (there is also a
StepContext
object that provides information about a specific step). It gives us the
possibility to get the properties defined for a job, its current ID, status, and additional tran-
sient data.
The heart of our writer is, of course, the
writeItem
method. It receives a list of items
(seats in our case) to be written and its responsibility is to persist them. This method can
be invoked multiple times up to the moment when there is no more data to be written. You
can configure the number of elements that will be processed in every chunk. What's more,
every chunk runs in its own transaction.
Finally, when the last chunk is written, the
close
method writes a summary and closes
the file.
All elements are now in place, so we need to create a batch job specification. The file
ex-
ternalSystem.xml
should be placed in the
src/main/resources/META-INF/
batch-jobs
directory in your project. The contents are as follows:
<job id="
externalSystem
" xmlns="http://xmlns.jcp.org/xml/ns/
javaee"
version="1.0">
[1]
<properties>
<property name="
logFile
" value="log.txt" />
[2]
</properties>
<step id="processData">
<chunk
item-count="2"
>
[3]
<reader ref="
externalSystemReader
" />
[4]
<processor ref="
seatProcessor
" />
<writer ref="
seatWriter
" />
</chunk>
</step>
</job>