Netty的Future-Listener机制

前言

Netty的IO操作是异步的,如何判断当前操作是否成功还是失败呢,这就需要配置监听器了。

在服务端绑定端口时,绑定操作是异步操作,会立即返回ChannelFuture ,可通过future来获取操作状态。

ChannelFuture常用操作:

  1. isDone:判断操作是否执行完成
  2. isSuccess:判断操作是否执行成功
  3. isCancelled:判断操作是否被取消
  4. cause:获取操作失败的原因

案例1:监听绑定端口

    public static void main(String[] args) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        Map<Integer, SocketChannel> map = new ConcurrentHashMap<>();
        try {
            //服务端创建的是ServerBootstrap 
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup) 
                    .channel(NioServerSocketChannel.class)  
                    .childHandler(new ChannelInitializer<SocketChannel>() { 
                        @Override
                        protected void initChannel(io.netty.channel.socket.SocketChannel socketChannel) throws Exception {
                            map.put(socketChannel.hashCode(), socketChannel);
                            socketChannel.pipeline().addLast(new ServerHandler(map));
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture future = bootstrap.bind(9527).sync().addListener(future1 -> {
                if (future1.isSuccess()) {
                    System.out.println("端口绑定成功");
                } else {
                    System.out.println("端口绑定失败");
                }
                if (future1.isDone()) {
                    System.out.println("端口绑定任务执行完成");
                } else {
                    System.out.println("端口绑定任务执行失败");
                }

            });
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

案例1:监听发送消息

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ChannelFuture future = ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hello 客户端", CharsetUtil.UTF_8)).addListener(future1 -> {
            if (future1.isSuccess()) {
                System.out.println("消息发送成功");
            } else {
                System.out.println("消息发送失败");
            }
            if (future1.isDone()) {
                System.out.println("消息发送任务执行完成");
            } else {
                System.out.println("消息发送任务执行失败");
            }
        } );
    }

事件完成后,可根据监听器回调结果进行后续业务处理,比如修改推送消息的状态,是否成功推送?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值