Example usage for io.netty.channel ChannelPromise addListener

List of usage examples for io.netty.channel ChannelPromise addListener

Introduction

In this page you can find the example usage for io.netty.channel ChannelPromise addListener.

Prototype

@Override
    ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener);

Source Link

Usage

From source file:org.vertx.java.core.impl.FlowControlHandler.java

License:Apache License

@Override
public void flush(final ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    boolean writable = state.isWritable();
    if (writable) {
        writable = !outboundBuf.isReadable(highMark);
    }/*  w w w  .  ja  v a 2s.c o m*/
    if (!writable) {
        if (state.isWritable()) {
            state.updateWritable(false);
            ctx.fireUserEventTriggered(state);
        }
        promise.addListener(listener);

    }
    ctx.flush(promise);
}

From source file:reactor.ipc.netty.http.server.HttpServerHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    // modify message on way out to add headers if needed
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        trackResponse(response);/*  www.  ja  v a 2s .c om*/
        // Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
        if (!isKeepAlive(response) || !isSelfDefinedMessageLength(response)) {
            // No longer keep alive as the client can't tell when the message is done unless we close connection
            pendingResponses = 0;
            persistentConnection = false;
        }
        // Server might think it can keep connection alive, but we should fix response header if we know better
        if (!shouldKeepAlive()) {
            setKeepAlive(response, false);
        }
    }
    if (msg instanceof LastHttpContent) {
        if (!shouldKeepAlive()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(
                        "Detected non persistent http " + "connection," + " " + "preparing to close",
                        pendingResponses);
            }
            promise.addListener(ChannelFutureListener.CLOSE);
        } else if (mustRecycleEncoder) {
            mustRecycleEncoder = false;
            pendingResponses -= 1;
        }
    }
    ctx.write(msg, promise);
}