Friday, February 26, 2010

Calling external Java code in a Java Activity in BPEL

Create a simple asynchronous BPEL process and give it some logical name




Now select the folder you have created just now and right click on it select new



and chosse java class and say ok



Now give some name to the class just created.I will call it Calculate.



A java code will be created where in we will put some simple logic

public int getValue(String val) {
return 420;}



Now our aim is to call this java class from out Java Embed activity in BPEL.

Just drag and drop a simple java activity in the designer.




now double click on the java embed activity and start coding.

i will use the following code

Element inputValue = (Element)getVariableData("inputVariable", "payload","/client:JavaUsageProcessRequest/client:input");

Calculate calculate =new Calculate();


int newVal=calculate.getValue(inputValue.getNodeValue());

setVariableData("outputVariable", "payload","/client:JavaUsageProcessResponse/client:result", new Integer(newVal));

If you have knowledge of java then you can easily get that i am accessing the input variable and then i have created an object for the Calculator class.

Then i am calling the getValue function in the Calculate class where in i am providing the inputValue

finally i am setting the output variable the result of the getValue() function.

this is the only coding in java Embed activity but we also need to import some class file so we will add the following import statement


<bpelx:exec import="java.util.*"/>
<bpelx:exec import="java.lang.*"/>
<bpelx:exec import="java.rmi.RemoteException"/>
<bpelx:exec import="javax.naming.NamingException"/>
<bpelx:exec import="org.w3c.dom.Element"/>

this you have to provide in the source of the BPEL code.

Again you need to import one more class file that is the Calculate class.

Go to the jdeveloper and find the package for the Calculate class.



so now we need to add one more import statment

<bpelx:exec import="BusinessProcess.Calculate"/>

So now our BPEL process is complete .Just deploy the project.

and invoke it

You will get an ouput

<outputVariable>
- <part xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="payload">
- <JavaEmbedProcessResponse xmlns="http://xmlns.oracle.com/JavaEmbed">

<result>

420

</result>

</JavaEmbedProcessResponse>

</part>
</outputVariable>

As you can see it is the value returned from getValue() function in the Calculate class.So you can use this concept and do your coding.

If there is any external java files are there then Jar up the Java class you want to use and copy the jarfile to the
SOA_HOME\bpel\services\lib directory.

Now you can use the Java coding in java embed activity

No comments: