Sergey Shcherbakov opened SPR-10841 and commented
I'm building a Spring 4 driven application with an embedded Tomcat 8 and SockJS to support WebSocket clients. The latest 4.0.0.BUILD-SNAPSHOT (14.08.2013) as well as 4.0.0.M2 cannot upgrade the incoming HTTP connection to WS with the following exception (for the snapshot version):
2013-08-16 10:49:02,711 15913 ERROR [http-nio-8080-exec-4] org.apache.coyote.http11.Http11NioProtocol - Error reading request, ignored
java.lang.NullPointerException: null
at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.<init>(WsRemoteEndpointImplServer.java:57) ~[tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:116) ~[tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) ~[tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) [tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1592) [tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1550) [tomcat-embed-core-8.0.0-RC1.jar:8.0.0-RC1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_17]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_17]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_17]
Affects: 4.0 M2
Comment From: spring-projects-issues
Rossen Stoyanchev commented
Have you tried the standalone version? I've been using that with both 8.0 RC1 and Tomcat trunk so it would be good to confirm if this is due to the standalone vs embedded version, or something specific in your code that's triggering it.
Comment From: spring-projects-issues
Sergey Shcherbakov commented
Hi Rossen,
The NPE is caused by the webSocketContainer which is not getting passed to the preInit() method. I see that the TomcatRequestUpgradeStrategy is passing null to the preInit() method because the ServerHttpRequest.getServletRequest().getServletContext().getAttribute("javax.websocket.server.ServerContainer") returns null, i.e. the servletContext attribute is not set when my embedded Tomcat gets started by the spring-boot (this may be a spring-boot issue, I looking into it further).
Comment From: spring-projects-issues
Rossen Stoyanchev commented
It sounds SCI scanning is not enabled. It's what underlies the new WebSocket support so if not enabled, there is no ServerContainer. You might want to see https://issues.apache.org/bugzilla/show_bug.cgi?id=55312.
Comment From: spring-projects-issues
Sergey Shcherbakov commented
I have found the way to fix the issue locally. The following customization code suffice to switch on the WS SCI scanning for embedded Tomcat bootstrapped by spring-boot:
@Bean
public TomcatEmbeddedServletContainerFactory tomcatContainerFactory() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setTomcatContextCustomizers(Arrays.asList(new TomcatContextCustomizer[] {
tomcatContextCustomizer()
}));
return factory;
}
@Bean
public TomcatContextCustomizer tomcatContextCustomizer() {
return new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
context.addServletContainerInitializer(new WsSci(), null);
}
};
}
The WS upgrade and communication works perfectly after that. Thanks for the help! The issue can probably be closed now.
PS: The spring-boot team might be interested in switching this customization on by default.
Comment From: spring-projects-issues
Rossen Stoyanchev commented
/cc Dave Syer
Comment From: spring-projects-issues
Rossen Stoyanchev commented
You might want to open an issue with Spring Boot. Doesn't seem to be anything we can fix in the core Spring framework.
Comment From: Safwene12
I Face the some problem , but I can't do the same configuration in the solution since it's depressed in version 2+ , Any Help please