Teaching how to integrate with Java code in Oracle SOA (composites)

I am using the pre-built virtual image that Oracle provides as a public VM in OTN (http://www.oracle.com/technetwork/middleware/soasuite/learnmore/soa-vm-2870913.html). If you don’t have an existing environment, you can download this VM and use it as part of the following exercises.

Java snippets

Let’s add a Java snippet to demonstrate how simple it is to add Java embedded logic into BPEL. For this, let’s add a Java snippet into the BPEL process that invokes the File Adapter. Then let’s log the content of the user as well.

  • Drag and drop a Java Embedding snippet after the Invoke activity to the file adapter.
  • Double click the Java snippet. We can start adding Java logic in the text area. In this case, we want to create an XML DOM object containing the value of the InputVariable as received by the BPEL and then we are converting that into a String representation, so that we can write it down into the system console:

    This is the script, in case you want to copy paste it:

System.out.println(“CRI: JMS Adapter process picked up a message”);

XMLElement inputParam = (XMLElement)getVariableData(

“Receive1_consumeUsers_InputVariable”,

“opaque”,

“/ns3:opaqueElement”);

String input_str = inputParam.getTextContent();

System.out.ptintln(“CRI: Input String is [” + input_str + “]”);

Notice that Receive1_consumeusers_InputVariable/opaque/ns3:opaqueElement is the canonical name of the my input variable. It might vary from environment to environment, depending on what name you gave to the variable in the Assign activity.

Note: You can see yours if you open any assign activity and expand this variable. For example in the following example, it is “Receive1_consumeUserMessages_InputVariable” – Get yours right!

  • Save all and try to deploy.
  • You should get an error saying that we are using a class XMLElement, but it cannot be found in the classpath.

This basically means that we have not imported this class. It is very simple to add external classes and libraries into BPEL. You just have to make sure that the libraries are available and loaded into CLASSPATH, either at the project level (SCA-INF/lib or SCA-INF/classes) or at the domain level (WebLogic libraries). Then on the specific BPEL where you need a particular Class, simply add an import statement on the BPEL you need – Remember that BPEL components are Java classes at runtime, so think of them as if they were Java classes that you are implementing.

Normally you don’t write a lot of complex Java logic inside java Snippets. Normally what you do is to use your own IDE such as Eclipse or JDev to write the Java classes and then you simply import those classes/jars into your SOA-INF libraries and access Java functions from the java Snippets. Also you can create custom XPATH functions that point to Java Classes, so that you can use them as part of XSLT/XQuery XPATH transformations.

In this case the class XMLElement, for sure is loaded as part of the standard SOA domain. It is just a matter of having to import it into the BPEL component.

  • Click on the Source tab of the BPEL to get to the actual XML based source code. Then at the top, add another import statement. In this case:

<import location=”oracle.xml.parser.v2.XMLElement” importType=”http://schemas.oracle.com/bpel/extension/java”/&gt;


    The errors should now go away.

  • Save All and re-deploy the project. First make sure that the composite instance did not have any error and that the Java snippet ran successfully

  • Then go to the log files under /u01/fmw/soacd/user_projects/domains/compact_domain/servers/AdminServer/logs/AdminServer.out and verify that your log message is there.
  • It should look something like:

  • Take a moment to analyse this output:
    • Why is the output a binary format?
    • Why we are not seeing the standard XML that we stored in the JMS queue?
  • The situation here is that we decided on purpose to use an “opaque element“, which basically means that we don’t care about the format, so BPEL treats it as a base64binary format. Let’s take this opportunity to do something that could be quite hard in BPEL, XPATH or even XQUERY, but it results simple in Java, we just need to decode a base64binary message back to String.
  • Let’s add a few more Java lines inside our snippet.

Basically, we are using a Base64Decoder (notice how I can also use full canonical Java Classes and avoid importing them) to decode the message. Pretty easy huh?

System.out.println(“CRI: JMS Adapter process picked up a message”);

XMLElement inputParam = (XMLElement)getVariableData(

“Receive1_consumeUsers_InputVariable”,

“opaque”,

“/ns3:opaqueElement”);

String input_str = inputParam.getTextContent();

System.out.ptintln(“CRI: Input String is [” + input_str + “]”);

// Decoding:

try{

oracle.soa.common.util.Base64Decoder decoder = new oracle.soa.common.util.Base64Decoder();

String decoded = decoder.decode(input_str);

System.out.println(“CRI: Base64decode message decoded is [” + decoded + “]”);

} catch(Exception e){

System.out.println(“CRI: Error while trying to decode element [” + e + “]”);

e.printStackTrace();

}

  • Save All and re-deploy.
  • Send another test and have a look at the console output. This time you should see a decoded message as well.

Teaching how to integrate with the Spring Framework

Another very common and efficient way to integrate with existing Java logic is by utilising the Spring framework. The idea behind the scenes is that you can bring a lot of Java Logic into a new SOA composite and leverage such logic by making internal wiring (same JTA) or SOAP invocations of such logic extremely easy. Follow the next instruction to test this functionality:

OVERVIEW

This sample contains instructions on creating and testing a simple “HelloWorld” Composite with a Spring Component.

STEP-BY-STEP INSTRUCTIONS
  • Create a new SOA Project within your existing Application with whatever name you want and click Next.

  • Choose “Composite with Spring Context” and click Finish.

    • Once you click “Finish”, the New Spring Context Dialog launches. Provide the name given below for the Spring Context and click OK.

    • You can now see the newly created Spring Service component within your composite.

    • Next, create a new Java Interface for your Spring Bean. Right click on your project > New > Java Interface.
    • Name the interface IHelloWorld within the following package.

    • Add one simple method within the interface named greetUser as shown below.

    • Next, right click on the project again, but this time to create a new Java Class that implements the above interface within the same package and name it HelloWorld, as shown below.

    • Implement the method within the class to return a greeting message as indicated below.

    • Next, open up the Spring Context file and define the Bean and the SCA service within. The target attribute of the service refers to the id of the bean. The class attribute of the bean refers to the fully qualified name of the implementation class. The type attribute of the service refers to the interface that the spring bean implements.

    • Open up the Composite in the graphical mode and then drag and drop the Spring component onto the Services swim lane. Choose to create a Web Services binding for the component.

    • Your composite should look like this:

    • Save All and deploy the Composite.
    • Navigate to the EM Test Page for the Service and enter an input value for the service. Click “Test Web Service”.

    • The Service will respond with the String “Hello” followed by the input you specified.

    • The flow trace will indicate that the invocation was successful.

    Congratulations you have created and successfully tested different methods to incorporate Java as part of your OSB/SOA projects.

Author: Carlos Rodriguez Iturria

I am extremely passionate about people, technology and the most effective ways to connect the two by sharing my knowledge and experience. Working collaboratively with customers and partners inspires and excites me, especially when the outcome is noticeable valuable to a business and results in true innovation. I enjoy learning and teaching, as I recognise that this is a critical aspect of remaining at the forefront of technology in the modern era. Over the past 10+ years, I have developed and defined solutions that are reliable, secure and scalable, working closely with a diverse range of stakeholders. I enjoy leading engagements and am very active in the technical communities – both internal and external. I have stood out as a noticeable mentor running technology events across major cities in Australia and New Zealand, including various technology areas such as, Enterprise Integrations, API Management, Cloud Integration, IaaS and PaaS adoption, DevOps, Continuous Integration, Continuous Automation among others. In recent years, I have shaped my role and directed my capabilities towards educating and architecting benefits for customers using Oracle and AWS Cloud technologies. I get especially excited when I am able to position both as a way to exceed my customers’ expectations. I hold a bachelor degree in Computer Science and certifications in Oracle and AWS Solutions Architecture.

1 thought on “Teaching how to integrate with Java code in Oracle SOA (composites)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s