如何为"slave"应用程序引导网络通道



我想实现一个netty "slave"应用程序,监听传出的 websocket连接上的请求。那么在bootstrap中,应用程序将:

  1. 打开到远程服务器的ws连接
  2. 使用"boss"事件循环监听ws连接上的命令
  3. 在"worker"事件循环中处理传入命令

我的问题是如何引导通道—为了方便起见,我宁愿使用一个引导对象(我的意思是与手动配置通道相比),但是:

  1. ServerBootstrap需要一个本地监听套接字
  2. Bootstrap只有一个事件循环
  3. AbstractBootstrap不能在包外子类化,ServerBootstrap/Bootstrap是最终的

关于如何引导通道而不复制一堆现有的*引导代码的任何建议?

我最终通过ChannelPipeline.html.addLast()将EventExecutorGroup直接传递给管道-我不知怎么在api文档中错过了这一点。

来自javadocs:

static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);
...
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new MyProtocolDecoder());
pipeline.addLast("encoder", new MyProtocolEncoder());
// Tell the pipeline to run MyBusinessLogicHandler's event handler methods
// in a different thread than an I/O thread so that the I/O thread is not blocked by
// a time-consuming task.
// If your business logic is fully asynchronous or finished very quickly, you don't
// need to specify a group.
pipeline.addLast(group, "handler", new MyBusinessLogicHandler());

最新更新