Monday, 28 May 2012

Invoking Soa Suite 11g Service from java

Invoking Soa Suite 11g Service from java

In Soa Suite 11g we can not call the composite service directly from java. We need to copy the service in the composite, change its binding to adf and wire this service to the component. All the credits goes to Jay's Blog and Clemens, Great work.

The first step is to open the composite xml and find your service.
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Generated by Oracle SOA Modeler version 1.0 at [8/25/09 3:01 PM]. -->
<composite name="Helloworld"
  <import namespace=""
          location="BPELProcess1.wsdl" importType="wsdl"/>
   <service name="bpelprocess1_client_ep" ui:wsdlLocation="BPELProcess1.wsdl">
     <interface.wsdl interface=""/>
      < port="">
      <wsp:PolicyReference URI="oracle/log_policy" orawsp:category="management"

Copy this service and give it a unique name and now we need to add the binding.adf binding to this service instead of the
<service name="bpelprocess1_client_ep" ui:wsdlLocation="BPELProcess1.wsdl">
     <interface.wsdl interface=""/>
      < port="">
      <wsp:PolicyReference URI="oracle/log_policy" orawsp:category="management"
<service name="ADFBindingService" ui:wsdlLocation="BPELProcess1.wsdl">
     <interface.wsdl interface=""/>
    <binding.adf serviceName="{}ADFBindingService"

Go back to the design mode and open the new adf binding service and select the same wsdl as your other service ( this will correct the serviceName ) and at last we need to wire the new service to the component

Now we only need to call this service from java
package nl.whitehorses.soa.client;
import java.util.Hashtable;
import java.util.UUID;
import java.util.List;
import javax.naming.Context;
import oracle.fabric.common.NormalizedMessage;
import oracle.fabric.common.NormalizedMessageImpl;
import java.util.Map;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.Element;
public class StartProcess {
    public StartProcess() {
        Hashtable jndiProps = new Hashtable();
        jndiProps.put(Context.PROVIDER_URL, "t3://soaps3.alfa.local:8001/soa-infra");
        jndiProps.put(Context.SECURITY_PRINCIPAL, "weblogic");
        jndiProps.put(Context.SECURITY_CREDENTIALS, "weblogic1");
        jndiProps.put("dedicated.connection", "true");
        String inputPayload =
        "<process xmlns=\"\">" +
        " <input>hello</input>" +
        "</process>" ;
        Locator locator = null;
        try {
            // connect to the soa server
            locator = LocatorFactory.createLocator(jndiProps);
            String compositeDN = "default/Helloworld!1.0";
            // find composite
            Composite composite = locator.lookupComposite("default/Helloworld!1.0");
            System.out.println("Got Composite : "+ composite.toString());
            // find exposed service of the composite
            Service service = composite.getService("ADFBindingService");
            System.out.println("Got serviceName : "+ service.toString());
            // make the input request and add this to a operation of the service
            NormalizedMessage input = new NormalizedMessageImpl();
            String uuid = "uuid:" + UUID.randomUUID();
            // payload is the partname of the process operation
            // process is the operation of the employee service
            NormalizedMessage res = null;
            try {
               res = service.request("process", input);
            } catch(Exception e) {
            Map payload = res.getPayload();
            Element element = (Element)payload.get("payload");
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(element);
            transformer.transform(source, result);
            CompositeInstanceFilter filter = new CompositeInstanceFilter();
            filter.setMinCreationDate(new java.util.Date((System.currentTimeMillis() - 2000000)));
            // get composite instances by filter ..
            List<CompositeInstance> obInstances = composite.getInstances(filter);
            // for each of the returned composite instances..
            for (CompositeInstance instance : obInstances) {
                System.out.println(" DN: " + instance.getCompositeDN() +
                                   " Instance: " + instance.getId() +
                                   " creation-date: " + instance.getCreationDate() +
                                   " state (" + instance.getState() + "): " + getStateAsString(instance.getState())
                // setup a component filter
                ComponentInstanceFilter cInstanceFilter = new ComponentInstanceFilter();
                // get child component instances ..
                List<ComponentInstance> childComponentInstances = instance.getChildComponentInstances(cInstanceFilter);
                // for each child component instance (e.g. a bpel process)
                for (ComponentInstance cInstance : childComponentInstances) {
                    System.out.println(" -> componentinstance: " + cInstance.getComponentName() +
                                       " type: " + cInstance.getServiceEngine().getEngineType() +
                                       " state: " +getStateAsString(cInstance.getState())
                    System.out.println("State: "+cInstance.getNormalizedStateAsString() );
        } catch (Exception e) {
    private String getStateAsString(int state)
        // note that this is dependent on wheter the composite state is captured or not
        if (state == CompositeInstance.STATE_COMPLETED_SUCCESSFULLY)
            return ("success");
        else if (state == CompositeInstance.STATE_FAULTED)
            return ("faulted");
        else if (state == CompositeInstance.STATE_RECOVERY_REQUIRED)
            return ("recovery required");
        else if (state == CompositeInstance.STATE_RUNNING)
            return ("running");
        else if (state == CompositeInstance.STATE_STALE)
            return ("stale");
            return ("unknown");
    public static void main(String[] args) {
        StartProcess startUnitProcess = new StartProcess();
} /////////////////////////////////////  jars to be downloaded and imported MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.mgmt_11.1.1\soa-infra-mgmt.jar MiddlewareJdev11gR1PS1\oracle_common\modules\oracle.fabriccommon_11.1.1\fabric-common.jar MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1\fabric-client.jar MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1\fabric-ext.jar MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1\fabric-runtime.jar MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1\oracle-soa-client-api.jar MiddlewareJdev11gR1PS1\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1\oracle.soa.fabric.jar ////////////////////////////////////

No comments:

Post a Comment