SEDAPage edited by charles MoulliardSEDA ComponentThe seda: component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer. Note that queues are only visible within a single CamelContext. If you want to communicate across CamelContext instances (for example, communicating between Web applications), see the VM component. This component does not implement any kind of persistence or recovery, if the VM terminates while messages are yet to be processed. If you need persistence, reliability or distributed SEDA, try using either JMS or ActiveMQ.
URI formatseda:someName[?options] Where someName can be any string that uniquely identifies the endpoint within the current CamelContext.
Changes in Camel 2.0In Camel 2.0 the SEDA component supports using Request Reply, where the caller will wait for the Async route to complete. For instance: from("mina:tcp://0.0.0.0:9876?textline=true&sync=true").to("seda:input"); from("seda:input").to("bean:processInput").to("bean:createResponse"); In the route above, we have a TCP listener on port 9876 that accepts incoming requests. The request is routed to the seda:input queue. As it is a Request Reply message, we wait for the response. When the consumer on the seda:input queue is complete, it copies the response to the original message response. Camel 1.x does not have this feature implemented, the SEDA queues in Camel 1.x will newer wait. Concurrent consumersBy 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(...)
Difference between thread pools and concurrent consumersThe thread pool is a pool that can increase/shrink dynamically at runtime depending on load, whereas the concurrent consumers are always fixed. Thread poolsBe 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 want to consider configuring 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. SampleIn the route below we use the SEDA queue to send the request to this async queue to be able to send a fire-and-forget message for further processing in another thread, and return a constant reply in this thread to the original caller. public void configure() throws Exception { from("direct:start") // send it to the seda queue that is async .to("seda:next") // return a constant response .transform(constant("OK")); from("seda:next").to("mock:result"); } Here we send a Hello World message and expects the reply to be OK. Object out = template.requestBody("direct:start", "Hello World"); assertEquals("OK", out); The "Hello World" message will be consumed from the SEDA queue from another thread for further processing. Since this is from a unit test, it will be sent to a mock endpoint where we can do assertions in the unit test. Using multipleConsumersAvailable as of Camel 2.2 In this example we have defined two consumers and registered them as spring beans. <!-- define the consumers as spring beans --> <bean id="consumer1" class="org.apache.camel.spring.example.FooEventConsumer"/> <bean id="consumer2" class="org.apache.camel.spring.example.AnotherFooEventConsumer"/> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <!-- define a shared endpoint which the consumers can refer to instead of using url --> <endpoint id="foo" uri="seda:foo?multipleConsumers=true"/> </camelContext> Since we have specified multipleConsumers=true on the seda foo endpoint we can have those two consumers receive their own copy of the message as a kind of pub-sub style messaging. As the beans are part of an unit test they simply send the message to a mock endpoint, but notice how we can use @Consume to consume from the seda queue. public class FooEventConsumer { @EndpointInject(uri = "mock:result") private ProducerTemplate destination; @Consume(ref = "foo") public void doSomething(String body) { destination.sendBody("foo" + body); } } See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence
- [CONF] Apache Camel > SEDA confluence