netty中future.channel().closeFuture().sync()作用

看到的非常通俗的讲解,记录一下

 原文链接:netty中future.channel().closeFuture().sync()作用_大米饭-优快云博客

future.channel().closeFuture().sync() 执行完 会使主线程 wait 阻塞后续逻辑执行


我们看下官方的入门例子:

启动一个nettyserver,监听8088端口,再通过命令行执行 telnet localhost 8088来连接。
完整的例子请参考之前的入门例子介绍:
Netty入门官方例子解析(一)丢弃服务器《入门例子,不返回消息》章节

public class DiscardServer {       
        try {                       
            f = b.bind(port).sync();
            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            f.channel().close();
        }
    }   
}


在这里面future.channel().closeFuture().sync();这个语句的主要目的是,如果缺失上述代码,则main方法所在的线程,即主线程会在执行完bind().sync()方法后,会进入finally 代码块,之前的启动的nettyserver也会随之关闭掉,整个程序都结束了。原文的例子有英文注释:
Wait until the server socket is closed,In this example, this does not happen, but you can do that to gracefully shut down your server.
让线程进入wait状态,也就是main线程暂时不会执行到finally里面,nettyserver也持续运行,如果监听到关闭事件,可以优雅的关闭通道和nettyserver,虽然这个例子中,永远不会监听到关闭事件。也就是说这个例子是仅仅为了展示存在api shutdownGracefully,可以优雅的关闭nettyserver。

下面我们来验证下:

public class DiscardServer {
       
        try {
                       
            f = b.bind(port).sync();
          //  f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            f.channel().close();
        }
    }   
}


我们注掉了future.channel().closeFuture().sync(),这时,再执行telnet命令的时候会提示连接失败


如果我们不想加f.channel().closeFuture().sync()又想保证程序正常运行怎么办,简单,去掉finally 里面关闭nettyserver的语句即可。下面我们来改造下: 

 try {
                       
            f = b.bind(port).sync();
          //  f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            //bossGroup.shutdownGracefully();
            //workerGroup.shutdownGracefully();
            //f.channel().close();
        }


是不是一目了然了?其实入门例子可以完全按照我的改造的方法来演示,去掉f.channel().closeFuture().sync()。否则,对于刚入门的小白来说,无法理解这个例子中的奥义。

 

Exception in thread "Thread-23" java.net.BindException: Address already in use: bind at java.base/sun.nio.ch.Net.bind0(Native Method) at java.base/sun.nio.ch.Net.bind(Net.java:461) at java.base/sun.nio.ch.Net.bind(Net.java:453) at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227) at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:141) at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:562) at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334) at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:600) at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:579) at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973) at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:260) at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:380) at io.netty.util.concurrent.AbstractEventExecutor.runTask$$$capture(AbstractEventExecutor.java:173) at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:166) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:834)
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值