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:com.bloom.zerofs.rest.HealthCheckHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!restServerState.isServiceUp()) {
        if (msg instanceof LastHttpContent) {
            // Start closing client channels after we've completed writing to them (even if they are keep-alive)
            logger.info("Health check request handler closing connection " + ctx.channel()
                    + " since in shutdown mode.");
            promise.addListener(ChannelFutureListener.CLOSE);
            nettyMetrics.healthCheckHandlerChannelCloseOnWriteCount.inc();
        }/*from  w  ww . j a  v  a  2  s.  co m*/
    }
    super.write(ctx, msg, promise);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryAuthHandler.java

License:Open Source License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {
    originalPromise = promise;//w  w  w . j  av a 2s .  co m
    ChannelPromise downPromise = ctx.newPromise();
    downPromise.addListener(new GenericFutureListener<Future<Void>>() {
        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess() && !originalPromise.isDone()) {
                originalPromise.setFailure(future.cause());
            }
        }
    });
    ctx.connect(remoteAddress, localAddress, downPromise);
}

From source file:com.ebay.jetstream.http.netty.server.KeepAliveHandler.java

License:MIT License

private void closeConnection(Object msg, ChannelPromise promise) {
    if (msg instanceof HttpResponse) {
        HttpHeaders.setHeader((HttpResponse) msg, HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    }//w  w  w . ja  v a2  s  .co m
    promise.addListener(ChannelFutureListener.CLOSE);
}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

/**
 * @param ctx/*w  ww  .jav a  2 s. com*/
 * @param queue
 */
public void flush(ChannelHandlerContext ctx, AutoFlushWriterChannelQueue queue) {

    if (queue == null)
        return;

    MessageEvent[] events = queue.get();

    if (events == null)
        return;

    ByteBuf[] msgs = new ByteBuf[events.length];

    for (int j = 0; j < events.length; j++) {
        msgs[j] = (ByteBuf) events[j].getMsg();
    }

    try {
        ByteBuf composite = Unpooled.wrappedBuffer(msgs);

        ChannelPromise promise = new ExtendedChannelPromise(ctx.channel());

        promise.addListener(new AutoFlushWriterChannelListener(events));

        queue.setLastFlushTime(System.currentTimeMillis());

        super.write(ctx, composite, promise);
        super.flush(ctx);

    } catch (Throwable t) {
        LOGGER.error("Error while Flushing : " + Arrays.toString(t.getStackTrace()));
    }

}

From source file:com.github.milenkovicm.kafka.handler.MetadataHandler.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    final ControlTuple tuple = (ControlTuple) msg;
    final int currentCorrelation = correlation++;
    ByteBuf kafkaMessage = createMetadataRequest(ctx.alloc(), tuple.topicName, currentCorrelation);

    // not sure if it is possible that tread may read response message before it
    // puts future in the map. that's why I have it here .
    acks.put(currentCorrelation, tuple.promise);
    promise.addListener(new GenericFutureListener<ChannelPromise>() {
        @Override/*from   w  ww  .  j a va2 s.  c o m*/
        public void operationComplete(ChannelPromise future) throws Exception {
            // shouldn't be possible to cancel this operation
            //                if (future.isCancelled()) {
            //                    tuple.promise.cancel(true);
            //                    return;
            //                }
            if (future.isDone() && !future.isSuccess()) {
                acks.remove(currentCorrelation);
                tuple.promise.setFailure(future.cause());
            }
        }
    });
    super.write(ctx, kafkaMessage, promise);
}

From source file:com.linecorp.armeria.common.RequestContextTest.java

License:Apache License

@Test
@SuppressWarnings("deprecation")
public void makeContextAwareChannelFutureListener() {
    RequestContext context = createContext();
    ChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    promise.addListener(context.makeContextAware((ChannelFutureListener) f -> {
        assertEquals(context, RequestContext.current());
        assertTrue(entered.get());/*from  ww  w.  jav  a  2 s  .  co  m*/
    }));
    promise.setSuccess(null);
}

From source file:com.mobius.software.android.iotbroker.mqtt.net.ExceptionHandler.java

License:Open Source License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) {
    ctx.connect(remoteAddress, localAddress, promise.addListener(new ChannelFutureListener() {
        @Override//w w  w . j a v  a2s.  c o  m
        public void operationComplete(ChannelFuture future) {
            if (!future.isSuccess())
                Log.d("", "an error occured while connect");
        }
    }));
}

From source file:com.mobius.software.android.iotbroker.mqtt.net.ExceptionHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    ctx.write(msg, promise.addListener(new ChannelFutureListener() {
        @Override//  www. j  a  va 2  s .  c o  m
        public void operationComplete(ChannelFuture future) {
            if (!future.isSuccess()) {
                Log.d("", "an error occured while write");

                if (listener != null)
                    listener.writeError();
            }
        }
    }));
}

From source file:com.mobius.software.mqtt.performance.controller.net.ExceptionHandler.java

License:Open Source License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) {
    ctx.connect(remoteAddress, localAddress, promise.addListener(new ChannelFutureListener() {
        @Override//from w w  w . java  2  s.c  o  m
        public void operationComplete(ChannelFuture future) {
            if (!future.isSuccess())
                exceptionCaught(ctx, future.cause());
        }
    }));
}

From source file:com.mobius.software.mqtt.performance.controller.net.ExceptionHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    ctx.write(msg, promise.addListener(new ChannelFutureListener() {
        @Override//from   ww  w  .j a v  a  2s  .c  o m
        public void operationComplete(ChannelFuture future) {
            if (!future.isSuccess())
                exceptionCaught(ctx, future.cause());
        }
    }));
}