JAVA embedding in Oracle SOA 12c

In this post we will explore how to EMBED Java or call Java API from Oracle SOA BPEL process.

In order to demonstrated this, I have created a simple JAVA project with 3 classes.
1.      Account.java -> This class represent Account DTO
      2.     Party.java -> This class represent a party (customer) DTO
      3.     GetNumber.java -> This class contains the method that takes Party DTO as input and apply some business logic and return party’s AccountDTO in response.
public Account getNumber(Party party)

The above classes are created to simulate the calling of actual business logic API’s.

Account.java
Party.java
GetNumber.java
public class Account {
    public Account() {
        super();
    }
    String account;

    public void setAccount(String account) {
        this.account = account;
    }

    public String getAccount() {
        return account;
    }
}

public class Party {
    public Party() {
        super();
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    String name;
}
public class GetNumber {
    public GetNumber() {
        super();
    }

    public Account getNumber(Party party){
        Account account=new Account();
//Just copying input to output for demo
        account.setAccount("Hello "+party.getName());
        return account;
    }

    }

Now we need to create the JAR of the project.  
Right the Java project->deploy as a jar


Now let’s create New SOA project (JAVASOAEMBED). This will create a new project in JDEV.

Now create new sync BPEL 2.0 process in composite.xml.
Request: string input
Response string output


Now add the JAVA Embedding component from oracle extensions to BPEL Process.

Name it as : call Java


Now we will add the code to JAVA activity to call the dummy business API the we created above.
      1.    Add the JAR as a library reference to your JDEV so that it does not give compilation error.
Right Click -> Project Properties -> Libraries and classpath-> add the JAR
      2.     In BPEL 2.0 all the imports for the external classes need to be specific in BPEL process source code.
I.e.
Click on Source code of the BPEL process. Under Process element add below imports

    <import location="oracle.xml.parser.v2.XMLElement" importType="http://schemas.oracle.com/bpel/extension/java"/>
    <import location="testjavasoa.Account" importType="http://schemas.oracle.com/bpel/extension/java"/>
    <import location="testjavasoa.Party" importType="http://schemas.oracle.com/bpel/extension/java"/>
    <import location="testjavasoa.GetNumber" importType="http://schemas.oracle.com/bpel/extension/java"/>


       3. Have added below code which simply takes the input from payload and pass it to JAVA API. Also copy the output from JAVA API to response variable.
Please find the comments below to under the code in more details.

try { 
  
//Fetch the element value from InputVariable using getVariableData() function.

XMLElement input     
= (XMLElement) getVariableData("inputVariable", "payload", "/client:process/client:input");    
addAuditTrailEntry("Input  "+ input.getTextContent());   
String name = input.getTextContent();   

//Pass the input payload data to JAVA API   

GetNumber getNumber = new GetNumber();   
Party p=new Party();   
p.setName(name);   

//The function allow us to add the audit data which is visible when we open the instance flow from EM console.

addAuditTrailEntry(getNumber.getNumber(p).getAccount()); 

//the function will set the data in outputVarialble   
setVariableData("outputVariable", "payload", "/client:processResponse/client:result", getNumber.getNumber(p).getAccount()); 
  
} catch (Exception e) { 
  System.out.println("exception "+e.getMessage()); 
  addAuditTrailEntry(e); 
}
For more info on available function please reference below link:
http://docs.oracle.com/cd/E15586_01/integration.1111/e10224/bp_java.htm
Table 13-1 Built in Methods for bpelx:exec

Please Note below points:
1. setVariableData function requires the variable to be initialized prior. Thus if you try to assign value without initializing then it will thrown Invalid or empty variable exception. For this I added the assign activity before JAVA callout in order to initialize the outputVariable with a blank string.
2. Before deploying the SOA project you need to configure the BPEL classpath properties by mentioning the required JARs.
3. If you face the below “NoClassDefFoundError”, you need to put the jar file in SCA-INF/Lib folder and redploy. 
####<Feb 25, 2016 7:07:48 PM IST> <Error> <oracle.soa.bpel.system> <SML-0646> <DefaultServer> <[ACTIVE] ExecuteThread: '6' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <BEA1-5236741793AD9E472B1E> <4bbddfd6-7fb6-40e9-9ede-6da1762b9bea-000005dd> <1456407468639> <BEA-000000> <Unhandled BPELFault:faultName: {{http://schemas.oracle.com/bpel/extension}runtimeFault}
messageType: {{http://schemas.oracle.com/bpel/extension}RuntimeFaultMessage}
parts: {{
summary=<summary>testjavasoa/GetNumber</summary>
,detail=<detail>java.lang.NoClassDefFoundError: testjavasoa/GetNumber
                at orabpel.bpelprocess1.ExecLetBxExe0.execute(ExecLetBxExe0.java:69)
                at com.collaxa.cube.engine.ext.bpel.common.wmp.BPELxExecWMP.__executeStatements(BPELxExecWMP.java:42)
                at com.collaxa.cube.engine.ext.bpel.common.wmp.BaseBPELActivityWMP.perform(BaseBPELActivityWMP.java:188)
                at com.collaxa.cube.engine.CubeEngine.performActivity(CubeEngine.java:2864)

In this way we can call JAVA API from SOA BPEL process.

Popular posts from this blog

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS