Developing SOA Applications using POJOs

3 min read

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.

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.

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);

}
}

Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago