Web services are self-contained, modular business applications that have open, Internet-oriented, standards-based interfaces. Web services are a type of service that can be shared by and used as components of distributed Web-based applications. The software industry is evolving towards loosely coupled service-oriented applications that dynamically interact over the Web. The applications break down the larger software system into smaller modular components, or shared services. These services can reside on different computers and can be implemented by vastly different technologies, but they are packaged and transported using standard Web protocols, such as XML and HTTP, thus making them easily accessible by any user on the Web.
- Web services are accessed over the Web.
- Web services describe themselves using WSDL an XML-based description language.
- Web services communicate with clients (both end-user applications or other Web services) through SOAP a XML messages that are transmitted by standard Internet protocols, such as HTTP.
Need for Web Service Conversation
HTTP architecture has a stateless nature. Once a HTTP request is serviced, the server forgets all about it. Even if the same remote user connects a few seconds later, from the server’s point of view it is a completely new interaction. For developing a typical web application like a shopping cart (where the server is expected to keep a list of items in the cart, and present this list to the client on demand) the state/session of the client’s previous requests has to be maintained. This is the same incase of the client accessing the web services. The System which runs these web services takes care of maintaining the session between the client and the web service provided the web services follows some rules which is described in the rest of this document.
JAX-RPC and Web Services
The Java API for XML-based RPC (JAX-RPC) is a Sun Microsystems specification that defines the server and client API to build and invoking a Web service. In JAX-RPC, a remote procedure call is represented by an XML-based protocol such as SOAP. Although JAX-RPC relies on complex protocols, the API hides this complexity from the application developer. On the server side, the developer specifies the remote procedures by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods. Client programs are also easy to code. After locating the service endpoint by specifying a URL, the client simply invokes the methods on a local object (a stub) that represents the remote service.
JAX-RPC Service Endpoint Lifecycle
According to JAX-RPC specification v1.0, if the soap service implements the ServiceLifeycle interface, the servlet container based JAX-RPC runtime system is required to manage the lifecycle of the corresponding service endpoint instances. The lifecycle of a service endpoint instance is realized through the implementation of the init and destroy methods of the ServiceLifecycle interface. The JAX-RPC runtime system is responsible for loading and instantiation of service endpoint instances. The JAX-RPC runtime system may choose to load and instantiate a service endpoint instance during the startup of the runtime system or when a service endpoint instance is needed to service an RPC request. The JAX-RPC runtime system loads the service endpoint class using the Java class loading facility. After service endpoint class is loaded, the JAX-RPC runtime system instantiates the class.
After the service endpoint instance is instantiated, the JAX-RPC runtime system is required to initialize the endpoint instance before any requests can be serviced. The JAX-RPC runtime system is required to invoke the ServiceLifecycle.init method (if this interface is implemented by the soap service) for the initialization of the service endpoint instance. The service endpoint instance uses the init method to initialize its configuration and setup access to any external resources. The context parameter in the init method enables the endpoint instance to access the endpoint context provided by the underlying JAX-RPC runtime system. Once a service endpoint instance has been initialized (and in a method ready state), the JAX-RPC runtime system may dispatch multiple remote method invocations to the service endpoint instance. These method invocations must correspond to the remote methods in the service endpoint interface implemented by the service endpoint class.
The JAX-RPC runtime system is required to invoke the destroy method when the runtime system determines that the service endpoint instance needs to be removed from service of handling remote invocations. For example, the JAX-RPC runtime may remove an endpoint instance from service when the runtime system is shutting down or managing memory resources. The service endpoint class releases its resources and performs cleanup in the implementation of the destroy method.
After successful invocation of the destroy method, a service endpoint instance is ready for garbage collection. The JAX-RPC runtime system should not dispatch any remote method invocations to a destroyed endpoint instance. The JAX-RPC runtime systems are required to instantiate and initialize a new service endpoint instance for servicing new remote method invocations.
ServletEndpointContext
For a service endpoint components deployed on a servlet container based JAX-RPC runtime system, the context parameter in the ServiceLifecycle.init method is required to be of the Java type javax.xml.rpc.server.ServletEndpointContext. The ServletEndpoint Context provides an endpoint context maintained by the underlying servlet container based JAX-RPC runtime system.
ServletEndpointContext Interface
package javax.xml.rpc.server; public interface ServletEndpointContext { public java.security.Principal getUserPrincipal(); public javax.xml.rpc.handler.MessageContext getMessageContext(); public javax.servlet.http.HttpSession getHttpSession(); public javax.servlet.ServletContext getServletContext(); }
A servlet container based JAX-RPC runtime system is required to implement the ServletEndpoint- Context interface. The JAX-RPC runtime system is required to provide appropriate session, message context, servlet context and user principal information per method invocation on service endpoint instances.
Creating Session Oriented SOAP Services
- The soap service should implement javax.xml.rpc.server.ServiceLifecycle.
- The soap service should define the init( ), a call back method through which
the JAX-RPC runtime system passes the context object to the soap service.
destroy( ) method should also be defined. - The context passed to the soap service from the JAX-RPC runtime system will be of type ServletEndpointContext. ServletEndpointContext.getHttpSession() will get the current http session.
Code Snippet
import javax.xml.rpc.server.ServiceLifecycle; import javax.xml.rpc.server.ServletEndpointContext; import javax.xml.rpc.ServiceException; import javax.servlet.http.HttpSession; import javax.xml.soap.SOAPMessage; import javax.xml.rpc.handler.soap.SOAPMessageContext; public class HelloWorldSession implements ServiceLifecycle{ public ServletEndpointContext endpointContext; public HelloWorldSession(){ } public void init(Object context)throws ServiceException { endpointContext = (ServletEndpointContext)context; } public String getStock(String tickerSymbol){ if(endpointContext != null){ HttpSession session = endpointContext.getHttpSession(); String tickerValue = session.getAttribute(tickerSymbol); If(tickerValue == null){ // Business logic for getting the stock value // Storing the ticker value in the session which can be used in further interactions. session.setAttribute(tickerSymbol,tickerValue); } return tickerValue; } } public void destroy(){ // Implementation for cleaning the unused objects } }
Invoking a Session Based SOAP Service
ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService(new QName("http://...")); Call call = service.createCall(); call.addParameter( "tickerSymbol", new QName("http://www.w3.org/2001/XMLSchema","string"), ParameterMode.IN ); : : call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string")); call.setProperty(call.SESSION_MAINTAIN_PROPERTY,new Boolean(true)); : : call.setTargetEndpointAddress("http://localhost:7001/axis/services/HelloWorldSession"); call.invoke(new QName("http://...","getStock" ), new Object[]{"HPQ"});
Conclusion
This article has discussed the need for Web service conversation and how to achieve conversation using JAX RPC axis. The code examples, which were tried out with Apache axis 1.0 serve as illustration of basic functionalities. In future, the authors hope, that web service conversation will be supported by more standards and the business processes in the enterprise would be able to use them to their fullest advantage.
About the Authors
Kumar Raj Moorthy has been working as a Senior Software Engineer for Hewlett-Packard. He has a Bachelor of Engineering Degree in the field of Computer Science from the Bharathiyar University, Coimbatore India. He has about four years of industry experience. He has being working on Web Service technologies such as WSDL, SOAP, and UDDI for 2 years. Currently he is involved in integrating web applications with telecom network services. He can be reached at kumarraj@india.hp.com.
R Venkatavaradan has been working as a Technical Consultant for Hewlett-Packard. He has a Masters Degree from the School of Automation, Indian Institute of Science, Bangalore. He has about 13 years of industry experience. His field of work ranges from signal processing to Web Services, J2EE, Enterprise Application Integration, and so forth. He has extensively worked on Web Service technologies such as WSDL, SOAP, and UDDI, and provided technical consultancy to various projects in the field of mobile, telecom, and EAI, which have been architected based on Web Service concepts. He can be reached at varadan@india.hp.com.