Skip to main content

WS Reliable Messaging and Session Support (Part1)

Posted by bhaktimehta on August 2, 2006 at 2:31 PM PDT

This sample was shown as a demo in the session Reliable and Transacted Web
Services between Java Technology-Based Project Tango and Microsoft
Indigo" TS-1603 Javaone 2006.

This is a part of tri series blogs where in Part 1 we will show one way
of supporting sessions with WS Reliable Messaging. Then Mike will show in his blog what are the problems with this approach and we will conclude with a third part where we have tried to fix some of these
problems .

Sessions are unique ids used to identify a client. They would help
maintain state for each client. In this sample you will see how
sessions can be supported with WS Reliable Messaging (WS RM) .
WS RM is one of the enterprise features in Project Tango
which is Java Web Services interoperability project with Microsoft's Windows Communication Foundation (WCF).

The following snippets of code show how the JAX-WS Endpoint
implementation which is RM enabled supports sessions .

This is the
wsdl
which shows RM is enabled in the endpoint by the presence of href="http://specs.xmlsoap.org/ws/2005/02/rm/WS-RMPolicy.pdf">WS-RM
Policy Assertions.

Here is endpoint implementation class

@WebService(endpointInterface="rmdemo.server.RMDemo")
  public class RMDemoImpl

We create a HashTable of  SessionIds  as the keys and Strings
which are entered by the clients as the values.

private final HashTable sessionTable = new HashTable<String, String()

JAX-WS uses annotations defined by Common Annotations for the Java
Platform (JSR 250),
to inject the Web ServiceContext and declare lifecycle methods. Web
ServiceContext holds the

context information pertaining to a request being served. With JAX-WS Web
Service all you need to do is mark a field or method
with @Resource.  From the WebServiceContext,  href="http://download.java.net/jdk6/docs/api/javax/xml/ws/handler/MessageContext.html">MessageContext
pertaining to the the current request can be accessed. More information
on how MessageContext  can be used to share metadata is explained in this href="https://jax-ws.dev.java.net/articles/MessageContext.html">article.

@Resource
    private javax.xml.ws.WebServiceContext context;

As mentioned above WebServiceContext exposes  the href="http://download.java.net/jdk6/docs/api/javax/xml/ws/handler/MessageContext.html">MessageContext 
for the request being served when this method is called .

The session id is obtained from the MessageContext using a jax-ws property
"com.sun.xml.ws.sessionid"

private String getSessionId() { 
       return (String)context.getMessageContext().get("com.sun.xml.ws.sessionid");
}

The following getter method returns the String associated with each request . Correspondingly the setter method 
stores the Strings for each session in the
HashTable respectively

 private String getSessionData() { 
         String id = getSessionId();
         String ret = sessionTable.get(id);
         return ret!= null ? ret : "";
      }
    

These addString method and getResult method are exposed by our RM
endpoint. The method addString  takes a String as entered by a user and adds it to the Strings stored in the session data for that session.
The getResult will return all the strings entered by the user during
that session.

@WebMethod
public void addString(String s )
    setSessionData(getSessionData() + " " + s); }
@WebMethod
public String getResult() {
   return getSessionData()
}

This example is a good demonstration of how href="http://specs.xmlsoap.org/ws/2005/02/rm/ws-reliablemessaging.pdf">InOrder
delivery assurance (i.e messages are delivered in

the order that they are sent)  in WS Reliable Messaging works. If the ordered
delivery would not work right then the strings stored in the session would not have been in the right order.
Full source code for the RMDemoImpl.java  is here.

Client CodeHere
is the source code for the client. The client enters  various input Strings and finally hits carriage return to terminate the client application and all the Strings received
so far for that client will be returned by the
server.

Sample will be bundled in the href="https://wsit.dev.java.net/source/browse/wsit/wsit/samples/">samples
folder.
Instructions to run the sample
Please follow the href="https://wsit.dev.java.net/source/browse/wsit/wsit/samples/ws-rm/javaone-2006-demo/rmdemo/README">
Readme to setup and run the sample.



For any questions or feedback
on the sample please write to users@wsit.dev.java.net

Related Topics >>