This recipe explains how to use Java code in the BPEL process from within the Java Embedding activity.
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>
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:
String input_date= ((oracle.xml.parser.v2.XMLElement)getVariableData("inputVariable","payload","/client:process/client:input_date")).getTextContent();
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.
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("E"); String dateStr = sdf.format(dt);
setVariableData("outputVariable", "payload", "/client:processResponse/client:day_of_date", dateStr);
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:
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.
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);
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.