我正在研究Netty应用程序.我想在不同的端口上运行多个服务器,如果没有(阻塞)closeFuture().sync(),它就无法工作.
我使用以下代码在ServerManager类中启动服务器:
gpcmServer = new GpcmServer(port); gpspServer = new GpspServer(port);
在这些类中,我按如下方式启动服务器:
public GpspServer(int port) throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup,workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { // Add the server handler and its decoder ch.pipeline().addLast(new GpspDecoder(),new GpspServerHandler()); } }) .option(ChannelOption.so_BACKLOG,128) .childOption(ChannelOption.so_KEEPALIVE,true); // Bind and start to accept incoming connections. bindFuture = bootstrap.bind(port).sync(); bindFuture.channel().closeFuture(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
但是,当我不调用closeFuture().sync()时,我无法连接到服务器.当我将.sync()添加到bindFuture.channel().closeFuture()时,我可以连接到服务器.我如何继续这样做,仍然使服务器工作?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。