Code review

This page shows the features of the Business Process Choreographer (BPC) API, that are used to create, start, delete and query business processes. The code sections provided on this page have been extracted from the EJBEAR enterprise application. For a more comprehensive discussion of the BPC API refer to the InfoCenter. The Javadoc of the BPC is provided with WebSphere Process Server.

Note: In the following code sections, exception handling is left out. A detailed description of the exceptions thrown by the particular commands can be found in the Javadoc.

Java libraries

The required Java classes and interfaces can be found in the following Java package:

com.ibm.bpe.api.*

BusinessFlowManager EJB

The key access point to the BPC API is the BusinessFlowManager EJB. So, add to the deployment descriptor of your client application a reference to the BusinessFlowManager EJB.

The reference to the local home interface for process applications is shown in the following code snippet:

<ejb-local-ref>
<ejb-ref-name>ejb/LocalBusinessFlowManagerHome</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.ibm.bpe.api.LocalBusinessFlowManagerHome</local-home>
<local>com.ibm.bpe.api.LocalBusinessFlowManager</local>
</ejb-local-ref>

Then, add the binding to the EJB reference. You can do this in the lower right field of the "References" tab of the Deployment Descriptor Editor:

Before you can use the public API, you must lookup and create the EJB using Java Naming and Directory Interface (JNDI) lookup mechanism. The following code snippet shows the JNDI lookup for the local EJB:

// Obtain the default initial JNDI context
Context initialContext = new InitialContext();

// Lookup the local home interface of the LocalBusinessFlowManager bean
Object result = initialContext.lookup("java:comp/env/ejb/LocalBusinessFlowManagerHome");

// Convert the lookup to the proper type
LocalBusinessFlowManagerHome processHome = (LocalBusinessFlowManagerHome)javax.rmi.PortableRemoteObject.narrow(result,LocalBusinessFlowManagerHome.class);

The home interface of the local session bean contains a create method for EJB objects. The method returns the local interface of the session bean:

BusinessFlowManager flowManager = processHome.create();

Note: If you want to connect to a remote business process container, create an InitialContext object using the following code:

Properties prop = System.getProperties();
prop.put(javax.naming.Context.PROVIDER_URL, "iiop://<your_host_name>");
context = new InitialContext(prop);

Note: The default JNDI name for the Process Choreographer API session bean is com/ibm/bpe/api/BusinessProcessHome. If your Business Process Container has been configured with another JNDI name, you must use that custom name.

Query process templates

If you want to work with processes, the first step is to query the appropriate process template. The following code snippet shows the query for the process template of this sample:

//Query the process template
String whereClause = "PROCESS_TEMPLATE.NAME = 'HelloSnippetProcess'";
String orderByClause = "PROCESS_TEMPLATE.VALID_FROM DESC";
ProcessTemplateData[] templates = flowManager.queryProcessTemplates(whereClause, orderByClause, null, null);

For a more detailed description of the query capabilities, refer to the Javadoc and the InfoCenter.

Data preparation

The API offers the method createMessage(...) that helps you to create process input messages in a convenient way. This method creates a client object wrapper that contains a data object with the proper type. This method does not implicitly create nested data objects. Nested data object have to be created explicitly using the createDataObject(...) method of the parent data object.

The following two diagrams illustrate the difference between creating the input data using the createByElement(...) method of the BOFactory and the createMessage(...) method of API.

To create the input data using the createByElement(...) method, you have to create the outer data object first and then add the inner data object:

For more detailed information, inspect the source code of HelloSnippetProcess.wsdl and DataIn.xsd.

This diagram illustrates the usage of the createMessage(...) method. First you create a client object wrapper, then you access the outer data object which was created implicitly. Finally, you create the inner data object using the createDataObject(...) method:

The following code snippet shows the creation of the input message:

ClientObjectWrapper cow = flowManager.createMessage(processTemplate.getID(),"Input");
DataObject input = (DataObject)cow.getObject();
DataObject dataIn = input.createDataObject("input");
dataIn.setString("name",in);

Note: You can obtain the type of the input message from the process template by calling processTemplate.getInputMessageTypeName(). This type is passed as the second argument to the createMessage(...) method.

Process invocation

In the case of a non-interuptable process you can use the call() method to invoke the process. The arguments of the call() method are the process template name and a client object wrapper in the shape of the input message. The method returns again a client object wrapper, which contains the output message of the process this time.

The following code snippet shows how to get the output client object wrapper and how to read out the data:

ClientObjectWrapper cowProcessOut = flowManager.call("HelloSnippetProcess",cow);
DataObject inner = (DataObject)cowProcessOut.getObject();
String resp = inner.getDataObject("output").getString("hello");
out.println(resp);

Click on the links to see the complete source code of the invoke.jsp and the index.html.