5 Replies Latest reply on Jul 1, 2010 10:44 PM by Jim Ma

    Web Service annotated with @PostConstruct - wrong order w.r.t Listener

    Newbie

      I have a Web Service:

       

      //javase imports
      import java.io.InputStream;
      import java.io.StringReader;
      import org.xml.sax.InputSource;
      //java eXtension imports
      import
      javax.annotation.PostConstruct;
      import
      javax.annotation.PreDestroy;
      import javax.servlet.ServletContext;
      import javax.xml.namespace.QName;
      import
      javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.soap.MessageFactory;
      import javax.xml.soap.SOAPException;
      import
      javax.xml.soap.SOAPFactory;
      import javax.xml.soap.SOAPFault;
      import javax.xml.soap.SOAPMessage;
      import
      javax.xml.soap.SOAPPart;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.ws.Provider;
      import javax.xml.ws.ServiceMode;
      import javax.xml.ws.WebServiceProvider;
      import
      javax.xml.ws.soap.SOAPFaultException;
      import static javax.xml.ws.Service.Mode.MESSAGE;@WebServiceProvider(     wsdlLocation = "WEB-INF/wsdl/eclipselink-dbws.wsdl",     serviceName = "simpleService",     portName = "simpleServicePort",     targetNamespace = "urn:simpleService" ) @ServiceMode(MESSAGE) public class SimpleProvider implements Provider<SOAPMessage> {     @PostConstruct     public void init() {         System.out.println("*****************init");         ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();         InputStream eclipseLinkStream = parentClassLoader.getResourceAsStream(             "/META-INF/eclipselink-dbws-or.xml");         if (eclipseLinkStream != null) {             System.out.println("/META-INF/eclipselink-dbws-or.xml = " +                 eclipseLinkStream.toString());         }         ServletContext sc = ProviderListener.SC;         if (sc == null) {             System.out.println("servlet context is null");         }         else {             InputStream schemaStream =                 sc.getResourceAsStream("/WEB-INF/wsdl/eclipselink-dbws-schema.xsd");             System.out.println("WEB-INF/wsdl/eclipselink-dbws-schema.xsd = " +                 schemaStream.toString());         }     }
      ...

       

      I also have a ServletContextListener:

      import javax.servlet.ServletContext;
      import javax.servlet.ServletContextEvent;
      import javax.servlet.ServletContextListener;
      public class ProviderListener implements ServletContextListener {         public static ServletContext SC = null;         public  ProviderListener() {         super();     }     public void contextInitialized(ServletContextEvent sce) {         System.out.println("*****************ServletContext initialized");         SC = sce.getServletContext();     }     public void contextDestroyed(ServletContextEvent sce) {         // no-op    } }

       

      When I deploy to Glassfish, WebLogic and WebSphere, the order of execution is: Listener, then @PostConstruct

       

      However, when I deploy to JBoss 5.10 (w Metro), the order is @PostConstruct then Listener.

       

      Is this a known issue, or is there a workaround?

       

      Thanks in advance,

      Mike Norman

        • 1. Re: Web Service annotated with @PostConstruct - wrong order w.r.t Listener
          Jim Ma Apprentice

          Does this cause some problem ? Can you paste your deployment descriptor here ?

          • 2. Re: Web Service annotated with @PostConstruct - wrong order w.r.t Listener
            Newbie

            Yes, it does cause problems - I have some initialization code that I hope to maintain across four AppServers (Glassfish, WebLogic,

            WebSphere and JBoss). The main job of the ServletContextListener is to grab the ServletContext so that later in the @PostConstruct

            init-method on the Web service, I can load some resources that are only available from the ServletContext's classloader. So far,

            I'm hitting 0.750 (I should be in the majors!)

             

            Here is my deployment descriptor for my 'Simple' Web service:

             

            <?xml version="1.0" encoding="UTF-8"?>
            <web-app
              xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
              version="2.5"> 
              <
            listener>
                <listener-class>_dbws.ProviderListener</listener-class>
              </listener>
              <servlet>
                <servlet-name>_dbws.SimpleProvider</servlet-name>
                <servlet-class>_dbws.SimpleProvider</servlet-class>
                <load-on-startup>0</load-on-startup>
              </servlet>
              <servlet-mapping>
                <servlet-name>_dbws.SimpleProvider</servlet-name>
                <url-pattern>/simple</url-pattern>
              </servlet-mapping>
            </
            web-app>
            • 3. Re: Web Service annotated with @PostConstruct - wrong order w.r.t Listener
              Jim Ma Apprentice

              I suspect this is because the eager initialization metro web service in deployment : the  metro web service will be initialized and started before servlet.init().  The jbossws-cxf stack before 3.3.0.GA version supports lazy load. If the jbossws-cxf stack can be your option , you can switch to jbossws-cxf-3.2.2.GA to see if it works for your case.

              • 4. Re: Web Service annotated with @PostConstruct - wrong order w.r.t Listener
                Alessio Soldano Master

                Yes, that's the reason. The endpoint is created at deployment time, before the serving servlet is actually created.

                Jim, is there any explicit reference in specs about ordering of endpoint @PostConstruct invocation and the servlet lifecycle? As far as I remember, jsr109 just say that the @PostConstruct needs to be called after the endpoint is created and before any request is served.

                • 5. Re: Web Service annotated with @PostConstruct - wrong order w.r.t Listener
                  Jim Ma Apprentice
                  Jim, is there any explicit reference in specs about ordering of endpoint  @PostConstruct invocation and the servlet lifecycle? As far as I  remember, jsr109 just say that the @PostConstruct needs to be called  after the endpoint is created and before any request is served.

                   

                  No. I didn't find any spec compliant items except you mentioned one in jsr109.