Oracle® BPEL Process Manager Developer's Guide
10g Release 2 (10.1.2) B14448-02 |
|
Previous |
Next |
You can embed sections of Java code into a BPEL process.
This chapter contains the following topics:
This use case demonstrates how you can invoke a session bean from within a BPEL process. In this sample, you design the BPEL process so that it picks up a Social Security number from the payload, and then use a Java embedding activity to invoke the session bean (called CreditRating) by using the Social Security number (called ssn
) as a parameter. The CreditRating session bean then returns the credit rating for the given Social Security number.
See Also: The following examples:
|
This chapter explains how you can embed sections of Java code into a BPEL process. This is particularly useful when there is already Java code that can perform the desired function, and you want to use the existing code rather than start over with BPEL.
You can incorporate Java code using any of the following methods:
If the Java application has a BPEL-compatible interface, you either use Web Services Inspection Language (WSIF) binding or wrap the Java code as a SOAP service to use it in a BPEL process.
WSIF Binding is the most common way of using Java code in a BPEL process. This method enables a BPEL process to invoke an Enterprise Java Bean through native J2EE protocol (local or remote method invocation (RMI)). With WSIF binding, a section of the WSDL file defines the protocol for communicating between Java and XML. This approach maintains Java's transactionality and does not sacrifice performance. It is also quicker for you to add WSIF binding to an existing Java application rather than starting over in Oracle BPEL Process Manager. However, WSIF binding has the following drawbacks:
It has less tool support than SOAP services (described next).
It has less interoperability, because each application server needs a specific binding.
Currently, you must write the binding manually.
As an alternative to WSIF binding, you can wrap the Java code as a SOAP service. As with WSIF binding, this method requires that the Java application have a BPEL-compatible interface. A Java application wrapped as a SOAP service appears as any other Web service, which can be used by many different kinds of applications. There are also tools available for writing SOAP wrappers.
However, a Java application wrapped as a SOAP service has the following drawbacks:
It loses performance, because interactions are constantly being mapped back and forth between the Java code and the SOAP wrapper.
It loses interoperability, that is, the ability to perform several operations in an all-or-none mode (such as debiting one bank account while crediting another, where either both transactions must be completed, or neither of them).
Another way to use Java in a BPEL process is to embed the code directly into the BPEL process using the Java BPEL exec extension bpelx:exec
. The benefits of this approach are speed and transactionality. However, you can incorporate only fairly small segments of code. If you want to incorporate larger segments of code, or if the project requires the Java code to have the same look and feel throughout all the BPEL processes being created, consider using WSIF binding or wrapping it as a SOAP service.
The BPEL tag bpelx:exec
enables you to embed a snippet of Java code within a BPEL process. The server executes any snippet of Java code contained within a bpelx:exec
activity, within its Java Transaction API (JTA) transaction context.The BPEL tag bpelx:exec
converts Java exceptions into BPEL faults and then adds them into the BPEL process.The Java snippet can propagate its JTA transaction to session and entity beans that it calls.
For example, the SessionBeanSample.bpel
file uses the following bpelx:exec
tag to embed the invokeSessionBean
Java bean:
<bpelx:exec name="invokeSessionBean" language="java" version="1.4"> <![CDATA[ try { Object homeObj = lookup("ejb/session/CreditRating"); Class cls = Class.forName( "com.otn.samples.sessionbean.CreditRatingServiceHome"); CreditRatingServiceHome ratingHome = (CreditRatingServiceHome) PortableRemoteObject.narrow(homeObj,cls); if (ratingHome == null) { addAuditTrailEntry("Failed to lookup 'ejb.session.CreditRating'" + ". Please make sure that the bean has been" + " successfully deployed"); return; } CreditRatingService ratingService = ratingHome.create(); // Retrieve ssn from scope Element ssn = (Element)getVariableData("input","payload","/ssn"); int rating = ratingService.getRating( ssn.getNodeValue() ); addAuditTrailEntry("Rating is: " + rating); setVariableData("output", "payload", "/tns:rating", new Integer(rating)); } catch (NamingException ne) { addAuditTrailEntry(ne); } catch (ClassNotFoundException cnfe) { addAuditTrailEntry(cnfe); } catch (CreateException ce) { addAuditTrailEntry(ce); } catch (RemoteException re) { addAuditTrailEntry(re); } ]]> </bpelx:exec>
See Also:
|
You can use an XML facade to simplify DOM manipulation. Oracle BPEL Process Manager provides a lightweight Java Architecture for XML Binding (JAXB)-like Java object model on top of XML (called a facade). XML facade provides a Java bean-like front end for an XML document or element that has a schema. Facade classes can provide easy manipulation of the XML document and element in Java programs.
You add the XML facade by using a createFacade
method within the bpelx:exec
statement in the .bpel
file. For example:
<bpelx:exec name= ... <![CDATA ... Element element = ... (Element)getVariableData("input","payload","/loanApplication/"): //Create an XMLFacade for the Loan Application Document LoanApplication xmlLoanApp= LoanApplicationFactory.createFacade(element); ...
To generate the facade classes, use the schemac
tool, which is provided with Oracle BPEL Process Manager. You can find the schemac
tool in the following locations:
c:\orabpel\bin
(for Eclipse BPEL Designer)
Oracle_Home
\integration\orabpel\bin
(for JDeveloper BPEL Designer
To use schemac
, run a command similar to the following to generate the facades from WSDL or XSD files:
C:\BPEL_project_dir\> schemac *.wsdl /*.xsd
After you run schemac
, it creates a src
folder for a HelperService.java
service and a com
folder for the generated Java classes. Oracle provides a sample in the following directories that showcases the use of facade classes in Java bindings:
c:\orabpel\samples\tutorials\702.Bindings\JavaBinding
(for Eclipse BPEL Designer)
Oracle_Home
\integration\orabpel\samples\tutorials\702.Bindings\JavaBinding
(for JDeveloper BPEL Designer)
When it generates the facade, schemac
uses the following files:
Using build.xml
, schemac
generates the source of the facade classes.
The schemac tool creates a Java binding provider class HelperService.java
, which in the 702.Bindings
example is located under 702.Bindings\JavaBinding\src\com\otn\services
. It has one method, which uses the facade classes CommentsType
and CommentType
:
public CommentsType addComment(CommentsType payload, CommentType comment)
To map the XML types to the corresponding facade classes, a Java binding service is defined in the HelperService.wsdl
file. See the format:typeMapping
section of Java binding:
<format:typeMapping encoding="Java" style="Java"> <format:typeMap typeName="tns:commentType" formatType="com.otn.services.CommentType" /> <format:typeMap typeName="tns:commentsType" formatType="com.otn.services.CommentsType" /> </format:typeMapping>
Table 10-1 lists a set of bpelx:exec
built-in methods that you can use to read and update scope variables, instance metadata, and audit trails.
Table 10-1 Built in Methods for <bpelx:exec>
Method Name | Description |
---|---|
|
JNDI access |
|
BPEL Process Manager Locator |
|
Unique ID associated with each instance |
|
Title of this instance |
|
Status of this instance |
|
Six indexes can be used for search |
|
Priority |
|
Who initiated this instance |
|
Second primary key |
|
Metadata for generating lists |
|
Access preference defined in |
|
Add an entry to the audit trail |
|
Access file stored in the suitcase |
|
Access and update variables stored in the scope |
|
|
|
|
|
|
|
|
|
|
In JDeveloper BPEL Designer, you can add the bpelx:exec
activity, and copy the code snippet into a dialog box, as follows:
Drag and drop the Java Embedding activity (with the coffee cup icon) from the Component Palette.
Double-click the Java Embedding activity to display the Java Embedding dialog box.
Name the Java Embedding activity.
In the Code Snippet field, enter (or cut and paste) the Java code.
For example, the bpel:exec
code example described under "Using the bpelx:exec Tag to Embed Java Code Snippets into a BPEL Process" appears as follows:
See Also: "Java Embedding Activity" for additional details about this activity, including adding JAR files to classpaths. |
This chapter demonstrates how you can embed sections of Java code into a BPEL process using one of the following techniques:
If the Java application has a BPEL-compatible interface, you can use WSIF binding or wrap the Java code in a SOAP service.
You can directly embed the Java code by including an inline code snippet using bpelx:exec
. This snippet is executed within the transaction context of Oracle BPEL Server. This method allows you to propagate that transaction to your own session and entity beans. You can use a set of built-in methods to enable the bpelx:exec
snippet to read and update variables, change instance meta data, and throw faults. To simplify DOM manipulation, use an XML facade.