Search in sources :

Example 11 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project jimfs by google.

the class JimfsAsynchronousFileChannel method closedChannelFuture.

/**
   * Immediate future indicating that the channel is closed.
   */
private static <V> ListenableFuture<V> closedChannelFuture() {
    SettableFuture<V> future = SettableFuture.create();
    future.setException(new ClosedChannelException());
    return future;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 12 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project rest.li by linkedin.

the class RAPResponseDecoder method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    Timeout<None> timeout = ctx.channel().attr(TIMEOUT_ATTR_KEY).getAndRemove();
    if (timeout != null) {
        timeout.getItem();
    }
    if (_chunkedMessageWriter != null) {
        _chunkedMessageWriter.fail(new ClosedChannelException());
        _chunkedMessageWriter = null;
    }
    ctx.fireChannelInactive();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) None(com.linkedin.common.util.None)

Example 13 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project camel by apache.

the class HttpServerMultiplexChannelHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    HttpServerChannelHandler handler = (HttpServerChannelHandler) ctx.getAttachment();
    if (handler != null) {
        handler.exceptionCaught(ctx, e);
    } else {
        if (e.getCause() instanceof ClosedChannelException) {
            // The channel is closed so we do nothing here
            LOG.debug("Channel already closed. Ignoring this exception.");
            return;
        } else {
            if ("Broken pipe".equals(e.getCause().getMessage())) {
                // Can't recover channel at this point. Only valid thing to do is close. A TCP RST is a possible cause for this.
                // Note that trying to write to channel in this state will cause infinite recursion in netty 3.x
                LOG.debug("Channel pipe is broken. Closing channel now.", e);
                ctx.getChannel().close();
            } else {
                // we cannot throw the exception here
                LOG.warn("HttpServerChannelHandler is not found as attachment to handle exception, send 404 back to the client.", e.getCause());
                // Now we just send 404 back to the client
                HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
                response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
                response.headers().set(Exchange.CONTENT_LENGTH, 0);
                // Here we don't want to expose the exception detail to the client
                response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
                ctx.getChannel().write(response).syncUninterruptibly();
                // close the channel after send error message
                ctx.getChannel().close();
            }
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Example 14 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project camel by apache.

the class HttpServerMultiplexChannelHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    Attribute<HttpServerChannelHandler> attr = ctx.channel().attr(SERVER_HANDLER_KEY);
    HttpServerChannelHandler handler = attr.get();
    if (handler != null) {
        handler.exceptionCaught(ctx, cause);
    } else {
        if (cause instanceof ClosedChannelException) {
            // The channel is closed so we do nothing here
            LOG.debug("Channel already closed. Ignoring this exception.");
            return;
        } else {
            // we cannot throw the exception here
            LOG.warn("HttpServerChannelHandler is not found as attachment to handle exception, send 404 back to the client.", cause);
            // Now we just send 404 back to the client
            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
            response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
            response.headers().set(Exchange.CONTENT_LENGTH, 0);
            ctx.writeAndFlush(response);
            ctx.close();
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 15 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerSimpleRequestTimeout.

@Test
public /**
   * Simulates the follow communication with no request timeout
   *
   * <table>
   * <th><td>client</td><td>server</td></th>
   * <tr><td>send "hello"</td><td></td></tr>
   * <tr><td></td><td>start read timeout</td></tr>
   * <tr><td>induce timeout</td><td></td></tr>
   * <tr><td>generate read timeout</td><td></td></tr>
   * <tr><td></td><td>disconnect client</td></tr>
   * <tr><td>send "eom"</td><td></td></tr>
   * <tr><td>detect it has been disconnected</td><td></td></tr>
   * </table>
   */
void testServerSimpleRequestTimeout() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(3, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(3, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    //hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    //hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    //send a request
    ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
    ChannelFuture writeFuture = clientConn.getChannel().write(msg);
    //wait for the request to propagate
    try {
        writeFuture.await(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertTrue(writeFuture.isDone(), "write completed");
    Assert.assertTrue(writeFuture.isSuccess(), "write successful");
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "hello", "message read");
    //start server read timeout
    srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    //Timeout
    try {
        Thread.sleep(300);
    } catch (InterruptedException ie) {
    }
    ;
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    writeFuture = clientConn.getChannel().write(msg2);
    //wait for the respomse to propagate
    try {
        writeFuture.await(10);
    } catch (InterruptedException ie) {
    }
    ;
    //start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertTrue(srvExceptionListener.getLastException() instanceof ReadTimeoutException, "server read timeout");
    Assert.assertTrue(clientExceptionListener.getLastException() instanceof ClosedChannelException, "failed write");
    Assert.assertTrue(!lastSrvConnPipeline.getChannel().isConnected(), "client has been disconnected");
    Assert.assertTrue(!clientPipeline.getChannel().isConnected(), "disconnected from server");
    //stop server read timeout
    srvTimeoutHandler.stop();
    //stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) ChannelFuture(org.jboss.netty.channel.ChannelFuture) ClosedChannelException(java.nio.channels.ClosedChannelException) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5