0

I have configured the following route to query my local MongoDB instance. The instance is running on localhost on port 27017 without authentication.

The route is:

from("direct:start")
    .to("mongodb:mongoBean?" +
    "database=camel-source" +
    "&collection=RacingEvents" +
    "&operation=getDbStats")        
    .convertBodyTo(String.class)
    .to("file://E:/data/test.txt");

My mongoBean is defined in spring as:

<bean id="mongoBean" class="com.mongodb.Mongo">
        <constructor-arg name="host" value="localhost" />
        <constructor-arg name="port" value="27017" />
</bean>

The route starts up fine but no data is sent to the file endpoint.

If I replace the direct: component endpoint with a timer: component data is written to the file endpoint:

from("timer://foo?delay=1&repeatCount=1")
        .to("mongodb:mongoBean?" +
        "database=camel-source" +
        "&collection=RacingEvents" +
        "&operation=getDbStats")        
        .convertBodyTo(String.class)
        .to("file://E:/data/test.txt");

The question is why does the direct component not initiate the call to MongoDB but the timer component does.

Maciej
  • 2,175
  • 1
  • 18
  • 29

1 Answers1

2

The direct component only routes if you send a message to it, its like a direct method invocation in Java, eg when you call a method on a java instance. The timer on the other hand runs indpendently, and triggeres a new empty message every X period.

See more details at

And a bit in this FAQ

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thank you for the quick response. To send a message to the endpoint at route start I could then use the method you outlined in http://stackoverflow.com/a/7727197/600775 using a ProducerTemplate, correct? – Maciej Jul 17 '15 at 14:21
  • Yeah a ProducerTemplate allows to send a message to any Camel endpoint from Java code (to produce a message to an endpoint). Then the consumer on that endpoint would be able to route it (though a few endpoints dont accept producers such as a timer) – Claus Ibsen Jul 17 '15 at 14:46
  • Works perfectly. Thank you for your help. – Maciej Jul 17 '15 at 16:53