Saturday, September 13, 2008

starting with WSIF

Recently I was working on JCA using WSIF extensions that’s when I got a query on how to get started with WSIF implementations. For those who want to try out WSIF there is a good tutorial on BPEL Cookbook - Using WSIF for Integration. When ever I try out something new I always start with a HelloWorld example which I believe has grown in me from the time I started working in Java. Working example of HelloWorld will always help you in getting started with the new topics, technology and frameworks. Coming back to WSIF I had taken a session on how to create a Helloworld sample in WSIF. These are some steps I did.

Create an empty WSDL file. Add the necessary namespace imports along with the mandatory ones

xmlns:format="http://schemas.xmlsoap.org/wsdl/formatbinding/"

xmlns:java="http://schemas.xmlsoap.org/wsdl/java/"

I decided to go for a person object with 2 elements name and age.

So I added the xsd elements

<schema elementFormDefault="qualified"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://geo.com/domain/person/">

<xsd:complexType name="PersonType">

<xsd:sequence>

<xsd:element name="name" type="xsd:string" />

<xsd:element name="age" type="xsd:string" />

</xsd:sequence>

</xsd:complexType>

</schema>

Use schema compiler utility called schemac provided by BPEL PM (available in bpel/bin) to generate the corresponding java classes.

Then write another java file which has the functionality to be invoked using WSIF. It will create ObjectFactory which will return instances of the required classes.

package com.geo.service;

import com.geo.domain.PersonType;

public class HelloWorld {

public String printHelloWorld(PersonType person) {

return “Hello world from ”+person.getName()+” My Age is ”+person.getAge();

}

}

Compile this class by putting PersonType in classpath and put all these class files in BPEL’s classpath. Either put it in Oracle_Home\bpel\system\classes or add the path where you have classes to bpelcClassPath in the configuration page of BPEL Console

After creating the message types most important part is plugging in of java classes into WSDL. The binding and service tags serve this purpose.

The java class is defined in service as the”<java:address className=”…..”/>

And operation <java:operation methodName=" ….."/>

Code snippets

<binding name="HelloBinding" >

<java:binding/>

<format:typeMapping encoding="Java" style="Java">

<format:typeMap typeName="tns:PersonType"

formatType="com.geo.PersonType" />

<operation name="PrintHello">

<java:operation methodName=" printHelloWorld "/>

<input/>

<output/>

</operation>

</binding>

...

...

<service name="HelloService">

<port name="HelloPort" binding="tns:HelloBinding">

<java:address className="com.geo.HelloWorld"/>

</port>

</service>

Now you are ready to go. Create a bpel process which takes 2 inputs name and age .Create a partner link using this WSIF WSDL. Invoke it from your bpel process. Assign i/p and o/p. Compile and deploy to bpel server. Test it and see your message of HelloWorld.

In this sample we were returning a string that is a primitive type. I decided to go a step forward by replacing the return string with another object.

But that was not as easy as I had expected. We have to add another complex type in <types> and regenerate classes using schemac utility. Then modify the response message type. Add a format typemap corresponding to the new complextype.

Modify our old HelloWorld.java so that it returns new object instead of string. Place the classes in classpath. Test it using another bpel process.

While trying to run the test2 you will be faced with a null pointer exception. We tried by adding returnPart attribute to operation tag.But that also didn’t solve our problem. The issue was with Object factory which was throwing the null pointer. So the only solution that we came up was to replace the schemac created classes(POJOs and factory classes) with our own created POJOs with getters and setters. Thanks to Syam who helped me in solving this issue. Hope this will be helpful to you all who are starting with WSIF.

No comments: