| 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 ? 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.
<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.
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |


