This topic describes how to write a simple
batch application.
Before you begin
Before starting this task, you must set up your environment.
You might need to configure the com.ibm.websphere.ejbcontainer.expandCMPCFJNDIName
custom property before developing a Compute Grid batch application.
See
Java virtual machine custom properties
for configuration
information.
About this task
This sample task assumes that the user is using Rational
Application Developer. 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 Enterprise ARchive (EAR) file and Enterprise
EJB project.
- Add batchruntime.jar to the Java build
path for the PostingsSample2EJB project. The batchruntime.jar
is located in the <WAS_HOME>/lib folder.
- Assume that there is an existing Java class, AccountOp, which is 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 that is inherited from an abstract batch step class.
Complete the following substeps to create the abstract job step PostingSample2Step:
- In the PostingsSample2EJB project, create the abstract
Java class, PostingSample2Step , that is inherited
from the BatchJobStepInterface.
- Add the BatchJobStepInterface.
- Make the class abstract.
- Create the batch bean. The following substeps
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 insert this type name to Key field.
- Select the BeanSuperclass. Create a matching
type of PostingStep2. This action
creates the new EJB, but only the EJB bean class is displayed in the
projects. The local interface, home interface and the key class are
picked up from the programming model-provided classes.
- Edit the deployment descriptor. Make the following changes
in the ejb-jar.xml file 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 from 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 the classes are all part of the
runtime.
- From the ejb-jar.xml file, in the Bean tab, click Add to create the new session EJB, 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 because
all the classes exist in the runtime environment. You are only declaring
the Job Session bean in the deployment descriptor.
- Edit the WebSphere bindings Java Naming and Directory
Interface (JNDI) name to ejb/com/ibm/ws/batch/PostingsSample2Job.
- Add an EJB reference to the PostingsSample2Job.
- 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, click Next,
and provide the following information:
- Name: wm/BatchWorkManager
- Type: Select commonj.work.WorkManager
- Authentication: Container
- Sharing scope: Shareable
- Edit the JNDI name of the wm/BatchWorkManger to wm/default.
- Save and close the ejb-jar.xml file.
- Create the input batch data stream. There can
be zero, 1 or n input and or output streams. In this example,
there is one input stream reading from a file and no output stream.
Output is written to the console. The batch data stream (BDS) classes
are Plain Old Java Object (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 action creates the new PostingSample2Input stream class. You
can add the implementations later.
- Create Posting2Class 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 PostingSample2Data bean,
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 you add the implementation classes, you can deploy the
EJB.