Invoking Session bean from the BPEL process

Session beans are defined by the Java Enterprise Edition specification. They are divided into stateless and stateful beans. A stateless session bean operates on the operation level. This means that after the operation is executed, the state of the session bean is not preserved. On the contrary, stateful beans preserve state across a multi-operation level.

This recipe will explain how to call a session bean operation from the Java Embedding activity from the BPEL process.

Getting ready

To complete the recipe, we will create a project in JDeveloper and a session bean that will act as exchange money operation on ATM. The session bean will calculate how much money the customer will get, based on the amount of money inserted into ATM, exchange rate of the currency, and deduced fee.

  1. We start by creating a generic project in JDeveloper and naming it ExchangeATM. Right-click on the project node and select the New… option. Select the Session Bean (EJB) option from the New Gallery window.
    Getting ready
  2. In the Create Session Bean wizard select the Enterprise JavaBeans 3.0 option and continue to the next step.
  3. We change the EJB Name and Mapped Name fields in the EJB Name and Options step of the wizard.
    Getting ready
  4. We name the session bean in the next step of the wizard org.packt.ejb.atm.ExchATM_EJB.
  5. In the last step we name the local and remote interfaces and finish the wizard.
    Getting ready

Now we have the session bean created in the JDeveloper project. We have to add the code to perform the calculation and deploy it to the WebLogic server.

  1. We add the following method to all the interfaces and implementation:
    public double exchangeMoney(double originalAmount, String currency);
  2. The implementation looks rather simple. We read the exchange rate and calculate the exchange amount. Based on the exchange amount, we deduce the fee.
    public double exchangeMoney(double originalAmount, String currency) {
      double fee= 0.95; //5 %    
      double rate= ((Double)rates.get(currency)).doubleValue();
      
      double excMoneyNoFee= originalAmount * rate;
      double excMoneyWFee= excMoneyNoFee * fee;
      
      return excMoneyWFee;
    }
  3. We deploy the session bean by configuring the deployment descriptor. We won't describe the deployment process in detail, as it is rather trivial and does not require any special configuration steps.

How to do it…

The following steps will explain how to achieve this:

  1. First we will extend the schema of input and output messages to support the ATM operation. The expanded input message schema is as follows:
    <element name="process">
      <complexType>
        <sequence>
          <element name="input_date" type="date"/>
          <element name="atm_amount" type="double"/>
          <element name="atm_curr" type="string"/>
        </sequence>
      </complexType>
    </element>
  2. The output schema has the following structure after modification:
    <element name="processResponse">
      <complexType>
        <sequence>
          <element name="day_of_date" type="string"/>
          <element name="exchangeMsg" type="string"/>
        </sequence>
      </complexType>
    </element>
  3. Open the BPEL_and_Java_2_0 process. Insert another Java Embedded activity and name it Java_ATM.
  4. Insert the code into Java Embedding activity code snippet. Initially, we read the input parameters as follows:
    String input_amt= ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/client:process/client:atm_amount")).getTextContent();      
    String input_curr= ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/client:process/client:atm_curr")).getTextContent();       
  5. Cast the input amount to the input variable type of the EJB bean:
    double dbl_amt= Double.parseDouble(input_amt); 
  6. We enclose the rest of the code into the try/catch block that handles the exceptions with the Java naming service. Prepare the environment for the Java naming service and create the initial context:
      java.util.Hashtable env = new java.util.Hashtable();    
      // WebLogic Server 10.x connection details    
      env.put( javax.naming.Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );    
      env.put(javax.naming.Context.PROVIDER_URL, "t3://localhost:7001");    
      javax.naming.Context context = new javax.naming.InitialContext( env );    
  7. Initiate the lookup for the EJB session bean:
      Object obj= context.lookup("ExchangeATM_EJB#org.packt.ejb.atm.ExchATM_EJBRemote");    
  8. We perform the operation on the EJB session bean if we receive the reference to it and set the text to the output message:
       if (obj != null)   
      {       
       org.packt.ejb.atm.ExchATM_EJBRemote atmBean= (org.packt.ejb.atm.ExchATM_EJBRemote)obj;       
        
       double exchMoney=atmBean.exchangeMoney(dbl_amt, input_curr);    
         
       setVariableData("outputVariable", "payload", "/client:processResponse/client:exchangeMsg", "ATM gives back " + exchMoney + " EUR.");         
      
      }   

How it works…

We lookup EJBs (Enterprise JavaBeans) through Java Naming and Directory Interface (JNDI). We have access to the JNDI from the Java Embedding activity. Unfortunately, the JNDI of the BPEL process is connected to the BPEL cube instance and EJBs are registered into the Oracle WebLogic server. That is why we have to set up the JNDI environment manually and address EJBs through our own JNDI context.

To learn more on JNDI and RMI, the following resources are available:

Through the JNDI we receive a link to the remote interface of the EJB. Based on the interface definition, we are able to call the methods on the EJB.

Tip

Another possibility to call EJBs is through SOAP messages. The implementation of EJB as a web service is rather simple, as it merely contains the usage of JAX-WS annotations, for example.

There is also another possibility for calling the EJB from the BPEL process and that is through a Spring component. We can develop the Spring component that calls the EJB. In the SCA composite we than add the Spring component and link it with the BPEL process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset