Search in sources :

Example 6 with DefaultPromise

use of io.netty.util.concurrent.DefaultPromise in project openzaly by akaxincom.

the class NettyClient2 method sendRedisCommand.

public Future<IRedisCommandResponse> sendRedisCommand(final RedisCommand redisCommand) {
    final Future<IRedisCommandResponse> responseFuture;
    // channelPromise.channel().isActive());
    if (channelPromise != null) {
        final ChannelPromise readyPromise = this.channelPromise;
        final DefaultPromise<IRedisCommandResponse> responsePromise = new DefaultPromise<IRedisCommandResponse>(readyPromise.channel().eventLoop());
        // 提交一个事件
        readyPromise.channel().eventLoop().submit(new Runnable() {

            @Override
            public void run() {
                // 将这个结果赋值给responsePromise
                NettyClient2.this.responsePromise = responsePromise;
            }
        });
        readyPromise.channel().writeAndFlush(redisCommand).addListener(new GenericFutureListener<ChannelFuture>() {

            @Override
            public void operationComplete(final ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    // 如果失败了,直接将promise返回
                    responsePromise.tryFailure(future.cause());
                    logger.error("send push message error: {},cause={}", redisCommand, future.cause());
                } else {
                // logger.info("write data to platform success");
                }
            }
        });
        responseFuture = responsePromise;
    } else {
        logger.error("send push error because client is not connected: {}", redisCommand.toString());
        responseFuture = new FailedFuture<IRedisCommandResponse>(GlobalEventExecutor.INSTANCE, CONNECT_EXCEPTION);
    }
    return responseFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultPromise(io.netty.util.concurrent.DefaultPromise) ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

DefaultPromise (io.netty.util.concurrent.DefaultPromise)6 ChannelFuture (io.netty.channel.ChannelFuture)2 Before (org.junit.Before)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelPromise (io.netty.channel.ChannelPromise)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 EpollServerSocketChannel (io.netty.channel.epoll.EpollServerSocketChannel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 SslHandler (io.netty.handler.ssl.SslHandler)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1