Tuesday, January 7, 2014

Exposing a OSB Proxy Service as EJB through JEJB transport

Exposing a OSB Proxy Service as EJB through JEJB transport

The JEJB transport lets you pass Plain Old Java Objects (POJOs) through Oracle Service Bus.To a J2EE client, a JEJB proxy service looks like a stateless session bean. A JEJB proxy service, on receiving the method arguments, passes their XML representation in the pipeline $body variable. POJO arguments are represented as the XML fragment. This XML fragment contains the location of the actual POJO stored in the object repository within the pipeline.

For deployment, Oracle Service Bus automatically packages JEJB proxy services as enterprise archives (EARs) and deploys to weblogic server(OSB Server).


JEJB transport uses the Java Interface to expose as EJB.

Here i will explain exposing the proxy service as EJB and invoking the same through java client by passing/receiving  the java objects and invoking the business service by passing/receiving the XML(Java utility methods will be used to convert Java to XML and XML to Java).The BPEL service invoked will receive the empno and and return back the employee details.

Steps to expose the proxy service as EJB

Create a Java interface that will receive/return the Employee object .

package jejbsample;
public interface EmployeeDetails {
 
    Employee getEmployeeDetails(Employee emp);
}

package jejbsample;
import java.io.Serializable;
public class Employee implements Serializable{
    private String empName;
    private String empNo;
    private String empDept;
    private String empLoc;  
    
    public Employee() {
        super();
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpNo(String empNo) {
        this.empNo = empNo;
    }
    public String getEmpNo() {
        return empNo;
    }
    public void setEmpDept(String empDept) {
        this.empDept = empDept;
    }
    public String getEmpDept() {
        return empDept;
    }
    public void setEmpLoc(String empLoc) {
        this.empLoc = empLoc;
    }
    public String getEmpLoc() {
        return empLoc;
    }
}

Create a Utility class that will convert the Java to XML and XML to Java.

package javaxmlconverter;
import java.io.InputStream;
import javax.xml.parsers.*;
import jejbsample.Employee;
import org.apache.xmlbeans.*;
import org.w3c.dom.*;

public class JavaXMLConverter {
    public JavaXMLConverter() {
        super();
    }
    public static String getEmpRequestXML(Employee emp) {       
        return emp.getEmpNo();
    }
    public static Employee getEmpResponseJava(XmlObject xml) throws Exception{
            Employee emp = new Employee();        
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            InputStream inputStream = xml.newInputStream();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(inputStream);           
            String empName=null ;
            String empNo=null;
            String empDept=null;
            String empLoc=null;            
            
            NodeList nList = doc.getElementsByTagName("EmployeeResponse");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element)nNode;
                     empName =eElement.getElementsByTagName("empName").item(0).getTextContent();
                     empNo =eElement.getElementsByTagName("empNo").item(0).getTextContent();
                     empDept =eElement.getElementsByTagName("empDept").item(0).getTextContent();
                     empLoc =eElement.getElementsByTagName("empLoc").item(0).getTextContent();
                   
                }
            }
        emp.setEmpDept(empDept);
        emp.setEmpName(empName);
        emp.setEmpLoc(empLoc);
        emp.setEmpNo(empNo);
        
        return emp;
        }
    }

Create a jar file based on this classes.

Create a OSB project and load the Jar file created in the previous step to the project.



Create a BusinessService based on the service WSDL.


Create a proxy service and select the Service Type as Transport Typed Service, by default the protocol will be selected as jejb.

Select the EJB Spec Version as 3.0 and select the client jar and the Business Interface accordingly.



Open the Proxy Service Message Flow and edit the Route Node.
Create a Request Actions, Java Callout to the method getEmpRequestXML(input parameter $body/open:getEmployeeDetails/open:arg0/ctx:java-content) from the utility class, this will return the empno from input java object.

Assign the request payload
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ns1="http://xmlns.oracle.com/JEJBSample/EmployeeDetailService/EmployeeDetailService">
<ns1:EmployeeRequest> <ns1:empNo>{$empno}</ns1:empNo></ns1:EmployeeRequest> 
</soap:Body>  to the body variable


Create a Response Actions, Java Callout to the method getEmpResponseJava from the utility class, this will return the response employee object as reference(make sure you have selected the option Return Parameter as Reference)

Replace the content of body with the required response structure

<open:getEmployeeDetailsResponse><open:return>{$javaresult}</open:return></open:getEmployeeDetailsResponse>


While activating the OSB projcet, OSB creates the Stateless session bean(the registered JNDI will be same as Endpoint URI of the Proxy Service ) and bundle the same as ear file and deploys to weblogic server 



Java client and invoke the proxy service as EJB


package jejbsample;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;

public class ServiceClient {
    public ServiceClient() {
        super();
    }
    public static void main(String[] args) {
            {
                try
                {
                Hashtable<String, String> env = new Hashtable<String, String>();
                env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
                env.put(Context.SECURITY_PRINCIPAL,"weblogic");
                env.put(Context.SECURITY_CREDENTIALS, "welcome1");
                env.put(Context.PROVIDER_URL,"t3://localhost:8000");
                InitialContext ctx = new InitialContext(env);

                System.out.println("Initial Context created");
                Employee request=new Employee();
                request.setEmpNo("48");

                EmployeeDetails empDetails  = (EmployeeDetails)ctx.lookup("EmployeeDetailsPS#jejbsample.EmployeeDetails");
                 // Employee response=  empDetails.getEmployeeDetails(request);
                System.out.println(empDetails.getEmployeeDetails(request).getEmpLoc());
                }
                catch (Exception e)
                {
                e.printStackTrace();
                }
        }
        }
}




No comments:

Post a Comment