How to fix NPE during WebSocketFactory.upgrade () on embedded Jetty 8.1.12

I am trying to start the cometd websocket server as part of my own war, which launches the berth 8.1.12 in the built-in mode. I can run all other web applications this way, but for the case with websocket, I get this error:

java.lang.NullPointerException at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:238) at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:396) at org.cometd.websocket.server.WebSocketTransport.handle(WebSocketTransport.java:157) at org.cometd.server.CometdServlet.service(CometdServlet.java:166) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686) 

I read all the other posts, and I think this is different.

I have 2 maven projects, one of which creates a uber jar containing the 8.1.12 berth server using the maven-shades plugin, and the other that includes this first jar as a military overlay. The overlay puts all the berth-server classes at the root of the war, so it can be started using "java -jar my.war". Since Cometd also needs a jetty for web maps, I made it double sure that all the dependencies and banks in the WEB-INF / lib directory are 8.1.12 anyway. Thus, there is only one server and its berth 8.1.12. All other messages / questions are related to a non-transport or non-websocket container or non-http connection, but this is a 100% berth 8.1.12, and webapp works great when deployed with a stand-alone external application 8.1.12 .

Dependencies for uberjar from maven-shades (for built-in home-made war with the pier)

 [INFO] com.pgi.pulsar:pulsar-container:jar:1.0-SNAPSHOT [INFO] +- org.eclipse.jetty:jetty-server:jar:8.1.12.v20130726:compile [INFO] | +- org.eclipse.jetty.orbit:javax.servlet:jar:3.0.0.v201112011016:compile [INFO] | +- org.eclipse.jetty:jetty-continuation:jar:8.1.12.v20130726:compile [INFO] | \- org.eclipse.jetty:jetty-http:jar:8.1.12.v20130726:compile [INFO] | \- org.eclipse.jetty:jetty-io:jar:8.1.12.v20130726:compile [INFO] +- org.eclipse.jetty:jetty-servlet:jar:8.1.12.v20130726:compile [INFO] | \- org.eclipse.jetty:jetty-security:jar:8.1.12.v20130726:compile [INFO] +- org.eclipse.jetty:jetty-webapp:jar:8.1.12.v20130726:compile [INFO] | \- org.eclipse.jetty:jetty-xml:jar:8.1.12.v20130726:compile [INFO] +- org.eclipse.jetty:jetty-servlets:jar:8.1.12.v20130726:compile [INFO] | +- org.eclipse.jetty:jetty-client:jar:8.1.12.v20130726:compile [INFO] | \- org.eclipse.jetty:jetty-util:jar:8.1.12.v20130726:compile [INFO] \- junit:junit:jar:4.8.2:test 

Here is the code to launch the built-in pier:

 public class Main { public static void main(String[] args) throws Exception { Server server = new Server(); SocketConnector connector = new SocketConnector(); connector.setMaxIdleTime(1000 * 60 * 60); connector.setSoLingerTime(-1); String portValue = System.getProperty("pulsar.port"); int port = (portValue != null ? Integer.parseInt(portValue) : 8080); connector.setPort(port); server.setConnectors(new Connector[]{connector}); WebAppContext context = new WebAppContext(); context.setServer(server); context.setContextPath("/"); ProtectionDomain protectionDomain = Main.class.getProtectionDomain(); URL location = protectionDomain.getCodeSource().getLocation(); context.setWar(location.toExternalForm()); server.setHandler(context); server.start(); System.in.read(); server.stop(); server.join(); 

Libraries in webapp WEB-INF / libs:

  jar tvf target/pulsar-websockets-1.0-SNAPSHOT.war | grep WEB-INF/lib/jetty | cut -b 36-100 WEB-INF/lib/jetty-client-8.1.12.v20130726.jar WEB-INF/lib/jetty-continuation-8.1.12.v20130726.jar WEB-INF/lib/jetty-http-8.1.12.v20130726.jar WEB-INF/lib/jetty-io-8.1.12.v20130726.jar WEB-INF/lib/jetty-jmx-8.1.12.v20130726.jar WEB-INF/lib/jetty-server-8.1.12.v20130726.jar WEB-INF/lib/jetty-util-8.1.12.v20130726.jar WEB-INF/lib/jetty-websocket-8.1.12.v20130726.jar WEB-INF/lib/jetty-xml-8.1.12.v20130726.jar 

The problem is that it could be related to the problem of loading classes between berth classes such as HttpConnection (I read the post about HttpConnection-ThreadLocal and osgi) that are shared between webapp and the adjacent berth. These classes are actually located in both places, but I cannot find a way to separate them, since they are needed in both places.

Maybe there is a way for the dock to share the classes loaded by the class using the webapp class loader? At the moment I have run out of ideas and I don’t know what I can try next. What can I do to make this work?

+4
source share
2 answers

It worked. After comparing the exact differences between the external berth server that works and the built-in configuration, I was able to get this to work. This required a little trial and error, and it is not entirely obvious, so I share what I did for others.

Firstly, the only box I need in WEB-INF / lib is this:

 jetty-util-8.1.12.v20130726.jar 

All the rest that I have outside for the built-in container. Despite the fact that this bank was present on the way to server classes, the application did not load. The only way this will work is to add only this jar to the / lib directory. This led me to the following error:

 java.lang.IllegalStateException: Websockets not supported on blocking connectors at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:237) at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:396) at org.cometd.websocket.server.WebSocketTransport.handle(WebSocketTransport.java:157) 

Note that he is passing by the NPE, which is a progress. So, looking at the log line on the other working server of the berth and reading a little on non-blocking connectors, I used this connector instead of SocketConector:

 import org.eclipse.jetty.server.nio.SelectChannelConnector; . . . SelectChannelConnector connector = new SelectChannelConnector(); 

These two very simple changes made me work and work.

+1
source

I had the same problem, the application I'm trying to build based on the atmosphere And I ran the application under Jetty-9 .

I found Jetty cans in war/WEB-INF/lib :

jetty-continuation-7.6.7.v20120910.jar jetty-http-7.6.7.v20120910.jar jetty-io-7.6.7.v20120910.jar jetty-security-7.6.7.v20120910.jar jetty-server-7.6.7.v20120910.jar jetty-servlet-7.6.7.v20120910.jar jetty-util-7.6.7.v20120910.jar jetty-websocket-7.6.7.v20120910.jar

I switched to Jetty-8 and the problem disappeared.

0
source

Source: https://habr.com/ru/post/1501291/


All Articles