@Override public ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) { delegate.addListener(listener); return this; }
@Override public ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) { delegate.addListener(listener); return this; }
@Override public ChannelPromise unvoid() { ChannelPromise promise = new DefaultChannelPromise(channel); if (fireExceptionListener != null) { promise.addListener(fireExceptionListener); } return promise; }
@Override public ChannelPromise unvoid() { ChannelPromise promise = new DefaultChannelPromise(channel); if (fireExceptionListener != null) { promise.addListener(fireExceptionListener); } return promise; }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (LOG.isTraceEnabled()) { promise.addListener((future) -> { LOG.trace("write {}", future.isSuccess() ? "complete" : "failed"); }); } super.write(ctx, msg, promise); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { promise.addListener(Listener.INSTANCE); super.write(ctx, msg, promise); }
@Override public NetSocketInternal writeMessage(Object message, Handler<AsyncResult<Void>> handler) { ChannelPromise promise = chctx.newPromise(); super.writeToChannel(message, promise); promise.addListener((future) -> { if (future.isSuccess()) { handler.handle(Future.succeededFuture()); } else { handler.handle(Future.failedFuture(future.cause())); } } ); return this; }
/** * Remove the first {@link ByteBuf} from the queue. * @param aggregatePromise used to aggregate the promises and listeners for the returned buffer. * @return the first {@link ByteBuf} from the queue. */ public final ByteBuf removeFirst(ChannelPromise aggregatePromise) { Object entry = bufAndListenerPairs.poll(); if (entry == null) { return null; } assert entry instanceof ByteBuf; ByteBuf result = (ByteBuf) entry; decrementReadableBytes(result.readableBytes()); entry = bufAndListenerPairs.peek(); if (entry instanceof ChannelFutureListener) { aggregatePromise.addListener((ChannelFutureListener) entry); bufAndListenerPairs.poll(); } return result; }
@Override public void run() { if (localAddress == null) { channel.connect(remoteAddress, connectPromise); } else { channel.connect(remoteAddress, localAddress, connectPromise); } connectPromise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE); } });
@Override public void run() { if (localAddress == null) { channel.connect(remoteAddress, connectPromise); } else { channel.connect(remoteAddress, localAddress, connectPromise); } connectPromise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE); } });
void writeData(Http2Stream stream, ByteBuf chunk, boolean end, Handler<AsyncResult<Void>> handler) { EventExecutor executor = chctx.executor(); ChannelPromise promise = chctx.newPromise(); if (handler != null) { promise.addListener((future) -> { if(future.isSuccess()) { handler.handle(Future.succeededFuture()); } else { handler.handle(Future.failedFuture(future.cause())); } }); } if (executor.inEventLoop()) { _writeData(stream, chunk, end, promise); } else { executor.execute(() -> { _writeData(stream, chunk, end, promise); }); } }
private void closeConnAfterWrite() { ChannelPromise channelFuture = conn.channelFuture(); conn.writeToChannel(Unpooled.EMPTY_BUFFER, channelFuture); channelFuture.addListener(fut -> conn.close()); }
private void scheduleTimeout(final ChannelHandlerContext ctx, final ChannelPromise promise) { // Schedule a timeout. final WriteTimeoutTask task = new WriteTimeoutTask(ctx, promise); task.scheduledFuture = ctx.executor().schedule(task, timeoutNanos, TimeUnit.NANOSECONDS); if (!task.scheduledFuture.isDone()) { addWriteTimeoutTask(task); // Cancel the scheduled timeout if the flush promise is complete. promise.addListener(task); } }
private void sendData(Channel ch) { QueueCommandHolder command = queue.peek(); if (command != null && command.trySend()) { QueueCommand data = command.getCommand(); List<CommandData<Object, Object>> pubSubOps = data.getPubSubOperations(); if (!pubSubOps.isEmpty()) { for (CommandData<Object, Object> cd : pubSubOps) { for (Object channel : cd.getParams()) { ch.pipeline().get(CommandPubSubDecoder.class).addPubSubCommand((ChannelName) channel, cd); } } } else { ch.attr(CURRENT_COMMAND).set(data); } command.getChannelPromise().addListener(listener); ch.writeAndFlush(data, command.getChannelPromise()); } }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); final NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); final boolean isRequest = msg instanceof Request; // We add listeners to make sure our out bound event is correct. // If our out bound event has an error (in most cases the encoder fails), // we need to have the request return directly instead of blocking the invoke process. promise.addListener(future -> { try { if (future.isSuccess()) { // if our future is success, mark the future to sent. handler.sent(channel, msg); return; } Throwable t = future.cause(); if (t != null && isRequest) { Request request = (Request) msg; Response response = buildErrorResponse(request, t); handler.received(channel, response); } } finally { NettyChannel.removeChannelIfDisconnected(ctx.channel()); } }); }
private void sendData(Channel ch) { QueueCommandHolder command = queue.peek(); if (command != null && command.trySend()) { QueueCommand data = command.getCommand(); List<CommandData<Object, Object>> pubSubOps = data.getPubSubOperations(); if (!pubSubOps.isEmpty()) { for (CommandData<Object, Object> cd : pubSubOps) { for (Object channel : cd.getParams()) { ch.pipeline().get(CommandPubSubDecoder.class).addPubSubCommand((ChannelName) channel, cd); } } } else { ch.attr(CURRENT_COMMAND).set(data); } command.getChannelPromise().addListener(listener); ch.writeAndFlush(data, command.getChannelPromise()); } }
public final ChannelFuture sendFile(RandomAccessFile raf, long offset, long length) throws IOException { // Write the content. ChannelPromise writeFuture = chctx.newPromise(); if (!supportsFileRegion()) { // Cannot use zero-copy writeToChannel(new ChunkedFile(raf, offset, length, 8192), writeFuture); } else { // No encryption - use zero-copy. sendFileRegion(raf, offset, length, writeFuture); } if (writeFuture != null) { writeFuture.addListener(fut -> raf.close()); } else { raf.close(); } return writeFuture; }
void writePushPromise(int streamId, Http2Headers headers, Handler<AsyncResult<Integer>> completionHandler) { int promisedStreamId = connection().local().incrementAndGetNextStreamId(); ChannelPromise promise = chctx.newPromise(); promise.addListener(fut -> { if (fut.isSuccess()) { completionHandler.handle(Future.succeededFuture(promisedStreamId)); } else { completionHandler.handle(Future.failedFuture(fut.cause())); } }); EventExecutor executor = chctx.executor(); if (executor.inEventLoop()) { _writePushPromise(streamId, promisedStreamId, headers, promise); } else { executor.execute(() -> { _writePushPromise(streamId, promisedStreamId, headers, promise); }); } }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); // Close the connection immediately after LastContent is written, rather than // waiting until the graceful-delay is up if this flag is set. if (isEndOfRequestResponse(msg)) { if (HttpChannelFlags.CLOSE_AFTER_RESPONSE.get(ctx)) { promise.addListener(future -> { Channel parent = parentChannel(ctx); closeChannel(ctx, parent, ConnectionCloseType.fromChannel(ctx.channel()), ctx.newPromise()); }); } } }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); // Close the connection immediately after LastContent is written, rather than // waiting until the graceful-delay is up if this flag is set. if (isEndOfRequestResponse(msg)) { if (HttpChannelFlags.CLOSE_AFTER_RESPONSE.get(ctx)) { promise.addListener(future -> { Channel parent = parentChannel(ctx); closeChannel(ctx, parent, ConnectionCloseType.fromChannel(ctx.channel()), ctx.newPromise()); }); } } }