The Source for Java Technology Collaboration
User: Password:



Bhakti Mehta

Bhakti Mehta's Blog

WS-Reliable Messaging and Session Support (Part3)

Posted by bhaktimehta on June 07, 2007 at 10:23 AM | Comments (1)

This is the third part of tri series blogs where in Part 1 we showed one way of supporting sessions with
WS Reliable Messaging. Mike showed in his blog  WS Reliable Messaging and Session Support Part 2
what are the problems with this approach and we now 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 Sun's 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 WS-RM Policy Assertions.
Here is endpoint implementation class
@WebService(endpointInterface="rmdemo.server.RMDemo")

public class RMDemoImpl {

....

}


As shown in part 1 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,  MessageContext pertaining
to the the current request can be accessed.

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

As mentioned above WebServiceContext exposes  the MessageContext  for the request being served when this method is called .
The session  is obtained from the MessageContext using a jax-ws property "com.sun.xml.ws.session"

  private Hashtable getSession() {
return (Hashtable)context.getMessageContext()
.get("com.sun.xml.ws.session");

}

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() {
Hashtable sess = getSession();
String ret = (String)sess.get("request_record");
return ret != null ? ret : "";
}

  private void setSessionData(String data) {
      Hashtable session = getSession();
session.put("request_record", data);
  }

These addString method and getResult method are exposed by our RM endpoint and are same as Part 1 .
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(); 

  }

Full source code for the RMDemoImpl.java  is here.

Advantages
Here the Session is created every time a new CreateSequence request is received and gets terminated everytime there is a
TerminateSequence request. This way the resource management issue as mentioned by Mike is addressed.

Client Code
Here 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 wsit workspace and this is the README to run the samples.


Additional sources of information

WSIT website
Check this website for latest source code, samples,documentation

Project GlassFish Open Source App Server
All WSIT technologies can be installed on this FREE app server

Netbeans IDE Module:
All WSIT technologies available today can be configured visuall using this Netbeans module.
WS-ReliableMessaging specification

Maintaining Session With JAX-WS








Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Nice Blog Bhakti. I enjoyed reading it.

    Posted by: tjkincaid on June 10, 2007 at 03:37 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds