What is the difference between “seda + concurrentConsumers” and “direct + threads”

旧巷老猫 提交于 2019-12-04 23:57:03

问题


Apache Camel provide two solutions for using thread pool:

from("seda:stageName?concurrentConsumers=5").process(...)

and

from("direct:stageName").thread(5).process(...)

I would like to know, what is the difference between the two solutions ? Is it just two kind of write the same thing or not ? What are the use cases ?


回答1:


SEDA Component

The seda: component provides asynchronous SEDA behavior so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread to the producer.

Direct Component

The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes or if a client in the same JVM as the router wants to access the routes.

Difference between thread pools and concurrent consumers

The thread pool is a pool that dynamically can increase/shrink at runtime depending on load, the concurrent consumers is always fixed.

Like, in your case,

For Concurrent consumers - from("seda:stageName?concurrentConsumers=5").process(...)

For Thread Pool - from("direct:stageName").thread(5).process(...)

WHAT TO USE

Now,if you always want to have 5 threads available then use Concurrent Consumers and if you want the threads to be available as per the load(but not more than 5) then use Thread Pool.




回答2:


Concurrent consumers By default, the SEDA endpoint uses a single consumer thread, but you can configure it to use concurrent consumer threads. So instead of thread pools you can use:

from("seda:stageName?concurrentConsumers=5").process(...) As for the difference between the two, note a thread pool can increase/shrink dynamically at runtime depending on load, whereas the number of concurrent consumers is always fixed.

Thread pools Be aware that adding a thread pool to a SEDA endpoint by doing something like:

from("seda:stageName").thread(5).process(...)

Can wind up with two BlockQueues: one from the SEDA endpoint, and one from the workqueue of the thread pool, which may not be what you want. Instead, you might wish to configure a Direct endpoint with a thread pool, which can process messages both synchronously and asynchronously. For example:

from("direct:stageName").thread(5).process(...)

You can also directly configure number of threads that process messages on a SEDA endpoint using the concurrentConsumers option.



来源:https://stackoverflow.com/questions/19511630/what-is-the-difference-between-seda-concurrentconsumers-and-direct-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!