问题
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