Oracle BPEL PM – Invoking a (remote) BPEL Service from Java using RMI

5

Next Thursday, June 15th, I will present on the Dutch Java User Group Conference, on the integration between Java on the one hand and BPEL (specifically Oracle BPEL PM) on the other. The presentation is titled: How Java and BPEL join forces – What every Java developer should know about BPEL . In the presentation, I want to a brief introduction of SOA (Service Oriented Architecture) and BPEL and their importance and likely invasion into the world of application development. Then I want to discuss how to make use of BPEL Services from Java Applications –  both formally through the SOAP WebService protocol and less formally and better performing – theoretically at least – as a RMI call to either a local or remote Session Bean. The second part of the presentation will discuss the reverse process: leveraging Java based services from a BPEL process: how can the BPEL process engage functionality that already exists in the organization, implemented using Java. Finally when time permits, we will go into the role Java based Web Applications can play to implement the Human Workflow steps that are part of many (BPEL) Processes.

In this article I will share some of my findings with regards to the local or remote invocation – using RMI – to the Session Bean Oracle BPEL PM exposes for invoking BPEL process programmatically from Java without going to full blown SOAP road.

....

The BPEL Service: CustomerProfileService

First we need a BPEL Service. And a simple one it is! Two PL/SQL packages – representing a CRM application and a Financial Module of an ERP system in the demo application – expose services that provide parts of the Customer Profile we want the BPEL Service to return. Note that most services will not be implemented in PL/SQL; however, for the purpose of the demo, the way in which the BPEL Process is constructed and the way it is invoked, it makes no difference. If you feel like it, just replace one of the PL/SQL package based services with a Siebel API and the other with an Oracle Financials interface and neither the BPEL process nor the calls to it will change.

The service interfaces of these two PL/SQL packages are as follows (note: all source code is downloadable in the Resources section at the end of the article):
 

package crm<br />procedure retrieve_customer_contact_data<br />( p_customer_id in number<br />, p_first_name       out varchar2<br />, p_last_name        out varchar2<br />, p_street           out varchar2<br />, p_house_number     out varchar2<br />, p_postal_code      out varchar2<br />, p_city             out varchar2<br />, p_country          out varchar2<br />, p_birthdate        out date<br />, p_receive_mailings out varchar2  -- Y or N<br />, p_email_address    out varchar2<br />, p_telephone        out varchar2<br />, p_customer_status  out varchar2 -- BRONZE, SILVER, GOLD or nothing at all<br />);<br /><br />package financials<br /><br />procedure retrieve_financial_status<br />( p_customer_id       in number<br />, p_total_revenue     out number<br />, p_credit_limit      out number<br />, p_outstanding_debt  out number<br />, p_avg_pay_delay     out number -- avg number of days from billing-date to pay-date<br />);<br />&nbsp;

 

Building a BPEL process that returns a (partial) CustomerProfile, leveraging these two services, is almost trivial. Let’s go through the motions – assuming that you have installed the packages and underlying tables in a schema in some Oracle database.

1. Start Oracle BPEL PM 10.1.2.0.2 JDeveloper BPEL Designer

2. Create a new Application and a new BPEL Process Project, for example: CustomerProfileService

Select the Template Synchronous BPEL Process.

 

3. Import Services aka Create PartnerLinks 

From the (BPEL) Process  Activities Component Palette, drag and drop a Partner Link to the Partner Links zone in the BPEL diagram. In the Edit Partner Link window that opens, type the Name as CRM and click on the Define Adapter Service icon. This brings up the Adapter Configuration Wizard. In this wizard, choose Database Adapter, select Call PL/SQL Procedure, choose (and if necessary first define) the Connection with the database schema that contains the packages CRM and FINANCIALS. The select the procedure CRM.retrieve_customer_contact_data and click on Finish. Click on OK back in the Edit Partner Link Window. Now we have set up the Partner Link representing the CRM(.retrieve_customer_contact_data) service and we are ready to invoke it from the BPEL process.

Now repeat these steps for the FINANCIALS service, based on the retrieve_financial_status procedure.

4. Prepare the proper data structure for the input and output of the BPEL Service

So far, we have not bothered with the data structures that go into and come out of the BPEL Service CustomerProfileService that we are creating. Now is a good time to start doing so. We will do it the simple and dirty way by creating all complexType definitions in the service WSDL instead of a separate XSD that we then import from the WSDL.
 
Open the file CustomerProfileService.wsdl (the name depends on the name you have given to the project). Locate the element describing the input to the service, in my case CustomerProfileServiceProcessRequest. Change its definition to something like:

&lt;element name=&quot;CustomerProfileServiceProcessRequest&quot;&gt;<br />   &lt;complexType&gt;<br />     &lt;sequence&gt;<br />       &lt;element name=&quot;customerId&quot; type=&quot;integer&quot;/&gt;<br />     &lt;/sequence&gt;<br />   &lt;/complexType&gt;<br />&lt;/element&gt;<br />	&nbsp;<br />

Here we have specified that our service will take a String input parameter that is called customerId. Clearly this is not yet spectacular. Let’s also redefine the CustomerProfileServiceProcessResponse. The next piece of WSDL contains only a small part of what it should be if we have worked out in all detail the data available from the PartnerLink invocations.

&lt;element name=&quot;CustomerProfileServiceProcessResponse&quot;&gt;<br /> &lt;complexType&gt;<br />  &lt;sequence&gt;<br />   &lt;element name=&quot;contactDetails&quot;&gt;<br />    &lt;complexType&gt;<br />     &lt;sequence&gt;<br />      &lt;element name=&quot;name&quot; type=&quot;string&quot;/&gt;<br />      &lt;element name=&quot;street&quot; type=&quot;string&quot;/&gt;<br />      &lt;element name=&quot;houseNumber&quot; type=&quot;string&quot;/&gt;<br />     &lt;/sequence&gt;<br />    &lt;/complexType&gt;<br />   &lt;/element&gt;<br />   &lt;element name=&quot;financialStatus&quot;&gt;<br />    &lt;complexType&gt;<br />     &lt;sequence&gt;<br />      &lt;element name=&quot;totalRevenue&quot; type=&quot;float&quot;/&gt;<br />      &lt;element name=&quot;creditLimit&quot; type=&quot;float&quot;/&gt;<br />     &lt;/sequence&gt;<br />    &lt;/complexType&gt;<br />   &lt;/element&gt;<br />  &lt;/sequence&gt;<br /> &lt;/complexType&gt;<br />&lt;/element&gt;			<br />&nbsp;

We have created two sections in the response message: one with ContactDetails (provided by the CRM service) and one with the FinancialStatus (based on the results from the FINANCIALS service). Based on these definitions for the inputVariable and outputVariable, we can now start creating invocation of the PartnerLinks.

5. Invoke the PartnerLinks and retrieve the results

Our service is almost trivially simple. We receive the inputVariable with a customerId. With that customerId we invoke – in parallel – the CRM and the FINANCIALS partner link and copy the results from these calls to the OutputVariable. Next, we are done!

To have the two services invoked in parallel – will improve performance and there is no reason not to since the calls to CRM and FINANCIALS are unrelated – we first drag a Flow component from the Component Palette onto the BPEL diagram. In the flow, we drag and drop two Scope components, one for each of our service invocations. Call one scope CRM and the other one FINANCIALS. The diagram now looks like this:

 

Next we will flesh out each of the two scopes, creating the actual service calls. Drill into the CRM scope. Drag and drop an Invoke Activity into the CRM scope. Connect the call end of the Invoke to the CRM PartnerLink:

The Invoke dialog window pops up. Use the Auto Create variable for both the Input Variable and the Output Variable. Set the scope to Local Variable.

Now we need to do two more things: assign the customerId value in the InputVariable to the Invoke_CRM_CRM_InputVariable_1 and retrieve the values from the Invoke_CRM_CRM_OutputVariable_1 into the OutputVariable of the BPEL Service.

Drag and drop two Assign Activities; one goes before the Invoke, the other one after.

 

Double click the first Assign. Specify the assignment of the input to the CRM Service:

Click OK twice. Now double click on the second Assign activity. Here we need to specify the retrieval of the service outcome of the OutputVariable. Specify for example using an XPath expression that the contactDetails/name element in the OutputVariable is populated from P_FIRST_NAME and P_LAST_NAME in the service output:

 

The specification for this Assign activity in BPEL syntax is like this:

&lt;assign name=&quot;Assign_CRM_output&quot;&gt;<br />  &lt;copy&gt;<br />    &lt;from expression=&quot;concat(bpws:getVariableData('Invoke_CRM_CRM_OutputVariable_1','OutputParameters','/ns2:OutputParameters/P_FIRST_NAME'),' ',bpws:getVariableData('Invoke_CRM_CRM_OutputVariable_1','OutputParameters','/ns2:OutputParameters/P_LAST_NAME'))&quot;/&gt;<br />    &lt;to variable=&quot;outputVariable&quot; part=&quot;payload&quot; query=&quot;/client:CustomerProfileServiceProcessResponse/client:contactDetails/client:name&quot;/&gt;<br />  &lt;/copy&gt;<br />  &lt;copy&gt;<br />    &lt;from variable=&quot;Invoke_CRM_CRM_OutputVariable_1&quot; part=&quot;OutputParameters&quot; query=&quot;/ns2:OutputParameters/P_STREET&quot;/&gt;<br />    &lt;to variable=&quot;outputVariable&quot; part=&quot;payload&quot; query=&quot;/client:CustomerProfileServiceProcessResponse/client:contactDetails/client:street&quot;/&gt;<br />  &lt;/copy&gt;<br />&lt;/assign&gt; <br />

Now set up the Scope FINANCIALS in the same, with two Assign operations and Invoke of the FINANCIALS partner link.

The complete BPEL Service now looks like this:

 

6. Deploy and Test

Our BPEL Process is ready for deployment. Nothing could be simpler. First ensure that the Oracle BPEL PM Server 10.1.2.0.2 is running. I will assume in this article a local BPEL PM Server. Right click the BPEL Project in the Application Navigator, choose Deploy and select LocalBPELServer, Deploy to defaul domain. The default password for the default domain is bpel.

The BPEL project is now compiled and when successfully compiled will be packaged and deployed on the local Oracle BPEL PM Server. When that is done, start the Oracle BPEL Console to see the Service Status and to test it.

In the Dashboard of the BPEL Console, select the CustomerProfileService. This takes us to the Initiate Page where we can easily test-run our service. Type a number (1..4) for the customerId and click on Post XML Message. This will run the service:

 

 The result of running the service looks like this:

and the visual flow is this one:

Invoking the CustomerProfileService from Java using RMI 

Now we come to the beef of this article: the first part is pretty trivial stuff, though still a pleasure to go through. How to create a client application that makes use of this BPEL Service CustomerProfileService.

1. Start JDeveloper 10.1.3. Create a new Application and a new Project, for example: CustomerProfileRMIClient.

2. Set the Libraries for the project

One of the steps that caused me the most headaches, attaching the correct set of JARs to my Client Application. The following set works for me – though I am not sure whether there are better alternatives or a subset that suffices:

The first four jars are all from the ORACLE_BPEL_HOME\integration\orabpel\lib directory. The Oc4j.jar is from ORACLE_BPEL_HOME\integration\orabpel\system\appserver\oc4j\j2ee\home.

3. Create a CustomerProfile bean

Create a simple class with all properties we need in a CustomerProfile – typically at least the properties returned by the CustomerProfileService.

 

4. Create a CustomerProfileManager class

This class will do the actual invocation through RMI to the remote BPEL Service CustomerProfileService. The class will make a local service available – through a public static method – that allows other classes to retrieve a CustomerProfile for a certain customer(Id):

public static CustomerProfile findCustomerProfile(String customerId)<br />

The class needs imports from a number of RMI and Oracle BPEL related classes and interfaces:

import com.oracle.bpel.client.Locator;<br />import com.oracle.bpel.client.NormalizedMessage;<br />import com.oracle.bpel.client.ServerException;<br />import com.oracle.bpel.client.delivery.IDeliveryService;<br /><br />import java.io.IOException;<br />import java.rmi.RemoteException;<br /><br />import java.util.Hashtable;<br />import java.util.Map;<br />import javax.naming.Context;<br />import org.w3c.dom.Element;<br />&nbsp;

The crucial part of the findCustomerProfile method is setting up the properties for making the RMI call:

  Hashtable jndi = null;<br />  jndi = new Hashtable();<br />  // Change the PROVIDER_URL to your BPEL PM host...<br />  jndi.put(Context.PROVIDER_URL, &quot;ormi://localhost/orabpel&quot;);<br />  jndi.put(Context.INITIAL_CONTEXT_FACTORY, <br />                 &quot;com.evermind.server.rmi.RMIInitialContextFactory&quot;);<br />  // connect to the admin principal                 <br />  jndi.put(Context.SECURITY_PRINCIPAL, &quot;admin&quot;);<br />  jndi.put(Context.SECURITY_CREDENTIALS, &quot;welcome&quot;);<br />  jndi.put(&quot;dedicated.connection&quot;, &quot;true&quot;);<br />  // call to default domain using (default) password bpel<br />  // note: if we run this code in the same JVM as the Oracle BPEL PM, then jndi can be null<br />  Locator locator = new Locator(&quot;default&quot;, &quot;bpel&quot;, jndi);<br />&nbsp;

We provide the location of the RMI server, in this case the OC4J instance that is running the Oracle BPEL PM Server (in my case on my laptop, hence the localhost). Next is the credentials for the OC4J Administrator. Using these RMI connection details, we create a Locator object. This is an Oracle BPEL specific object that helps us connect to a BPEL Domain (default) on the BPEL PM Server. The next piece of code has us link to the generic IDeliveryService SessionBean interface that will take our BPEL Service call and route it to the specific service (BPEL Process):

IDeliveryService deliveryService = <br />   (IDeliveryService)locator.lookupService(IDeliveryService.SERVICE_NAME);<br />// construct the normalized message and send to collaxa server<br />NormalizedMessage nm = new NormalizedMessage();<br />nm.addPart(&quot;payload&quot;, xml);<br />&nbsp;

The main thing we have to do before we can make the call, is prepare the CustomerProfileServiceRequestMessage to pass into the BPEL Service (the xml variable in the last line of the previous snippet):

String xml = <br />  &quot;&lt;CustomerProfileServiceProcessRequest xmlns=\&quot;http://xmlns.oracle.com/CustomerProfileService\&quot;&gt;&lt;customerId&gt;&quot; + <br />  customerId + <br />  &quot;&lt;/customerId&gt;&lt;/CustomerProfileServiceProcessRequest&gt; &quot;;<br />&nbsp;

Now we can make the call:

NormalizedMessage res = <br />   deliveryService.request(&quot;CustomerProfileService&quot;, &quot;process&quot;, nm);<br /><br />   Map payload = res.getPayload();<br />   Element partEl = (Element)payload.get(&quot;payload&quot;); <br />

Now the partEl element contains CustomerProfileServiceProcessResponse. A private static method is used to populate a new CustomerProfile Bean instance from the CustomerProfileServiceProcessResponse:

 

  ...<br />  return buildCustomerProfile(partEl);<br />}<br /><br />private static CustomerProfile buildCustomerProfile(Element serviceResponse) {<br />  CustomerProfile profile = new CustomerProfile();<br />  <br />  Element contactDetails = (Element)(serviceResponse.getElementsByTagName(&quot;contactDetails&quot;)).item(0);<br />  profile.setName((contactDetails.getElementsByTagName(&quot;name&quot;)).item(0).getNodeValue());<br />  profile.setStreet((contactDetails.getElementsByTagName(&quot;street&quot;)).item(0).getNodeValue());<br />  <br />  Element financialStatus = (Element)(serviceResponse.getElementsByTagName(&quot;financialStatus&quot;)).item(0);<br />  profile.setTotalRevenue(Float.parseFloat((financialStatus.getElementsByTagName(&quot;totalRevenue&quot;)).item(0).getNodeValue()));<br />  profile.setCreditLimit(Float.parseFloat((financialStatus.getElementsByTagName(&quot;creditLimit&quot;)).item(0).getNodeValue())); <br />  <br />  return profile;<br />}<br /><br />

The client application that leverages this local CustomerProfileService offered by the CustomerProfileManager looks like this:

package nl.amis.crm;<br /><br />import java.util.Date;<br />import nl.amis.crm.bpel.client.CustomerProfileManager;<br /><br />public class CustomerProfileClient {<br />    public CustomerProfileClient() {<br />    }<br /><br />    public static void printCustomerReport( String customerId) {<br />      try {<br />          CustomerProfile profile = CustomerProfileManager.findCustomerProfile(customerId);<br />          System.out.println(&quot;The Customer Report for &quot;+profile.getName()+&quot; on &quot;+new Date());<br />          System.out.println(&quot;============================================================================&quot;);<br />          System.out.println(&quot;Contact Details:&quot;);<br />          System.out.println(&quot;----------------&quot;);<br />          System.out.println(&quot;Address:&quot;+profile.getStreet()+&quot; &quot;+profile.getHouseNumber());<br />          System.out.println(&quot;Financial Status:&quot;);<br />          System.out.println(&quot;----------------&quot;);<br />          System.out.println(&quot;Total Revenue:&quot;+profile.getTotalRevenue());<br />          System.out.println(&quot;Credit Limit:&quot;+profile.getCreditLimit());<br />      }<br />      catch (Exception e) {<br />          System.out.println(&quot;The Customer Report is not available due to an exception. Our sincere apologies.&quot;);<br />          e.printStackTrace();<br />      }<br />    }<br /><br />    public static void main(String[] args) {<br />      // if no argument passed in on command line, then use 1 as default customerId        <br />      String customerId = args.length&gt;0?args[0]:&quot;1&quot;; <br />      CustomerProfileClient.printCustomerReport(customerId);<br />    }<br />}<br />&nbsp;

The output of runing the client application:

Resources

Download the sources for this article: 

http://technology.amis.nl/wp-content/uploads/images/CustomerProfileRMIClient.zip

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

5 Comments

  1. Shanthi Viswanathan on

    This code is real cool. I have a different quiestion:

    I have Oracle BPEL processes deployed on one server. I have a scheduler on a different server and this scheduler needs to invoke the BPEL process. I want to secure the BPEL process – maybe using OWSM. This scheduler can invoke anything using a shell script or just command line. How can I achieve this?

  2. Lucas I appreciate your efforts for sharing the information and I have been a regular vistor to your blog and found ur blog quite informative. Curiously following your posts. Thank you.

  3. I’m sorry to say that the presentation has not been approved for public availability. The seminar was intended for (could-be) clients and not for “concullega’s”.
    What we (there were 4 presentations) showed to the audience was how to use webservices and the SOA to integrate their business. Internally or with external parties. A live-demo of the creation (in BPEL-designer) and running of a BPEL process was one of the presentations. What we showed was how a simple form that changed the status of a record from NEW to ACCEPTED could trigger a step in the newly created BPEL process. Thus without accessing the actual webservice, and all possible from Oracle Designer, since it’s only PL/SQL. That’s what a lot of clients know and/or want to use.

    I guess you’re giving your demo now, since today is the 15th. Good luck.

  4. Reginald,

    Thanks for your compliments on the contents of the article. The presentation on the 15th will be on a much wider range of topics than just the Java based call into a BPEL Service. I will make available the contents of the entire presentation and all demonstrations in subsequent postings on this weblog.

    Can I find your presentation/demonstration somewhere on the internet? I am very interested to see what you did. Glad it was such a huge success.

    best regards,

    Lucas

  5. Lucas,

    It strikes me as a little odd that you will be giving the presentation on the 15th, but you are basically giving away the contents NOW.
    Today we (Transfer Solutions) have given a seminar around the BPEL subject too and it was a huge success. Lots of (potential) clients still use forms, so we decided to demonstrate a process that was controlled by updating a column in a table via a form (e.g. changing a statusfield). Simple, but very understandable for the audience, since they all know forms and PL/SQL.

    Good luck next Thursday.

    Reginald