This topic describes how to write a simple batch application.
Before you begin
Before starting this task, you must set up your environment.
About this task
This sample task assumes that the user is using Rational Application
Developer (RAD). Other development tools can be used as well.
Procedure
- Create a new enterprise application project entitled PostingsSample2.
- Add a new Enterprise JavaBeans (EJB) project module. This
creates a new EAR file and EJB project.
- Add BATCH_RUNTIME to the Java Build Path for
the PostingsSample2EJB project.
- Assume that there is an existing Java class, AccountOp,
a Java object that has two public interface methods, creditAccount(..) and debitAccount(..).
These methods are called from the batch bean as appropriate. Import this
class from the workspace, or write a batch calling class.
- Create the BatchStep class. The batch bean is an entity
bean which inherits from an abstract Batch Step class. Do the following to
create the abstract job step PostingSample2Step:
- In the PostingsSample2EJB project, create an abstract Java class PostingSample2Step that
inherits from the BatchJobStepInterface.
- Add the BatchJobStepInterface.
- Make the class abstract.
- Create the Batch Bean (Entity EJB). These steps describe
how to create the batch bean manually. The Batch Bean reuses the BatchJobStepLocalInterface,
the BatchJobStepKey and the BatchJobStepLocalHome classes from the provided
programming model; these classes are not recreated. Only the Batch Bean implementation
is generated.
- In the PostingSample2EJB project, create a new CMP EJB, entitled PostingsSample2Data.
- For the local home interface class, select BatchJobStepLocalHomeInterface.
- For the local interface class, select BatchJobStepLocalInterface.
- Deselect the Use single key attribute type for the key class.
- For the key class, select BatchJobStepKey.
- Remove the CMP attribute ID.
- In the CMP attribute, add the two CMP attributes, jobID and stepID of
type string.
- Add jobID as the name, and java.lang.String as
the type. Select Key.
- Similarly, add stepID, type java.lang.String and
make it the Key field.
- Select the BeanSuperclass. Create a matching type of PostingStep2.
This creates the new EJB, but only the EJB Bean class displays
in the projects. The local interface, home interface and the key class are
picked up from the programming model provided classes.
- Editing the deployment descriptor. Make the following changes
in the ejb-jar.xml for the PostingSample2Data.
- In the Bean tab, edit the CMP connection factory JNDI name to jdbc/lree,
and the Container Authorization type to Per_Connection_Factory.
- In the Assembly tab, for the container transactions, make all methods
of the PostingsSample2Data as Required.
- Edit the JNDI name under WebSphere bindings to ejb/com/ibm/ws/batch/sample/PostingsSample2Data.
- Save and close the deployment descriptor. As before, add the
implementation for the EJB later.
- Create the Batch Job Session Bean The batch Job is a
session bean that is declared in the deployment descriptor, but has no classes
generated, because they are all part of the runtime.
- From the ejb-jar xml, in the Bean tab,
click Add to create a new Session EJB, name it PostingsJob2.
- Change Transaction type to Bean.
- In the Bean class, remote home interface and remote interface,
type the following strings:
- Bean class : com.ibm.ws.batch.BatchJobControllerBean
- Remote interface: com.ibm.ws.batch.BatchJobController
- Remote home interface: com.ibm.ws.batch.BatchJobControllerHome
Note that no new class is generated; This is because
all the classes exist in the runtime. We are only declaring the Job Session
bean in the deployment descriptor.
- Edit the WebSphere bindings JNDI name to: ejb/com/ibm/ws/batch/PostingsSample2Job
- Add a reference to the PostingsSample2Job of type EJB reference.
- Select the PostingsSample2Data Bean and add the name: ejb/PostingsSample2Data
Select the local reference type.
- Create the work Manager reference.
- In the references tab, on the PostingSample2Job bean, click Add.
Select Resource Reference, and click Next.
- Name: wm/BatchWorkManager
- Type: Select commonj.work.WorkManager
- Authentication: Container
- Sharing Scope: Shareable
- Edit the Java Naming and Directory Interface (JNDI) name of
the wm/BatchWorkManger to wm/default.
- Save and close the ejb-jar.xml.
- Creating the input batch data stream There can be zero,
1 or n input and/or output streams. In this example, there is one InputStream
reading from a file and no output stream. Output is written to the console.
The BDS classes are POJO. To create the input stream, complete the following
steps:
- Create a new class in the com.ibm.ws.batch.sample package, called PostingsSample2InputStream.
- Implement the BatchDataStream Interface. This
creates the new PostingSample2Input Stream class. You can add the implementations
later.
- Create Posting2 Class to represent a record from the Input Stream.
This class is the object representation of a record in the batch input
stream. This is constructed while reading a record from the Input BDS, and
passed on to the batch bean. This class is like a data or cargo bean that
contains attributes and corresponding getters and setters, of the fields in
the input stream.
- Create a new class named Posting2 that implements the Serializable
interface. Implementations can be added to this later.
- Add implementations to the classes
- Now add implementations to the PostingSample2DataBean, PostingSample2Step
and PostingSample2InputStream as in the attached class files. This example
is kept simple from a processing standpoint to highlight the steps involved
in developing a new batch application. After the implementation
classes are added, you can deploy the EJB.
What to do next
In the administrative console navigation tree,
expand Runtime Operations and select Job Management. Verify
that in the panel on the right, there is an empty job table with no error
messages.