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;
}
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();
}
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();
}
}
}
}
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();
}
}
}
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();
}
Aggregations