Invoking Java code from the BPEL process

This recipe explains how to use Java code in the BPEL process from within the Java Embedding activity.

Getting ready

For this recipe to complete, we will update the sample from the previous recipe. We will utilize the BPEL_and_Java_2_0 process in order to show how to invoke Java code from the Java Embedding activity. In this recipe, we will read the input date variable and with the help of the Java Embedding activity set the result to the output variable. The output variable will take the input date and calculate which day in week the date presents.

We also need to open the BPEL process schema (BPEL_and_Java_2_0.xsd). Inside the schema we modify the input and output message definition:

<element name="process">
  <complexType>
    <sequence>
      <element name="input_date" type="date"/>
    </sequence>
  </complexType>
</element>

<element name="processResponse">
  <complexType>
    <sequence>
      <element name="day_of_date" type="string"/>
    </sequence>
  </complexType>
</element>

How to do it…

Open the BPEL_and_Java_2_0 process in JDeveloper.

Double-click on the Java_Embedding1 activity. The edit dialogs opens, where we enter the Java code inside the Code Snippet field. We enter the code that will read the content of the input date variable and as an output we return what day is presented by the date:

  1. Read the content of the input variable:
    String input_date= ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/client:process/client:input_date")).getTextContent();
  2. The content is returned as a string, so we have to parse it to to parse it to the java.util.Date object:
    java.text.SimpleDateFormat sdf_input = new java.text.SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); 
    java.util.Date dt= new java.util.Date(); 
     
    try { 
      dt= sdf_input.parse(input_date); 
    } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
    }  

    The parsing part must be enclosed with a try/catch block, as it may throw an error on parsing problems.

  3. We create a simple formatter that takes only a day out of the whole date:
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("E"); 
    String dateStr = sdf.format(dt);     
  4. Finally, we set the value of the formatter date to the output variable:
    setVariableData("outputVariable", "payload", "/client:processResponse/client:day_of_date", dateStr);

How it works…

The code snippet is included in the Java Embedding activity in the BPEL process. When deploying the BPEL process, the BPEL engine tries to compile the code snippet and if that succeeds, the BPEL process gets deployed. We notice that two methods are called out of the context: getVariableData and setVariableData. Those two methods present convenient methods to get value out of the BPEL process variable and to set the value of the BPEL process variables respectively.

When we run the instance of our BPEL process, there is not much information in the Audit Trail:

How it works…

We can see that the Java Embedding activity presents a black box, which simply states it was executed. Namely, the Audit Trail does not capture the intermediate status of the Java Embedding activity execution.

Tip

Note that if we also want to get some intermediate information about Java Embedding activity execution, then we should use the addAuditTrailEntry method.

There's more…

One of the deficiencies of the Java Embedding activity is the restriction of using the import statement inside code snippets. We used fully qualified names in the code. However, the problem in more complex code with in-depth class hierarchies is that code gets hard to read and maintain. Remember, when code is getting more complex, it is time we think of moving the code out of the BPEL process and create an independent web service or EJB. Then, we call the web service or EJB from the BPEL process.

To overcome this obstacle, we can use import statements in the BPEL process definition. Immediately after the <process> tag, we state the Java classes we wanted to import. That way we no longer need to use fully qualified names inside the Java Embedding activity. Let us try now to rewrite our code and import needed classes first. We need to import the following four classes:

<import location="java.text.ParseException" importType="http://schemas.oracle.com/bpel/extension/java"/>
<import location="java.text.SimpleDateFormat" importType="http://schemas.oracle.com/bpel/extension/java"/>
<import location="java.util.Date" importType="http://schemas.oracle.com/bpel/extension/java"/>
<import location="oracle.xml.parser.v2.XMLElement" importType="http://schemas.oracle.com/bpel/extension/java"/>

If we now examine the code itself, it is much cleaner and more understandable:

//get request data       
String input_date= ((XMLElement)getVariableData("inputVariable","payload","/client:process/client:input_date")).getTextContent();       
       
//parse it to the date       
SimpleDateFormat sdf_input = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH);       
Date dt= new Date();       
       
try {       
  dt= sdf_input.parse(input_date);       
} catch (ParseException e) {       
  e.printStackTrace();       
}       
       
//now format it to the new date       
SimpleDateFormat sdf = new SimpleDateFormat("E", Locale.ENGLISH);       
String dateStr = sdf.format(dt);           
       
//assign it to response variable       
setVariableData("outputVariable", "payload", "/client:processResponse/client:day_of_date", dateStr);  

See also

The getVariableData method is described in more detail in the Reading the BPEL process variables recipe in this chapter. To learn more about the setVariableData method, refer to the Setting the BPEL process variables recipe in this chapter as well. We examine the addAuditTrailEntry method in the Adding a log to the BPEL Audit Trail recipe in this chapter.

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

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