0 Replies Latest reply on Oct 1, 2007 8:32 AM by NageswaraRao Chilakala

    How can we manage session using JAX-WS?

    NageswaraRao Chilakala Newbie

      Hi,

      We are currently using the Jboss 4.0.5 GA, JAX-WS webservices. I am trying to implement session management. I am getting WebServiceContext injection is null. Please find the code below which I have implemented Server and client side for session management. Can anyone help me why I am getting WebServiceContext null. If you have any code works on Jboss4.0.5 it is really helpful for me

      Server Side code
      --------------------
      /*
      * Hello.java
      *
      * Created on September 24, 2007, 10:42 AM
      *
      * To change this template, choose Tools | Template Manager
      * and open the template in the editor.
      */

      package cisco;

      import javax.jws.*;
      import javax.xml.ws.WebServiceContext;
      import javax.annotation.Resource;
      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.xml.ws.WebServiceException;
      import javax.xml.ws.handler.MessageContext;
      import javax.servlet.http.HttpSession;

      @WebService
      public class Hello {
      /**
      * WebServiceContext is injected by the JAX-WS Runtime
      */
      @Resource
      private WebServiceContext wsContext;

      /**
      * another way to inject WebServiceContext
      * make sure this is not a WebMethod
      *
      */
      /*
      @WebMethod(exclude = true)
      @Resource public void initializeContext(WebServiceContext wsContext) {
      System.out.println("Setting WebServiceContext");
      this.wsContext = wsContext;
      }
      */
      @WebMethod
      public int getCounter() {
      MessageContext mc = wsContext.getMessageContext();
      HttpSession session = ((javax.servlet.http.HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST)).getSession();
      // Get a session property "counter" from context
      if (session == null)
      throw new WebServiceException("No session in WebServiceContext");
      Integer counter = (Integer) session.getAttribute("counter");
      if (counter == null) {
      counter = new Integer(0);
      System.out.println("Starting the Session");
      }
      counter = new Integer(counter.intValue() + 1);
      session.setAttribute("counter", counter);

      return counter;

      }

      /**
      * Lifecycle method called before servicing any requests
      */
      @PostConstruct
      @WebMethod(exclude = true)
      public void onBeginService() {
      System.out.println("Called onBeginService: Session test");
      // Initialize resources need by the Service
      // ....
      }

      /**
      * Lifecycle method called before destroying the service instance
      */
      @PreDestroy
      @WebMethod(exclude = true)
      public void onEndService() {
      System.out.println("Called onEndService: Session test");
      // Clean up resources need by the Service
      // ....
      }
      }

      Client Clode:

      try {
      cisco.HelloService service = new cisco.HelloService();
      cisco.Hello port = service.getHelloPort();
      ((BindingProvider)port).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY,new Boolean(true));
      // TODO process result here
      //int result = port.getCounter();
      //out.println("Result = "+result);

      //Hello proxy = new HelloService().getHelloPort();
      //((BindingProvider)proxy).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY,true);
      int result = port.getCounter();
      System.out.println(result);
      result = port.getCounter();
      System.out.println(result);

      } catch (Exception ex) {
      ex.printStackTrace();
      }