6 Replies Latest reply on Sep 14, 2014 10:43 PM by Sai Krishna

    Undertow - null pointer when creating a websocket endpoint

    John Ament Master

      Hi

       

      So I'm creating a websocket endpoint in Undertow.  I get this null pointer exception:

       

      Mar 09, 2014 9:02:07 AM org.xnio.ChannelListeners invokeChannelListener

      ERROR: XNIO001007: A channel event listener threw an exception

      java.lang.NullPointerException

              at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.handleBinaryMessage(AnnotatedEndpoint.java:373)

              at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.access$1100(AnnotatedEndpoint.java:126)

              at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$6.complete(AnnotatedEndpoint.java:332)

              at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$6.complete(AnnotatedEndpoint.java:329)

              at io.undertow.websockets.core.BufferedBinaryMessage.read(BufferedBinaryMessage.java:76)

              at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.onBinary(AnnotatedEndpoint.java:329)

              at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:24)

              at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:15)

              at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)

              at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:615)

              at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:601)

              at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)

              at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66)

              at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:87)

              at org.xnio.nio.WorkerThread.run(WorkerThread.java:531)

       

      I'm thinking I missed something in the setup, but not quite sure (and the error doesn't give much info).

       

      My server endpoint is setup like this:

       

      @ServerEndpoint("/socket")

      public class WebsocketEndpoint extends Endpoint {

          @Override

          public void onOpen(Session session, EndpointConfig endpointConfig) {

              session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

       

       

                  @Override

                  public void onMessage(ByteBuffer byteBuffer) {

                      System.out.println("New message: "+byteBuffer.asCharBuffer().array());

                  }

              });

          }

       

       

          @Override

          public void onClose(Session session, CloseReason closeReason) {

              super.onClose(session, closeReason);

          }

       

       

          @Override

          public void onError(Session session, Throwable thr) {

              super.onError(session, thr);

          }

      }

       

      I'm starting undertow like this:

       

      XnioWorker worker = this.createWorker();

              DeploymentInfo deploymentInfo = new DeploymentInfo()

                      .setClassLoader(ClassLoader.getSystemClassLoader())

                      .setContextPath("/")

                      .setClassIntrospecter(TestClassIntrospector.INSTANCE)

                      .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME,

                              new WebSocketDeploymentInfo()

                                      .setBuffers(new ByteBufferSlicePool(100, 1000))

                                      .setWorker(worker)

                                      .addEndpoint(WebsocketEndpoint.class)

                                      .addListener(new WebSocketDeploymentInfo.ContainerReadyListener() {

                                          @Override

                                          public void ready(ServerWebSocketContainer container) {

                                              deployment = container;

                                          }

                                      })

                      )

                      .setDeploymentName("mydeployment");

              DeploymentManager deploymentManager = Servlets.defaultContainer().addDeployment(deploymentInfo);

              deploymentManager.deploy();

              Undertow server = null;

              try {

                  server = Undertow.builder()

                          .addHttpListener(9991,"0.0.0.0")

                          .setHandler(deploymentManager.start())

                          .build();

                  server.start();

              } catch (ServletException e) {

                  e.printStackTrace();

              }

       

      The client is a Java SE client, just deploying it inside a JUnit test.

       

          @Test

          public void testSoemthing() throws Exception {

              URI uri = URI.create("ws://localhost:9991/socket");

              Session session = ContainerProvider.getWebSocketContainer().connectToServer(QDXClientEndpoint.class,uri);

              System.out.println("Is open"+session.isOpen());

              RemoteEndpoint.Basic basicRemote = session.getBasicRemote();

              String message= "Hello world!";

              ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(Charset.defaultCharset()));

              basicRemote.sendBinary(buffer);

              session.close();

          }

       

      where my client endpoint is annotated:

       

      @ClientEndpoint

      public class QDXClientEndpoint {

          @OnOpen

          public void onOpen(Session session) {

       

       

          }

       

       

          @OnMessage

          public void processMessage(String message) {

              System.out.println("Received message in client: " + message);

          }

       

       

          @OnError

          public void processError(Throwable t) {

              t.printStackTrace();

          }

      }

        • 1. Re: Undertow - null pointer when creating a websocket endpoint
          Jeff Mesnil Master

          Your server endpoint looks strange.

           

          Using the WebSocket API, one of your method should be annotated with @OnMessage to handle the web socket message payload.

           

          We have a quickstart for web socket at quickstart/helloworld-websocket at master · wildfly/quickstart · GitHub that might be helpful.

          • 2. Re: Undertow - null pointer when creating a websocket endpoint
            Tomaz Cerar Master

            John Ament wrote:


            I'm starting undertow like this:

             

            XnioWorker worker = this.createWorker();

                    DeploymentInfo deploymentInfo = new DeploymentInfo()

                            .setClassLoader(ClassLoader.getSystemClassLoader())

                            .setContextPath("/")

                            .setClassIntrospecter(TestClassIntrospector.INSTANCE)

                            .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME,

                                    new WebSocketDeploymentInfo()

                                            .setBuffers(new ByteBufferSlicePool(100, 1000))

                                            .setWorker(worker)

                                            .addEndpoint(WebsocketEndpoint.class)

                                            .addListener(new WebSocketDeploymentInfo.ContainerReadyListener() {

                                                @Override

                                                public void ready(ServerWebSocketContainer container) {

                                                    deployment = container;

                                                }

                                            })

                            )  

            Unrelated to websockets, you should really not create new workers and manually assign them.

            Undertow by default picks good defaults for all workers, threads & buffer pools depending on what kind of hardware it is running.

            Unless you really want to fine tune this, leave it to server itself.

             

            Also you can modify default thread sizes with Undertow.Builder flags, it is much easier that doing it this way.

             

            For junit tests, did you consider using test setup undertow itself uses?

             

            @RunWith(DefaultServer.class) stuff?

            for example Servlet with WebSockets testscase from undertow https://github.com/undertow-io/undertow/blob/master/servlet/src/test/java/io/undertow/servlet/test/websocket/WebSocketServletTest.java

             

            --

            tomaz

            • 3. Re: Undertow - null pointer when creating a websocket endpoint
              Catalin Kormos Newbie

              Hi there,

               

              I'm testing the websockets support in OpenShift, and tried a very simple example (from https://www.openshift.com/blogs/deploy-websocket-web-applications-with-jboss-wildfly). I'm getting the following exception:

               

              2014-06-12 07:15:43,940 ERROR [org.xnio.listener] (default I/O-2) XNIO001007: A channel event listener threw an exception: java.lang.NullPointerException

                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.handleTextMessage(AnnotatedEndpoint.java:281)

                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.access$1000(AnnotatedEndpoint.java:129)

                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$4.complete(AnnotatedEndpoint.java:254)

                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$4.complete(AnnotatedEndpoint.java:250)

                at io.undertow.websockets.core.BufferedTextMessage.read(BufferedTextMessage.java:87)

                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.onText(AnnotatedEndpoint.java:250)

                at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:26)

                at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:15)

                at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92) [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:632)

                at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:618)

                at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92) [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66) [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:87) [xnio-nio-3.2.2.Final.jar:3.2.2.Final]

                at org.xnio.nio.WorkerThread.run(WorkerThread.java:539) [xnio-nio-3.2.2.Final.jar:3.2.2.Final]

               

              Any ideas? current Undertow is at version 1.1.15.Final in OpenShift; As far as I can tell it might have been fixed in the current Undertow version....but in the meanwhile I can't seem to be able to figure out a workaround or something. Any help would be greatly appreciated.

               

              thx,

              Cata

              • 4. Re: Undertow - null pointer when creating a websocket endpoint
                Sai Krishna Newbie

                I am having similar problem. I am able to connect to server end point. when sending a text message @OnOpen from client, I get the null pointer error:

                 

                13:01:31,338 ERROR [org.xnio.listener] (default I/O-2) XNIO001007: A channel event listener threw an exception:

                1. java.lang.NullPointerException

                                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.handleTextMessage(AnnotatedEndpoint.java:281)

                                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.access$1000(AnnotatedEndpoint.java:129)

                                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$4.complete(AnnotatedEndpoint.java:254)

                                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler$4.complete(AnnotatedEndpoint.java:250)

                                at io.undertow.websockets.core.BufferedTextMessage.read(BufferedTextMessage.java:87)

                                at io.undertow.websockets.jsr.annotated.AnnotatedEndpoint$AnnotatedEndpointFrameHandler.onText(AnnotatedEndpoint.java:250)

                                at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:26)

                                at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:15)

                                at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)

                [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                                at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:632)

                                at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:618)

                                at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)

                [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                                at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66)

                [xnio-api-3.2.2.Final.jar:3.2.2.Final]

                                at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:87)

                [xnio-nio-3.2.2.Final.jar:3.2.2.Final]

                                at org.xnio.nio.WorkerThread.run(WorkerThread.java:539)

                [xnio-nio-3.2.2.Final.jar:3.2.2.Final]

                 

                13:01:31,347 INFO  [stdout] (default I/O-2) 3565595 [default I/O-2] ERROR org.xnio.listener  - XNIO001007: A channel event listener threw an exception

                 

                Is there a work around? I need to wrap this ASAP. Any patch jar would help.

                Thanks

                Sai

                • 5. Re: Undertow - null pointer when creating a websocket endpoint
                  Stuart Douglas Master

                  This is a bug that occurs if you send a text message to an annotated endpoint that does not have an @OnMessage method for text messages. This is fixed in Undertow upstream.