Sikorski
Sikorski

Reputation: 2691

Back to Basics : Apache Camel Routes and Direct Component

I am bit confused about Camel routes and its two endpoints : Direct and Seda. Well let's say i have a route like this :

public void configure()
{
 from("direct:services")
  .process(//Some processing here)
  .to("http://ThirdPartyServers")
}

On top of this I have a rest web service which receives several requests, does some processing and then hands over the message to this route to get response from some third party servers. I have instantiated Camel Context through Spring framework like this :

<camelContext id="appCamelContext" xmlns="http://camel.apache.org/schema/spring"
        trace="true" streamCache="true">
        <propertyPlaceholder id="properties"
            location="classpath:camel.properties" />
        <camel:routeBuilder ref="oneRouteBuilder" />
        <camel:routeBuilder ref="photosRouteBuilder" />
</camelContext>

Now the question is that in a instant I send multiple different messages to this route. Now Camel documentation says that direct component is invoked in single thread and is synchronous. So will all the messages be processed concurrently or one by one processing will happen ?

Also if i change the direct component to seda, will it make any difference ?

TIA

Update [after Petter's answer]: Although Petter's answer has clarified but i have new doubt regarding the same Direct and Seda components. Lets say my route is now like this :

public void configure(){
from("direct:services")
 .choice()
 .when("some predicate here-Predicate1")
 .to("seda:predicate1")
 .otherwise()
 .to("seda:fallback")
 .end();

 from("seda:predicate1")
 .process("some processing")
 .to("http://ThirdPartyServers");

 from("seda:fallback")
 .process("some processing")
 .to("jms:fallbackqueue");
}

Now if i send 5 messages to direct component from different threads, so these messages would be processed concurrently. As you can see in the above route, direct component sends the message to seda component. So now will there be only one thread of seda component which will process all the different 5 messages? Meaning in the end all the messages will be processed one by one ?

Upvotes: 4

Views: 7329

Answers (1)

Petter Nordlander
Petter Nordlander

Reputation: 22279

The direct component runs in the thread of the caller. Simplified, it's as a regular java method invocation. Multiple messages can run through the route as long as multiple threads are calling the direct endpoint.

When invoking the SEDA (or VM) endpoint, you message is put on a queue (in memory). Another thread (in the route) picks messages from the queue one by one and processes them. You can configure how many threads the seda consumer should have by setting the concurrentConsumers option. By default, the one thread consuming messages makes sure only one message at a time is processed no matter how many threads are producing to this route.

It boils down to your statement

in a instant I send multiple different messages to this route

If this means different threads, or just from one single thread in a sequence,

Upvotes: 6

Related Questions