【问题标题】:how to bootstrap netty channel for "slave" application如何为“从”应用程序引导 netty 通道
【发布时间】:2014-09-24 22:15:08
【问题描述】:

我想实现一个 netty “从属”应用程序,它监听 outgoing websocket 连接上的请求。因此,在引导程序中,应用程序将:

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

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

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

关于如何在不复制一堆现有 *Bootstrap 代码的情况下引导频道的任何建议?

【问题讨论】:

  • 我想我不明白你想做什么以及为什么 Bootstrap 在这里不起作用。
  • 我想创建一个(长期存在的)websocket 客户端,它可以根据服务器的响应消息同时执行阻塞操作。

标签: netty


【解决方案1】:

我最终通过 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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 2019-03-22
    • 2015-04-07
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多