39.10. 重新使用 Netty bos 和 worker 线程池
Netty 有两种类型的线程池:boss 和 worker。默认情况下,每个 Netty consumer 和 producer 具有其专用线程池。如果要在多个消费者或生成者间重复使用这些线程池,则必须在 Registry 中创建并加入线程池。
例如,使用 Spring XML,我们可使用带有 2 个 worker 线程的 NettyWorkerPoolBuilder
创建共享 worker 线程池,如下所示:
注意
对于 boss 线程池,有一个 org.apache.camel.component.netty.NettyServerBossPoolBuilder
构建器,以及用于 Netty producer 的 org.apache.camel.component.netty.NettyClientBossPoolBuilder
。
然后,在 Camel 路由中,您可以通过在 URI 中配置 workerPool
选项来引用此 worker 池,如下所示:
<route> <from uri="netty:tcp://0.0.0.0:5021?textline=true&sync=true&workerPool=#sharedPool&usingExecutorService=false"/> <to uri="log:result"/> ... </route>
<route>
<from uri="netty:tcp://0.0.0.0:5021?textline=true&sync=true&workerPool=#sharedPool&usingExecutorService=false"/>
<to uri="log:result"/>
...
</route>
如果我们有另一个路由,我们可以引用共享 worker 池:
<route> <from uri="netty:tcp://0.0.0.0:5022?textline=true&sync=true&workerPool=#sharedPool&usingExecutorService=false"/> <to uri="log:result"/> ... </route>
<route>
<from uri="netty:tcp://0.0.0.0:5022?textline=true&sync=true&workerPool=#sharedPool&usingExecutorService=false"/>
<to uri="log:result"/>
...
</route>
因此,