| | |
| | | /** |
| | | * 启动服务 |
| | | */ |
| | | @Async |
| | | @Async("alarm") |
| | | public void init(int port) { |
| | | |
| | | //表示服务器连接监听线程组,专门接受 accept 新的客户端client 连接 |
| | | EventLoopGroup bossLoopGroup = new NioEventLoopGroup(); |
| | | try { |
| | |
| | | serverBootstrap = serverBootstrap.handler(new NettyUdpHandler()); |
| | | //6、绑定server,通过调用sync()方法异步阻塞,直到绑定成功 |
| | | ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); |
| | | log.info("started and listened on " + channelFuture.channel().localAddress()); |
| | | log.debug("started and listened on " + channelFuture.channel().localAddress()); |
| | | //7、监听通道关闭事件,应用程序会一直等待,直到channel关闭 |
| | | channelFuture.channel().closeFuture().sync(); |
| | | } catch (Exception e) { |
| | | log.error("error:"+e.getMessage()); |
| | | log.error("error:" + e.getMessage()); |
| | | } finally { |
| | | log.info("netty udp close!"); |
| | | log.debug("netty udp close!"); |
| | | //8 关闭EventLoopGroup, |
| | | bossLoopGroup.shutdownGracefully(); |
| | | } |