Wednesday, June 06, 2007

Retrieve BPEL process instances with Java

As mentioned in a previous article calling a process from java can be done easy. From another perspective, you need in the application the state of a particular process or process-instance. In the next example the java code is searching for a particular process that is running and retrieves his status.

The status of running process is the name of the current scope the process is in. To test the Java program creat a BPEL process with some scopes. Each scope is waiting of a few minutes. Running the Java program multiple times, results that you see that the status of the process is chaning.
Note: The article is based on this article, using the properties file and libraries.

The program:

RMIListInstances.java

package nl.orasoa.bpel;

import com.collaxa.xml.XMLHelper;

import com.oracle.bpel.client.BPELProcessId;
import com.oracle.bpel.client.IBPELProcessHandle;
import com.oracle.bpel.client.IInstanceHandle;
import com.oracle.bpel.client.Locator;
import com.oracle.bpel.client.NormalizedMessage;
import com.oracle.bpel.client.delivery.IDeliveryService;

import com.oracle.bpel.client.util.WhereCondition;

import com.oracle.bpel.client.util.WhereConditionHelper;

import java.util.Map;
import java.util.Properties;

import org.w3c.dom.Element;

public class RMIListInstances
{
public static void main(String[] args)
throws Exception
{
IInstanceHandle[] instHandles;
IBPELProcessHandle[] procHandles;

IInstanceHandle instance = null;
BPELProcessId bpid = null;

WhereCondition cond;
WhereCondition cond1;
WhereCondition cond2;

try
{
// properties in the classpath
Properties props = new java.util.Properties();

// read the properties file
java.net.URL url =
ClassLoader.getSystemResource("context.properties");
props.load(url.openStream());
System.out.println(props);

// WhereCondition cond =
// WhereConditionHelper.whereInstancesClosed();
// WhereCondition cond =
// WhereConditionHelper.whereInstancesCompleted();
// Search only open instances
cond = WhereConditionHelper.whereInstancesOpen();

// append where clause to search for
// a specific process and version
cond1 = new WhereCondition( "process_id = ?" );
cond1.setString(1, "Wacht");
cond2 = new WhereCondition( "revision_tag = ?" );
cond2.setString(1, "1.0");

// append the whole where clause
cond.append("and").append(cond1).append("and").append(cond2);

// get a connection to the server
Locator locator = new Locator("default", "welcome1", props);

instHandles = locator.listInstances(cond);
for (int j = 0; j < instHandles.length; j++)
{
// get the instance
instance = instHandles[j];
// get the process for the instance
bpid = instance.getProcess().getProcessId();
System.out.print("Got process " + bpid.getProcessId()
+ "("+ bpid.getRevisionTag() + ") ");
System.out.println(instance.getConversationId() + " "
+ instance.getStatus() + " " + instance.getTitle());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


Calling BPEL/ESB Webservice from standalone java client

In this article a sample is give how we call BPEL/ESB process from a java standalone program. The example is based on the HelloWorld process that accepts a single string as input.

The reason why I wrote this example, is that I did not find a straight forward simple java program to do this. Examples are supplied with BPEL (no 102) but not exactly what I want.

To run the example, open in JDeveloper a new project and add the following java class into your project. Make sure you add the following libraries to your project to make to java program runnable.
Here is the property file, to make the connection to your SOA Server.

context.properties
orabpel.platform=ias_10g
java.naming.factory.initial=com.evermind.server.rmi.RMIInitialContextFactory
java.naming.provider.url=opmn:ormi://edison.eurotransplant.nl:6003:oc4j_soa/orabpel
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=scanner1


Here is the class file.

RMIClient.java

package nl.orasoa.bpel;

import com.collaxa.xml.XMLHelper;
import com.oracle.bpel.client.Locator;
import com.oracle.bpel.client.NormalizedMessage;
import com.oracle.bpel.client.delivery.IDeliveryService;

import java.util.Map;
import java.util.Properties;

import org.w3c.dom.Element;

public class RMIClient
{
public static

void main(String[] args)
throws Exception
{
try
{
String who = "Marc";
System.out.println("Who is " + who);

// properties in the classpath
Properties props = new java.util.Properties();

java.net.URL url =
ClassLoader.getSystemResource("context.properties");
props.load(url.openStream());
System.out.println(props);

String xml =
"<HelloWorldProcessRequest
xmlns=\"http://bpel.eurotransplant.nl/HelloWorld\">";
xml = xml + "<input>" + who + "</input>";
xml = xml + "</HelloWorldProcessRequest>";

Locator locator = new Locator("default", "bpel", props);

IDeliveryService deliveryService =
(IDeliveryService)
locator.lookupService(IDeliveryService.SERVICE_NAME);

NormalizedMessage nm = new NormalizedMessage();
nm.addPart("payload", xml);

NormalizedMessage res =
deliveryService.request("HelloWorld", "process", nm);

Map payload = res.getPayload();

System.out.println("BPELProcess HelloWorld executed!<br>");
Element partEl = (Element) payload.get("payload");
System.out.println("The Result was " + XMLHelper.toXML(partEl));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}