Home Web interfaces Expose EJB 3 as Web Services
12 | 03 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Login
Sign here for the NewsLetter.



Poll
What book could be in your wish list next XMas ?
 
JBoss admin resources
Banner
JBoss howto

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More

How do I configure a Queue/Topic to work in a cluster?

JBoss recipe of the day ...
Read More
Expose EJB 3 as Web Services
Written by Mark S.   

Enhancing support for Web services is a driving force introduced since the EJB 2.1 specification. This provides explicit support  for exposing stateless session beans as Web services. Java-based clients may access the Web services through JAX-RPC API, while non-Java clients through SOAP 1.1.  

This tutorial provides an example and discusses how can you expose a Stateless Session Bean as a Webservice endpoint. Also we'll analyze what are the pros and contros of this approach.  

What's the advantage of exposing an EJB as Web service instead of exposing a POJO as Web services ?

At first, the EJB option offers a richer set of protocols for your web services: you can access your services
through SOAP and RMI (simply adding a remote interface).
Second, since the EJB are running in an EJB container, you have access to framework like declarative security and transactions. You can even add another layer using interceptors to your EJB.

Here's a stateless Session bean with a method getUserInfo which returns an object Person
 

package com.sample;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@Stateless
@WebService 
public class SampleWS implements SampleWSLocal {

	public SampleWS() {
	}
	@WebMethod
	public Person getPerson(@WebParam(name = "userId") 
			String userId) { 
		return new Person("michael","jackson");	
	}
}

The only method exposed is getPerson which returns an instance of a simple POJO, Person. When you declare you Session Bean as WebService it means that the Container takes care to generate all the required stubs and the WSDL which describes the Contract between your Web Service and its Consumers.








Notice here: the @WebService annotation is on the Bean class method. If you use the @WebService annotation on the interface, then all public methods on the web service endpoint will be exposed in the web service.

In this sample we return a Person object: how can we return Java Objects to non-java clients ? the answer is that all objects sent via SOAP are serialized into XML. Here's our SOAP response message serialized:

<ns2:getPersonResponse xmlns:ns2="http://neptune/tasklist" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <return>
    <name>michael</name>
    <surname>jackson</surname>
  </return>
</ns2:getPersonResponse>

Best practices

Beware of returning large Object graph to the client: the XML representation of data can be quite big making up huge SOAP messages. In such  situation it's preferred to return simple Integer for example.

Another best practice is to expose only Coarse Grained EJB methods. What are Coarse Grained EJB methods ? Let's make an example: You want to represent an order system with an EJB. You can split each order line its own EJB (fine grained), or you can have one EJB arranging the entire order (coarse grained).
In our scenario it's better to expose EJB as Coarse grained so that you have fewer SOAP-EJB calls,less network load, and fewer transaction context.

Is it possible to expose Stateful session beans as well ? the answer is no because a Stateful Session Bean  cannot have a Web Service Endpoint Interface (SEI). This is because SOAP based Web Services are inherently stateless in nature.

Top-down Web services or Bottom-up ?

This approach for creating Web services is called bottom-up development. As a matter of fact Web Services can be created using two methods: top-down development and bottom-up development. Bottom-up Web services development involves creating a Web service from a Java™ bean or enterprise bean.
Top-down Web services are created from a Contract, stated by the WSDL, and next represented by a Class.

Which approach is better ? SOA analyst consider Top-Down development as the pure approach because the nature of the Web services is driven directly by the business and at last engineered in an Object. On the other hand bottom-up Web service development may be faster and easier, especially if you are new to Web services.

My personal opinion is that Top-Down approach might be the right choice for start-up projects, assumed that you have the necessary skills. While bottom-up Web services could be the solution for exposing your EJB layer as Web services, assuming that this layer has already been consolidated.


JBoss.org Search
Custom Search
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."