Using Camel direct Component

In Camel the direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. it provides a simple mechanism for linking together routes just like in programming code you would call functions.

For example consider the following route:

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
  public void configure() {
    from("file:data1?noop=true").to("direct:merge");
    from("file:data2?noop=true").to("direct:merge");
    from("direct:merge").to("file:out");
  }
});
context.start();
Thread.sleep(10000);
context.stop();

Here we are sending the content of the folders data1 and data2 to the direct endpoint named merge. The direct endpoint combines the files and sends them to the folder named “out”.

Important: The event model for the direct: component is synchronous. This means that subsequent segments of the route run in the same thread as the first segment.

The direct component is often used to encapsulate some functionalities, much like you would do with functions in programming. For example, supposing you want to create a function-like for sending messages:

from("direct:sendJMS")     .to("jms:queue:mydestination");

Now you can call this function from another piece of code like this:

CamelContext context = new DefaultCamelContext();
context.addRoutes(new JavaRouteBuilder());
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:sendJMS", "Camel Rocks");

Much the same way, you can use the direct component also to do the opposite, that is to receive body:

from("jms:queue:mydestination")     .to("direct:receiveJMS");

In this case, the ProducerTemplate will act as a Consumer from the Queue:

CamelContext context = new DefaultCamelContext();
context.addRoutes(new JavaRouteBuilder());
context.start();
ProducerTemplate producerTemplate = context.createConsumerTemplate();
String str = producerTemplate.receiveBody("direct:receiveJMS", String.class);

Using the SEDA component

The SEDA component is an alternative mechanism for linking together routes. You can use it much the same way as the direct component however consider the following rules:

  • Processing of a SEDA endpoint is asynchronous. Hence, when you send an exchange to a SEDA producer endpoint, control immediately returns to the preceding processor in the route.
  • SEDA endpoints contain a queue buffer which stores all of the incoming exchanges prior to processing by the next route segment.
  • Each SEDA consumer endpoint creates a thread pool (the default size is 5) to process exchange objects from the blocking queue.

Thanks to this mechanism, using the SEDA endpoint routes can be more responsive, using the built-in consumer thread pool. The above example can be re-written to use SEDA endpoints instead of direct endpoints, as follows:

CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { 	public void configure() {  		from("file:data1?noop=true").to("seda:merge"); 		from("file:data2?noop=true").to("seda:merge");  		from("seda:merge").to("file:out");   	} }); context.start(); Thread.sleep(10000); context.stop();

The main difference between this example and the direct example is that when using SEDA, the second route segment is processed by a pool of threads.