Skip to main content

WS-Reliable Messaging and Session Support (Part3)

Posted by bhaktimehta on June 7, 2007 at 10:23 AM PDT


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 href="http://weblogs.java.net/blog/mikeg/archive/2006/08/wsreliable_mess.html">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 href="http://weblogs.java.net/blog/haroldcarr/archive/2006/02/an_overview_of_1.html">Tango
which is Sun's href="http://java.sun.com/webservices/">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 href="http://weblogs.java.net/blog/bhaktimehta/archive/RMDemo.wsdl">
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 {

....

}





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,  href="http://download.java.net/jdk6/docs/api/javax/xml/ws/handler/MessageContext.html">MessageContext
pertaining

to the the current request can be accessed.



  @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 
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");

   } style="font-weight: bold;">



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.




 style="font-weight: normal;">  @WebMethod style="font-weight: normal;">  public void addString(String s )--> style="font-weight: normal;">
      setSessionData(getSessionData() + " " + s); style="font-weight: normal;">
style="font-weight: normal;">  } style="font-weight: normal;">
style="font-weight: normal;">  @WebMethod style="font-weight: normal;">  public String getResult() { style="font-weight: normal;">       return getSessionData();  style="font-weight: normal;">
style="font-weight: normal;">  }



Full source code for the href="https://wsit.dev.java.net/source/browse/wsit/wsit/samples/ws-rm/javaone-2006-demo/rmdemo/server/RMDemoImpl.java?rev=1.7&view=log">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

href="https://wsit.dev.java.net/source/browse/wsit/wsit/samples/ws-rm/javaone-2006-demo/rmdemo/client/DemoClient.java?rev=1.4&view=markup">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. style="font-weight: bold;">



Sample will be bundled in the wsit workspace and this is
the href="https://wsit.dev.java.net/source/browse/wsit/wsit/samples/ws-rm/javaone-2006-demo/rmdemo/client/DemoClient.java?rev=1.4&view=markup">README
to run the samples. style="font-weight: bold;">





style="font-weight: bold;">Additional sources of information



WSIT
website


Check this website for latest source code,
samples,documentation style="font-weight: bold;">


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. style="font-weight: bold;">

href="http://specs.xmlsoap.org/ws/2005/02/rm/ws-reliablemessaging.pdf">WS-ReliableMessaging
specification




href="http://weblogs.java.net/blog/ramapulavarthi/archive/2006/06/maintaining_ses.html">Maintaining
Session With JAX-WS
style="font-weight: bold;">