Developing SOA Applications using POJOs

by David Salter | April 2010 | Java

PoJos – Plain Old Java Objects, are, as the name describes, simple, ordinary Java objects. They do not have to be derived from a specific class, nor do they have to implement any specific interfaces. In the modern world of Java EE development where the latest frameworks have made enterprise development easier, wouldn't it be good if we could develop SOA based applications using PoJos? Fortunately, GlassFish ESB (OpenESB) allows us to do this using the PoJo Service Engine. In this article by David Salter, author of Building SOA-Based Composite Applications Using NetBeans IDE 6 and Seam 2.x Web Development, we will cover:

  • GlassFish ESB
  • Creating a PoJo and Deploying to GlassFish ESB
  • Creating A SOAP Binding for PoJos
  • Testing the Composite Application

GlassFish ESB

You may previously have used OpenESB, and if so you will be familiar with GlassFish ESB. GlassFish ESB is simply the OpenESB core runtime, bundled with the GlassFish application server, the NetBeans IDE and some of the more common JBI components already deployed to the ESB.

With release 2.2 of GlassFish ESB, the PoJo Service Engine has been included with the software download. Both GlassFish ESB and a number of additional JBI Components can be downloaded from the Open ESB website at: https://open-esb.dev.java.net/Downloads.html.

Creating a PoJo and Deploying to GlassFish ESB

Using GlassFish ESB, its easy to create PoJos and deploy them to the JBI runtime adding different bindings such as SOAP or REST to it or to deploy them to the BPEL engine and use them as part of a business process. By deploying to GlassFish ESB, we are allowing many different types of client to consume our PoJos.

For this article, I'm going to write a simple PoJo that reverses strings and then deploy this to GlasFish ESB with a SOAP binding. This will allow any web service client that supports SOAP to access the PoJo.

If you are familiar at all with NetBeans, then creating a PoJo for deployment to the ESB will be a simple matter. The first stage is creating a Java project in which the PoJo can be created. Note that PoJos are created in standard Java projects and not in a SOA project template.

Using the "New Project" option in NetBeans, create a new "Java Class Library" project and call it ReversePoJo.

Developing SOA Applications using POJOs

Upon creating a standard Java Project, PoJos can be created in the project by using the New PoJo Service menu option under the ESB category. Select this option and create a new PoJo Service as shown below.

Developing SOA Applications using POJOs

Developing SOA Applications using POJOs

Class Name: ReversePoJo
Package: com.davidsalter.soa.reversepojo
Method Name: reverseString
Input Argument Type: String
Return Type: String

When we press the Finish button, NetBeans will create an empty PoJo using the details supplied. The code is shown below.

@Provider
public class ReversePoJo {

public ReversePoJo() {
}

@Operation (outMessageTypeQN="{http://reversepojo.soa.
davidsalter.com/ReversePoJo/}ReversePoJoOperationResponse")
public String reverseString(String input) {
return input;
}
}

Looking at this code, we can see that this is indeed a simple PoJo. The class does not extend any special framework classes and does not need to implement any framework interfaces. We can see however that there are two annotations used on the class which are required to allow us to deploy the class to GlassFish ESB.

@Provider defines that the PoJo is a JBI provider, i.e. it provides business logic to other components. In this case, we are stating that the ReversePoJo class can provide business logic to other components, either by adding bindings to the service and exposing directly from the ESB, or being invoked within the ESB from the BPEL runtime.

@Operation defines methods in the class that can be consumed - that is methods that other components can invoke. In the simplest case, @Operation methods take a String as a parameter and return a String. More complex types such as org.w3c.dom.Node and javax.jbi.messaging.NormalizedMessage can however be used for both input and output parameters.

To implement the reverseString method in this class, add the following implementation.

@Operation (outMessageTypeQN="{http://reversepojo.soa.
davidsalter.com/ReversePoJo/}ReversePoJoOperationResponse")
public String reverseString(String input) {

StringBuffer reverse = new StringBuffer();
for (int i = input.length(); i > 0; i--) {
reverse.append(input.charAt(i-1));
}

return reverse.toString();
}

This code simply takes the input argument, reverses it and returns it to the caller.

One of the benefits of writing PoJos for deployment to the ESB runtime is that the PoJos can be easily tested. Unit tests can be added to a NetBeans project by selecting the Tools | Create Unit Tests menu option in the project explorer. A simple unit test for this class is shown below.

public class ReversePoJoTest {

public ReversePoJoTest() {
}

@Test
public void testReverseString() {

String input = "abcdefg";
ReversePoJo instance = new ReversePoJo();
String expResult = "gfedcba";
String result = instance.reverseString(input);
assertEquals(expResult, result);

}
}

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z