List of usage examples for io.netty.channel ChannelPromise setFailure
@Override ChannelPromise setFailure(Throwable cause);
From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandler.java
License:Apache License
@Override public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) { assert msg instanceof Netty4HttpResponse : "Invalid message type: " + msg.getClass(); Netty4HttpResponse response = (Netty4HttpResponse) msg; boolean success = false; try {//from ww w .ja v a 2 s. co m List<Tuple<Netty4HttpResponse, ChannelPromise>> readyResponses = aggregator.write(response, promise); for (Tuple<Netty4HttpResponse, ChannelPromise> readyResponse : readyResponses) { ctx.write(readyResponse.v1(), readyResponse.v2()); } success = true; } catch (IllegalStateException e) { ctx.channel().close(); } finally { if (success == false) { promise.setFailure(new ClosedChannelException()); } } }
From source file:org.elasticsearch.http.nio.NettyAdaptor.java
License:Apache License
NettyAdaptor(ChannelHandler... handlers) { nettyChannel = new EmbeddedChannel(); nettyChannel.pipeline().addLast("write_captor", new ChannelOutboundHandlerAdapter() { @Override/*from w ww . j a va2 s . com*/ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { // This is a little tricky. The embedded channel will complete the promise once it writes the message // to its outbound buffer. We do not want to complete the promise until the message is sent. So we // intercept the promise and pass a different promise back to the rest of the pipeline. try { ByteBuf message = (ByteBuf) msg; promise.addListener((f) -> message.release()); NettyListener listener = NettyListener.fromChannelPromise(promise); flushOperations.add(new FlushOperation(message.nioBuffers(), listener)); } catch (Exception e) { promise.setFailure(e); } } }); nettyChannel.pipeline().addLast(handlers); }
From source file:org.elasticsearch.http.nio.NioHttpPipeliningHandler.java
License:Apache License
@Override public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) { assert msg instanceof NioHttpResponse : "Invalid message type: " + msg.getClass(); NioHttpResponse response = (NioHttpResponse) msg; boolean success = false; try {/*from w w w . j av a2 s . c o m*/ NettyListener listener = NettyListener.fromChannelPromise(promise); List<Tuple<NioHttpResponse, NettyListener>> readyResponses = aggregator.write(response, listener); success = true; for (Tuple<NioHttpResponse, NettyListener> responseToWrite : readyResponses) { ctx.write(responseToWrite.v1(), responseToWrite.v2()); } } catch (IllegalStateException e) { ctx.channel().close(); } finally { if (success == false) { promise.setFailure(new ClosedChannelException()); } } }
From source file:org.fusesource.hawtdispatch.netty.HawtEventLoop.java
License:Apache License
@Override public ChannelFuture register(final Channel channel, final ChannelPromise promise) { if (isShutdown()) { channel.unsafe().closeForcibly(); promise.setFailure(new EventLoopException("cannot register a channel to a shut down loop")); return promise; }/*from w ww . j av a2 s . c o m*/ if (inEventLoop()) { channel.unsafe().register(this, promise); } else { execute(new Runnable() { @Override public void run() { channel.unsafe().register(HawtEventLoop.this, promise); } }); } return promise; }
From source file:org.fusesource.hawtdispatch.netty.HawtSocketChannel.java
License:Apache License
@Override public ChannelFuture shutdownOutput(final ChannelPromise promise) { EventLoop loop = eventLoop();// ww w . j ava2 s .c o m if (loop.inEventLoop()) { boolean success = false; try { javaChannel().socket().shutdownOutput(); success = true; promise.setSuccess(); } catch (Throwable t) { promise.setFailure(t); } finally { if (success) { outputShutdown = true; } } } else { loop.execute(new Runnable() { @Override public void run() { shutdownOutput(promise); } }); } return promise; }
From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClientHandshaker.java
License:Apache License
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) { channel.writeAndFlush(Unpooled.wrappedBuffer(version.getBytes())).addListener((ChannelFuture future) -> { if (!future.isSuccess()) { promise.setFailure(future.cause()); return; }// www .j ava 2 s. co m ChannelPipeline p = future.channel().pipeline(); ChannelHandlerContext ctx = p.context(ProtocolHandshakeHandler.class); p.addBefore(ctx.name(), "rfb-handshake-decoder", newRfbClientDecoder()); p.addBefore(ctx.name(), "rfb-handshake-encoder", newRfbClientEncoder()); promise.setSuccess(); }); return promise; }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter.java
License:Open Source License
public synchronized void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) { // TODO check for isClosed, isClosing might be performed by mina SSH internally and is not required here // If we are closed/closing, set immediate fail if (asyncIn == null || asyncIn.isClosed() || asyncIn.isClosing()) { promise.setFailure(new IllegalStateException("Channel closed")); } else {//from ww w . j a v a 2 s. co m final ByteBuf byteBufMsg = (ByteBuf) msg; if (pending.isEmpty() == false) { queueRequest(ctx, byteBufMsg, promise); return; } writeWithPendingDetection(ctx, promise, byteBufMsg, false); } }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter.java
License:Open Source License
private void writeWithPendingDetection(final ChannelHandlerContext ctx, final ChannelPromise promise, final ByteBuf byteBufMsg, boolean wasPending) { try {/* ww w.j a v a 2s . c o m*/ if (LOG.isTraceEnabled()) { LOG.trace("Writing request on channel: {}, message: {}", ctx.channel(), byteBufToString(byteBufMsg)); } asyncIn.write(toBuffer(byteBufMsg)).addListener(new SshFutureListener<IoWriteFuture>() { @Override public void operationComplete(final IoWriteFuture future) { if (LOG.isTraceEnabled()) { LOG.trace( "Ssh write request finished on channel: {} with result: {}: and ex:{}, message: {}", ctx.channel(), future.isWritten(), future.getException(), byteBufToString(byteBufMsg)); } // Notify success or failure if (future.isWritten()) { promise.setSuccess(); } else { LOG.warn("Ssh write request failed on channel: {} for message: {}", ctx.channel(), byteBufToString(byteBufMsg), future.getException()); promise.setFailure(future.getException()); } // Not needed anymore, release byteBufMsg.release(); // Check pending queue and schedule next // At this time we are guaranteed that we are not in pending state anymore so the next request should succeed writePendingIfAny(); } }); //rescheduling message from queue after successfully sent if (wasPending) { byteBufMsg.resetReaderIndex(); pending.remove(); } } catch (final WritePendingException e) { if (wasPending == false) { queueRequest(ctx, byteBufMsg, promise); } } }
From source file:org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter.java
License:Open Source License
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) { if (asyncIn == null) { promise.setFailure(new IllegalStateException("Channel closed")); return;/*from ww w. j av a 2 s . c o m*/ } // synchronized block due to deadlock that happens on ssh window resize // writes and pending writes would lock the underlyinch channel session // window resize write would try to write the message on an already locked channelSession // while the pending write was in progress from the write callback synchronized (asyncIn) { // TODO check for isClosed, isClosing might be performed by mina SSH internally and is not required here // If we are closed/closing, set immediate fail if (asyncIn.isClosed() || asyncIn.isClosing()) { promise.setFailure(new IllegalStateException("Channel closed")); } else { final ByteBuf byteBufMsg = (ByteBuf) msg; if (pending.isEmpty() == false) { queueRequest(ctx, byteBufMsg, promise); return; } writeWithPendingDetection(ctx, promise, byteBufMsg, false); } } }
From source file:org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerWriter.java
License:Open Source License
private void writeWithPendingDetection(final ChannelHandlerContext ctx, final ChannelPromise promise, final ByteBuf byteBufMsg, final boolean wasPending) { try {//from w w w.ja v a2 s. c o m if (LOG.isTraceEnabled()) { LOG.trace("Writing request on channel: {}, message: {}", ctx.channel(), byteBufToString(byteBufMsg)); } asyncIn.write(toBuffer(byteBufMsg)).addListener(new SshFutureListener<IoWriteFuture>() { @Override public void operationComplete(final IoWriteFuture future) { // synchronized block due to deadlock that happens on ssh window resize // writes and pending writes would lock the underlyinch channel session // window resize write would try to write the message on an already locked channelSession, // while the pending write was in progress from the write callback synchronized (asyncIn) { if (LOG.isTraceEnabled()) { LOG.trace( "Ssh write request finished on channel: {} with result: {}: and ex:{}, message: {}", ctx.channel(), future.isWritten(), future.getException(), byteBufToString(byteBufMsg)); } // Notify success or failure if (future.isWritten()) { promise.setSuccess(); } else { LOG.warn("Ssh write request failed on channel: {} for message: {}", ctx.channel(), byteBufToString(byteBufMsg), future.getException()); promise.setFailure(future.getException()); } // Not needed anymore, release byteBufMsg.release(); //rescheduling message from queue after successfully sent if (wasPending) { byteBufMsg.resetReaderIndex(); pending.remove(); } } // Check pending queue and schedule next // At this time we are guaranteed that we are not in pending state anymore so the next request should succeed writePendingIfAny(); } }); } catch (final WritePendingException e) { if (wasPending == false) { queueRequest(ctx, byteBufMsg, promise); } } }