[CONF] Apache Camel: POJO Messaging Example (page edited)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[CONF] Apache Camel: POJO Messaging Example (page edited)

Dhiraj Bokde (Confluence)

POJO Messaging Example has been edited by Jonathan Anstey (Jan 28, 2009).

(View changes)


POJO Messaging Example


This example shows that you don't need to learn Camel's super cool DSLs if you don't want to. Camel has a set of annotations that allow you to produce, consume or route messages to endpoints.


This requires Camel 2.0, the camel-jms component and Apache ActiveMQ. Of course, since we are using Maven these dependencies will be downloaded automatically.

Running the example

The README.txt states how to run the example from either Ant or Maven.

Here's how to run it with Maven:

mvn compile camel:run

Whats happening?

When you start the example up you'll see a whole bunch of logs that won't really mean anything to you The interesting stuff is happening in the background. Here's a diagram of whats going on.

At step 1 the SendFileRecordsToQueueBean polls the ./src/data directory for new files. There are 3 files in this directory so 3 messages will be created. As shown below, the @Consume annotation will cause any new messages coming from the file:src/data endpoint to be sent to the onFileSendToQueue method.

public class SendFileRecordsToQueueBean {
    ProducerTemplate producer;

    @Consume(uri = "file:src/data?noop=true")
    public void onFileSendToQueue(String body) {

At step 2 the SendFileRecordsToQueueBean then sends the contents of the File message as a String to the personnel.records JMS queue, which is backed by an embedded instance of Apache ActiveMQ. The conversion from String to JMS message is automatic. The @EndpointInject annotation is used to access the ActiveMQ endpoint.

At step 3 the DistributeRecordsBean (shown below) consumes the JMS message from the personnel.records queue. Again the @Consume annotation is used to get messages from the ActiveMQ endpoint.

public class DistributeRecordsBean {
    @Consume(uri = "activemq:personnel.records")
    public String[] route(@XPath("/person/city/text()") String city) {
        if (city.equals("London")) {
            return new String[] {"file:target/messages/emea/hr_pickup", 
        } else {
            return new String[] {"file:target/messages/amer/hr_pickup", 

You will notice an additional @RecipientList annotation on the route method. This turns the method into a Recipient List EIP where the return value is a list of URIs for the recipients. This annotation is great for creating custom dynamic Recipient Lists. In this case at step 4 we peek at the city field in the message (using the @XPath annotation) and provide a set of recipients based on that. For folk from London, their files will be sent to file locations for the EMEA region (file:target/messages/emea/...). Others fall into the AMER region (file:target/messages/amer/...).

After running the example, browse to the target/messages directory to see where the messages were saved.

See also