Java EE 7 SDK

Samples Main Page

# Sample Batch application demonstrating JobOperator APIs

This sample demonstrates how to use the `javax.batch.operations.JobOperator` interface to submit batch jobs.

## Description

This sample application demonstrates how to use the Batch Processing API (JSR 352) to submit batch jobs and how to obtain information about submitted jobs using the `JobOperator` interface. This sample also demostrates how to perform batch processing using multiple partitioned chunks.

The job xml contains one step called 'process'. This step uses three batch artifacts:

- PayrollPartitionMapper (that implements PartitionMapper) is a class that tells the Batch runtime about the number of partitions that must be created and also the set of partition specific input data. This is communicated to the Batch Runtime by returing a PartitionPlanImpl from the mapPartitions() method.

  The PayrollPartitionMapper creates 5 partitions to process 30 payroll records. Each Partition is run on a separate thread by the Batch runtime, but each Partition runs the same Chunk (meaning same ItemReader, same ItemWriter and same ItemProcessor). In order to let each Partition to work on different payroll records, the PayrollPartitionMapper sets the starting and ending employee numbers that each Partitioned step must process.

- SimpleItemReader (that implements ItemReader): An ItemReader reads input records. The Batch runtime calls ItemReader.readItem() to read input records. The ItemReader can read input records from a variety of sources like Database, a CSV flat file, from another JavaEE component etc. Each record that is read from a ItemReader is handed to an ItemProcessor.

  In this sample, the SimpleItemReader reads PayrollInputRecord(s) from a Singleton bean.

- SimpleItemProcessor (that implements ItemProcessor): An ItemProcessor processes an input record and returns (possibly a different) output record. The Batch runtime calls ItemProcessor.processItem() to process the record that was read by ItemReader.

  In this sample, SimpleItemProcessor receives PayrollInputRecord (from the batch runtime), computes the net pay and returns a PayrollRecord (to the batch runtime). The batch runtime then calls ItemWriter

- SimpleItemWriter (that implements ItemWriter): An ItemWrites writes the output record to some datastore. The datastore can range from Database, a CSV flat file, to another JavaEE component. The Batch runtime calls ItemWriter.writeItems() to write the records that were processed by ItemWriter.

  In this sample, SimpleItemWriter stores the processed PayrollRecord into an EJB Singleton bean.

The sample uses the embedded JavaDB database service included with the Glassfish server.

The sample is built and deployed as a WAR file.

## Key Features

This sample application demonstrates the following key features:

- Defining a batch job using a job XML file.
- Using the method `JobOperator.start()` to submit a batch job. This method returns the execution id for the job execution
- Using the method `JobOperator.getJobInstances()` method to obtain all JobInstabnces for the "payroll" job
- Using the method `jobOperator.getJobInstances()` to get a list of job instances submitted by this application.
- Using the method `jobOperator.getJobExecutions(jobInstance)` to get a list of `JobExecution` objects.
- Working with `JobExecution` objects:

- Using the method `JobExecution.getJobName()` to get the job name.
- Using the method `JobExecution.getBatchStatus()` to get the batch status of the job execution.
- Using the method `JobExecution.getExitStatus()` to get the exit status of the job execution.
- Using the method `JobExecution.getStartTime()` to obtain the time when the job execution started.
- Using the method `JobExecution.getEndTime()` to obtain the time when the job execution completed.

## Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
2. `samples_install_dir` is the sample application base directory. Go to: `samples_install_dir/javaee7/batch/joboperator-api`.
3. Build the sample application by running `mvn package` command from a command line terminal.
4. Deploy the sample application by running `asadmin deploy --force target/partition.war` command from a command line terminal.
5. The front page of this sample is at http://localhost:8080/joboperator-api/PayrollJobSubmitterServlet. (The port number might vary.)
6. Use `mvn clean` command from a command line terminal to clean up the target directory.
7. Undeploy the sample application by running `asadmin undeploy partition` command from a command line terminal.

## Troubleshooting

If you have problems when running the application, refer to the troubleshooting document.

Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.