Java EE7: Developing a Batch Processing Application
Overview
Purpose
This tutorial demonstrates how to develop a sample batch
application and run the batch application in Oracle GlassFish
Server.
Time to Complete
Approximately 1 hour
Introduction
JSR 352 (Batch Applications for the Java Platform) defines the
programming model for batch applications and a run time to run and
manage batch jobs. The programming model for batch applications
caters to batch processing concerns, such as jobs, steps,
repositories, reader processor writer patterns, chunks,
checkpoints, parallel processing, flow, split, transactions,
retries, sequencing, and partitioning.
As displayed in the diagram:
A job represents a series of closely related steps that
perform a discrete business process.
Steps can be executed in sequence or in parallel.
Steps can also be optional, with the decision to execute or
skip them conditioned on the outcome of prior steps in the same
workflow.
Steps can be check-pointed and retried, if needed, and are
generally transactional.
A repository stores information about the current jobs.
Jobs can be listed, started, stopped, paused, and cancelled
through the operator.
The operator is typically invoked in a scheduled or ad hoc
fashion.
The entire batch process is put together through a Job
Specification Language written in XML. Despite the robust
concepts, the programming model is kept simple.
Scenario
A very simple scenario is considered for this tutorial. A batch
job reads new-hire data for HR processing from a comma-separated
values (CSV) file. Each line in the file contains a name and the
hiring date for one employee. The batch job stores each line in
the file to a NewHire object and then writes out the
new hire's data into a database table.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
Java Platform, Standard Edition 7 (Java SE 7; Java SE 7u11
recommended)
NetBeans 7.3.1 IDE for Java Platform, Enterprise Edition 7
(Java EE 7)
Oracle GlassFish Server
Prerequisites
Before starting this tutorial, you should:
Have some experience writing and deploying web applications.
Have some experience with Contexts and Dependency Injection
beans and the Java Persistence API.
Have installed NetBeans 7.3.1, Java EE 7, and GlassFish 4.
In the New Entity Classes from Database dialog box, select jdbc/sample in theData Source: combo box,
select NEW_HIRE from Available Tables, click
Add, and then click Next.
On the Projects tab, browse to BatchExampleApp
> Source Packages > com.example.entity
and verify that the NewHire.java
entity class was created.
Creating the JSL File
A Job Specification Language (JSL) file specifies the order in
which steps must be executed to accomplish the job. In
this section, you create a JSL file.
On the Projects tab, expand BatchExampleApp
> Web Pages > resources > META-INF.
A JSL file must be placed in the META-INF/batch-jobs
directory.
In the META-INF folder, create a folder and name it batch-jobs.
In this batch job there is one step. It is a chunk-style step
and has an ItemReader, an ItemProcessor,
and an ItemWriter. Perform the following steps
to specify the step and the chunk:
Create an XML file in the batch-jobs folder
and name it newHireJob.
Remove the
<root></root> tag from the file.
Add the following content to the newHireJob.xml
file:
Developing the ItemReader, ItemProcessor,
and ItemWriter Classes
The JSL file specifies that the newHireJob has a
step that uses an ItemReader named NewHireItemReader,
an ItemProcessor named NewHireItemProcessor,
and an ItemWriter named NewHireItemWriter.
In this section, you create these classes.
Writing the NewHireItemReader and NewHireItemProcessor
Classes
On the Projects tab, expand BatchExampleApp
> Source Packages > com.example.batch
and then perform the following steps:
Create a Java class and name it NewHireItemReader.
This ItemReader implementation extends AbstractItemReader.