Showing posts with label Java API. Show all posts
Showing posts with label Java API. Show all posts

Monday, May 17, 2010

Invoke BPEL using Java API

This article helps you in kick start of creating your own BPEL console or automate program to do certain activities which BPEL console can perform.

All the operations performed in BPEL console can be created using the java API.

BPEL processes can be invoked using a Java API. The API will differ depending upon the type of operation involved. One way operation has an input message without result. Two way operations send and receive the message.

Refer Java API : http://download.oracle.com/docs/cd/B31017_01/integrate.1013/b28986/toc.htm

Note: orabpel.jar files should be added in the classpath


package com.bpel.client;

import com.oracle.bpel.client.*;
import com.oracle.bpel.client.dispatch.*;

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


public class InvokeBPEL
{

/**
*
* @author Ziaur Rahuman S
*/
public static void main(String[] args)
{

Properties props = new Properties();

props.setProperty("orabpel.platform", "ias_10g");
props.setProperty("java.naming.factory.initial",
"com.evermind.server.rmi.RMIInitialContextFactory");
props.setProperty("java.naming.provider.url",
"opmn:ormi://hostname:6003:oc4j_soa/orabpel");
props.setProperty("java.naming.security.principal", "oc4jadmin");
props.setProperty("java.naming.security.credentials", "oc4jadm1n");
props.setProperty("dedicated.connection", "true");


String xml = "<SyncHelloWorldProcessRequest><input>World</input></SyncHelloWorldProcessRequest>";

try {
// Create instance of Locator object with domain name and password as arguement
Locator locator = new Locator("ocoetest", "oc4jadm1n", props);
IDeliveryService dService = (IDeliveryService) locator.lookupService(IDeliveryService.SERVICE_NAME);

// Create normalized message with payload
NormalizedMessage nm = new NormalizedMessage();
nm.addPart("payload", xml);

// Invoke two way operation
NormalizedMessage response = dService.request("SyncHelloWorld","process",nm);
Map payload = response.getPayload();

// Invoke one way operation
//dService.post("AsyncHelloWorld","initiate",nm);

System.out.println("SyncHelloWorld Invoked Successfully !!!");

} catch (Exception e)
{
e.printStackTrace();
}


}
}


Friday, April 23, 2010

Java program to get the input xml/receive data from bpel instance

This java program helps in recover the input message for the given set of instances. The BPEL process name and instance id should be given as input in the instances.txt file as shown below.

SyncHelloWorld=370001
SyncHelloWorld=370002

BPELInstance.java

package client;

import com.oracle.bpel.client.IBPELDomainHandle;
import com.oracle.bpel.client.IInstanceHandle;
import com.oracle.bpel.client.Locator;
import com.oracle.bpel.client.ServerException;
import com.oracle.bpel.client.util.WhereCondition;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import java.util.Properties;
import java.util.Scanner;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import oracle.xml.parser.v2.XMLDocument;

import org.w3c.dom.NodeList;


public class BPELInstance {
public static void main(String[] args) throws ServerException,
IOException {
try {

Properties props = new Properties();
WhereCondition whCnd;
String processName;
Locator locator;
int value;

props.setProperty("orabpel.platform", "ias_10g");
props.setProperty("java.naming.factory.initial",
"com.evermind.server.rmi.RMIInitialContextFactory");
props.setProperty("java.naming.provider.url",
"opmn:ormi://hostname:6003:oc4j_soa/orabpel");
props.setProperty("java.naming.security.principal", "oc4jadmin");
props.setProperty("java.naming.security.credentials", "password");
props.setProperty("dedicated.connection", "true");

locator = new Locator("default", "password", props); // Provide the domain name and password
System.out.println("Connected to the Server");
File fFile =
new File("C:\\Zia\\Automation\\Client\\instances.txt");
Scanner scanner = new Scanner(fFile);
IBPELDomainHandle domain = locator.lookupDomain();
while (scanner.hasNextLine()) {
Scanner scanner1 = new Scanner(scanner.nextLine());
scanner1.useDelimiter("=");
if (scanner1.hasNext()) {
processName = scanner1.next();
value = Integer.parseInt(scanner1.next());
//System.out.println("ProcessName = " + processName + " Instance = " + value);
WhereCondition whereProcessId =
new WhereCondition("process_id = ?");
whereProcessId.setString(1, processName);
whCnd = new WhereCondition("cikey = ?");
whCnd.setInt(1, value);
whereProcessId.append("and").append(whCnd);

IInstanceHandle instances[] =
locator.listInstances(whereProcessId);
if (instances != null && instances.length > 0) {
System.out.println("*** Recovering " + processName +
"[" + value + "] ***");
String auditTrail = instances[0].getAuditTrail();
//System.out.println("\n\n"+auditTrail);
XMLDocument xmlDoc =
Utility.getXMLDocumentFromString(auditTrail);
if (xmlDoc != null) {
NodeList nodeList =
xmlDoc.selectNodes("//details[@id]");

if (nodeList != null) {
System.out.println("@@@@@@@@@" +
nodeList.getLength() +
"@@@@@@@@@");
if (nodeList.getLength() > 0) {
System.out.println("### Getting the data from AuditDetails ###");
String auditDetails =
instances[0].getAuditDetails(0);
System.out.println(auditDetails);
} else {
System.out.println("### Getting the data from AuditTrail ###");
XMLDocument xmlDoc1 =
Utility.getXMLDocumentFromString(Utility.getNodeValueByXPath(xmlDoc,
"/audit-trail/event/details/text()"));
NodeList nl =
xmlDoc1.getDocumentElement().getElementsByTagName("part");
StringWriter sw = new StringWriter();
Transformer serializer =
TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(nl.item(0).getFirstChild()),
new StreamResult(sw));
String result = sw.toString();
System.out.println(result);
}
}
//System.out.println("******************* End Recovery Steps ***********************");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output

Connected to the Server
*** Recovering SyncHelloWorld[370001] ***
@@@@@@@@@0@@@@@@@@@
### Getting the data from AuditTrail ###
<?xml version="1.0" encoding="UTF-8"?>
<ns1:SyncHelloWorldProcessRequest xmlns:ns1="http://xmlns.oracle.com/SyncHelloWorld">
<ns1:input>JavaTest</ns1:input>
</ns1:SyncHelloWorldProcessRequest>
*** Recovering SyncHelloWorld[370002] ***
@@@@@@@@@0@@@@@@@@@
### Getting the data from AuditTrail ###
<?xml version="1.0" encoding="UTF-8"?>
<ns1:SyncHelloWorldProcessRequest xmlns:ns1="http://xmlns.oracle.com/SyncHelloWorld">
<ns1:input>OracleSoaTest</ns1:input>
</ns1:SyncHelloWorldProcessRequest>