Introduction to Execution of One-way Invocations

12-6 Oracle Fusion Middleware Developers Guide for Oracle SOA Suite 13 Incorporating Java and Java EE Code in a BPEL Process 13-1 13 Incorporating Java and Java EE Code in a BPEL Process This chapter describes how to incorporate sections of Java code into BPEL process service components in SOA composite applications. This chapter includes the following sections: ■ Section 13.1, Introduction to Java and Java EE Code in BPEL Processes ■ Section 13.2, Incorporating Java and Java EE Code in BPEL Processes ■ Section 13.3, Adding Custom Classes and JAR Files ■ Section 13.4, Using Java Embedding in a BPEL Process in Oracle JDeveloper ■ Section 13.5, Embedding Service Data Objects with bpelx:exec ■ Section 13.6, Sharing a Custom Implementation of a Class with Oracle BPEL Process Manager

13.1 Introduction to Java and Java EE Code in BPEL Processes

This chapter explains how to incorporate sections of Java code into a BPEL process. This is particularly useful when there is Enterprise JavaBeans Java code that can perform the necessary function, and you want to use the existing code rather than start over with BPEL.

13.2 Incorporating Java and Java EE Code in BPEL Processes

There are several methods for incorporating Java and Java EE code in BPEL processes: ■ Wrap as a Simple Object Access Protocol SOAP service ■ Embed Java code snippets into a BPEL process with the bpelx:exec tag ■ Use an XML facade to simplify DOM manipulation ■ Use bpelx:exec built-in methods ■ Use Java code wrapped in a service interface

13.2.1 How to Wrap Java Code as a SOAP Service

You can wrap the Java code as a SOAP service. 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. 13-2 Oracle Fusion Middleware Developers Guide for Oracle SOA Suite

13.2.2 What You May Need to Know About Wrapping Java Code as a SOAP Service

A Java application wrapped as a SOAP service has the following drawbacks: ■ There may be reduced performance due to the nature of converting between Java and SOAP, and back and forth. ■ Since SOAP inherently has no support for transactions, this method loses atomic transactionality, 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.

13.2.3 How to Embed Java Code Snippets into a BPEL Process with the bpelx:exec Tag

You can embed Java code snippets directly into the BPEL process using the Java BPEL exec extension bpelx:exec. The benefits of this approach are speed and transactionality. It is recommended that you incorporate only small segments of code. BPEL is about separation of business logic from implementation. If you remove a lot of Java code in your process, you lose that separation. Java embedding is recommended for short utility-like operations, rather than business code. Place the business logic elsewhere and call it from BPEL. 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, a SessionBeanSample.bpel file uses the bpelx:exec tag shown in Example 13–1 to embed the invokeSessionBean Java bean: Example 13–1 bpelx:exec Extension bpelx:exec name=invokeSessionBean language=java version=1.5 [CDATA[ try { Object homeObj = lookupejbsessionCreditRating; Class cls = Class.forName com.otn.samples.sessionbean.CreditRatingServiceHome; CreditRatingServiceHome ratingHome = CreditRatingServiceHome PortableRemoteObject.narrowhomeObj,cls; if ratingHome == null { addAuditTrailEntryFailed to lookup ejb.session.CreditRating + . Ensure that the bean has been + successfully deployed; return; } CreditRatingService ratingService = ratingHome.create; Retrieve ssn from scope Element ssn = ElementgetVariableDatainput,payload,ssn; int rating = ratingService.getRating ssn.getNodeValue ; addAuditTrailEntryRating is: + rating; setVariableDataoutput, payload, tns:rating, new Integerrating; Incorporating Java and Java EE Code in a BPEL Process 13-3 } catch NamingException ne { addAuditTrailEntryne; } catch ClassNotFoundException cnfe { addAuditTrailEntrycnfe; } catch CreateException ce { addAuditTrailEntryce; } catch RemoteException re { addAuditTrailEntryre; } ]] bpelx:exec

13.2.4 How to Embed Java Code Snippets in a BPEL Process in BPEL 2.0

The examples in this chapter focus primarily on how to embed Java code snippets with the bpelx:exec extension. For BPEL projects that support version 2.0 of the BPEL specification, the syntax is slightly different. The bpelx:exec extension and Java code are wrapped in an extensionActivity element. Example 13–2 provides details. Example 13–2 bpelx:exec Extension in BPEL 2.0 extensionActivity bpelx:exec language=java [CDATA[ java code ]] bpelx:exec extensionActivity When you drag a Java Embedding activity into a BPEL process in Oracle BPEL Designer, the extensionActivity element and bpelx:exec tag are automatically added. Example 13–3 shows the import syntax for BPEL 2.0: Example 13–3 Import Syntax in BPEL 2.0 import location=classpackage name importType=http:schemas.oracle.combpelextensionjava Example 13–4 shows a BPEL file with two Java embedding activities for a project that supports BPEL version 2.0. Example 13–4 Java Embedding Activities in a BPEL File for Version 2.0 process name=Test targetNamespace=http:samples.otn.combpel2.0ch10.9 . . . . . . import location=oracle.xml.parser.v2.XMLElement importType=http:schemas.oracle.combpelextensionjava . . . sequence . . . extensionActivity bpelx:exec language=java XMLElement elem = XMLElement getVariableDataoutput, payload; elem.setTextContentset by java exec; bpelx:exec extensionActivity