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

Compare with Current View Page History

« Previous Version 7 Next »

POJO Messaging Example

Introduction

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.

Requirements

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

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

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

  • No labels