The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


WS Reliable Messaging and Session Support (Part1)

Posted by bhaktimehta on August 2, 2006 at 2:31 PM PDT
WS Reliable Messaging and Session Support 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 at  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 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 {

....

}

We create a HashTable of  SessionIds  as the keys and Strings which are entered by the clients as the values.
private final HashTable<String,String> 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,  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 article.

  @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 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 : "";
  }

  private void setSessionData(String data) {
       sessionTable.put(getSessionId(), data);
  }

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 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 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 samples folder.

Instructions to run the sample

Please follow the Readme to setup and run the sample.

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

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.

Slides for TS 1603
Reliable and Transacted Web Services between Javaâ„¢ Technology-Based Project Tango and Microsoft Indigo
 at Javaone 2006
WS-ReliableMessaging specification

Maintaining Session With JAX-WS










Related Topics >> Web Services and XML      
Comments
Comments are listed in date ascending order (oldest first)