On Fri, Sep 13, 2013 at 7:56 AM, Igor Urisman <igor.uris...@gmail.com>wrote:
> Dear all, > > It appears that if I obtain the ServletContext object via Spring's > WebApplicationInitializer > mechanism< > http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html > >, > it is not yet seeded with the WebSocket ServerContainer object. > Alternatively, if I use straight up ServletContextListener, all's good. > > More specifically, this works: > > import javax.servlet.ServletContext; > import javax.servlet.ServletContextEvent; > import javax.servlet.ServletContextListener; > import javax.servlet.annotation.WebListener; > > @WebListener > public class ServletBootstrapper implements ServletContextListener { > > @Override > public void contextInitialized(ServletContextEvent sce) { > ServletContext sc = sce.getServletContext(); > ServerContainer serverContainer = (ServerContainer) > sc.getAttribute("javax.websocket.server.ServerContainer"); > } > > @Override > public void contextDestroyed(ServletContextEvent sce) { > // NO-OP > } > } > > But this doesn't: > > import javax.servlet.ServletContext; > import javax.servlet.ServletException; > import org.springframework.web.WebApplicationInitializer; > > public class SpringBootstrapper implements WebApplicationInitializer { > > @Override > public void onStartup(ServletContext sc) throws ServletException { > // Next line returns null > ServerContainer serverContainer = (ServerContainer) > sc.getAttribute("javax.websocket.server.ServerContainer"); > } > } > > I verified that the ServletContext object returned in both is the same, so > that part is good. > > I imagine the root cause is that Spring uses an earlier container event to > post implementations of the WebApplicationInitializer interface, before the > ServerContainer attribute is set by the WebSocket code. Given the ubiquity > of Spring, this may prove problematic. > WebSocket implementation uses a ServletContainerInitializer <http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContainerInitializer.html>to register the servlet context attribute. This can be observed in WsSci implementation. The servlet spec does not guarantees an order of invocations of SCIs. A good problem description can be found here https://java.net/jira/browse/GLASSFISH-20788 Without knowing the Spring implementation can you check whether this is a ordering problem? The spring SCI is invoked before WsSci. cheers Niki > > Many thanks, > -Igor. >