Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket : java.nio.channels.ClosedChannelException &

On my Tomcat 8 server I'm using websocket to communicate with my android app. However, in the logs I can see once in a while the following exceptions that I do not understand:

27-Oct-2015 21:57:48.451 SEVERE [http-nio-80-exec-7] org.apache.tomcat.websocket.pojo.PojoEndpointBase.onError No error handling configured for [api.mobile.websocket.WebSocket] and the following error occurred
 java.io.IOException: Broken pipe
    at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
    at sun.nio.ch.IOUtil.write(IOUtil.java:65)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
    at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:124)
    at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:183)
    at org.apache.coyote.http11.upgrade.NioServletOutputStream.doWriteInternal(NioServletOutputStream.java:94)
    at org.apache.coyote.http11.upgrade.NioServletOutputStream.doWrite(NioServletOutputStream.java:61)
    at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.writeInternal(AbstractServletOutputStream.java:165)
    at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.onWritePossible(AbstractServletOutputStream.java:195)
    at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:98)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1452)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

27-Oct-2015 21:57:48.453 SEVERE [http-nio-80-exec-8] org.apache.tomcat.websocket.pojo.PojoEndpointBase.onError No error handling configured for [api.mobile.websocket.WebSocket] and the following error occurred
 java.nio.channels.ClosedChannelException
    at sun.nio.ch.SocketChannelImpl.ensureReadOpen(SocketChannelImpl.java:257)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:300)
    at org.apache.tomcat.util.net.NioChannel.read(NioChannel.java:137)
    at org.apache.coyote.http11.upgrade.NioServletInputStream.fillReadBuffer(NioServletInputStream.java:136)
    at org.apache.coyote.http11.upgrade.NioServletInputStream.doIsReady(NioServletInputStream.java:49)
    at org.apache.coyote.http11.upgrade.AbstractServletInputStream.isReady(AbstractServletInputStream.java:66)
    at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:49)
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:203)
    at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:198)
    at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:96)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

My code looks like this:

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint("/websocket")
public class WebSocket {

    public WebSocket() {}

    @OnOpen
    public void open(Session session) {}

    @OnMessage
    public void processMessage(Session session) {
        String json = JSONBuilder.createJSON();
        try {
            session.getBasicRemote().sendBinary(GZip.compress(json));
        } catch(Exception e) {}
    }


    @OnClose
    public void close(Session session) {}

}

So, what am I doing wrong here?

like image 268
toom Avatar asked Oct 27 '15 21:10

toom


1 Answers

The sockets are being closed from the client (browser / android), perhaps abruptly, causing the IO exception on your server. Handle this with an additional method using the @OnError annotation, along the lines of:

@OnError
public void onError(Session session, Throwable thr) {}

You may want log the error, or ignore it, as you see fit.

like image 121
dashrb Avatar answered Oct 03 '22 19:10

dashrb