You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 19 Next »

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 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.

Synchronous

The Direct component provides synchronous invocation of any consumers when a producer sends a message exchange.

URI format

seda:someName[?options]

Where someName can be any string that uniquely identifies the endpoint within the current CamelContext.

You can append query options to the URI in the following format, ?option=value&option=value&...

Same URI must be used for both producer and consumer

An exactly identical Seda endpoint URI must be used for both the producer endpoint and the consumer endpoint. Otherwise Camel will create a second Seda endpoint, even thought the someName portion of the URI is identical. For example:

from("direct:foo").to("seda:bar?concurrentConsumers=5");

from("seda:bar?concurrentConsumers=5").to("file://output");

Notice that we have to use the full URI including options in both the producer and consumer.

Options

Name

Default

Description

size

1000

The maximum size of the SEDA queue.

concurrentConsumers

1

Camel 1.6.1/2.0: Number of concurrent threads processing exchanges.

waitForTaskToComplete

IfReplyExpected

Camel 2.0: Option to specify whether the caller should wait for the async task to complete or not before continuing. The following three options are supported: Always, Newer or IfReplyExpected. The first two values are self-explanatory. The last value, IfReplyExpected, will only wait if the message is Request Reply based. The default option is IfReplyExpected. See more information about Async messaging.

timeout

30000

Camel 2.0: Timeout in millis a seda producer will at most waiting for an async task to complete. See waitForTaskToComplete and Async for more details.

Changes in Camel 2.0

In 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 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(...)

Difference between thread pools and concurrent consumers

The thread pool is a pool that can increase/shrink dynamically at runtime depending on load, whereas the concurrent consumers are 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 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.

Sample

In 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.

Error formatting macro: snippet: java.lang.NullPointerException

Here we send a Hello World message and expects the reply to be OK.

Error formatting macro: snippet: java.lang.NullPointerException

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.

  • No labels