Monday, November 11, 2013

#284 - BPM 11g EJB Service Clients

Scenario: Essentially the same as previous post, except we now have the BPM process exposed as an EJB Service. This is a sync BPM process -you probably won't be having many of these - so this is more applicable to BPEL based composites.



















Notice the SyncOrderService.

I deploy the BPM app and check out the ejb in the JNDI tree -











There it is, last in the list.

I now create an ejb jar to package up the EJB from the BPM App -


















The jar will include the SyncBPMProcessPortType which we will leverage in the EJB client.



















I then created a new project and added the jar to it -
















I created a class with the following code -

package niallcsimpleordersejbserviceclient;


import com.example.loanaudit.Order;

import com.oracle.xmlns.bpmn.bpmnprocess.syncbpmprocess.Start;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import java.math.BigDecimal;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import syncbpmprocess.bpmnprocess.bpmn.com.oracle.xmlns.SyncBPMProcessPortType;


public class SimpleOrdersEJBClient {
    public SimpleOrdersEJBClient() {
        super();
    }

       public String initiateProcessByEJB() throws NamingException,
                                                NoSuchMethodException,
                                                Throwable {
        System.out.println("initiateProcessByEJB()");
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY,
               "weblogic.jndi.WLInitialContextFactory");
        ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
        Context ctx;

        ctx = new InitialContext(ht);
           
        Proxy proxy;

        // preparing the method to be called on the remote EJB

        proxy = (Proxy)ctx.lookup("ejb/SyncOrderProcess");
 
        Method method;

        method =
                SyncBPMProcessPortType.class.getDeclaredMethod("start", new Class[] { Start.class });
     
        Start myStart = new Start();
        Order myOrder = new Order();
        myOrder.setCustCountry("IE");
        myOrder.setCustEmail("n@c.com");
        myOrder.setCustFirstName("Niall");
        myOrder.setCustLastName("C");
        myOrder.setExportRestriction("N");
        myOrder.setOrderNr("123");
        myOrder.setProduct("i");
        myOrder.setQuantity(12);
        myOrder.setSupplier("ORCL");
        myOrder.setUnitPrice(new BigDecimal("199.99"));
        myStart.setOrder(myOrder);
     
     
        //
        InvocationHandler handler = Proxy.getInvocationHandler(proxy);
     
     
        handler.invoke(proxy, method, new Object[] { myStart });
   
     
        return "ok";
    }
}

As you can see instantiate a proxy for our EJB, create the required payload(Order) and then
invoke the start method - which is the EJB service exposure of the BPM process start activity. 

I then create a Servlet that will call this client -

package niallcsimpleordersejbserviceclient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.NamingException;

import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out;
        String str = "INIT";
        out = response.getWriter();
        
        try{
        SimpleOrdersEJBClient client = new SimpleOrdersEJBClient();
        str = client.initiateProcessByEJB();
        }
        catch (Throwable e) {
            }
        out.println("");
        out.println("TestServlet");
        out.println("");
        out.println("The servlet has received a GET. This is the reply .
");
        out.println("
");
        out.close();
        } 
}

I deploy the Servlet to my AdminServer and test - I then check em to see that a new instance has been created.



No comments:

Post a Comment