Integrating systems in a complex enterprise landscape can get tough. You have all kinds of interactions going from one system to the other. Many of them taking in and spitting out different data formats. Which means you have to not only worry about the routing between these integrations but also the transformations between them. Updated to use Camel 2.11., Spring 3.2.2 and ActiveMQ 5.7.0.
Over the years it has become clear that integrations come in various flavors and each has a well defined vocabulary and some best practice design. Gregor Hohpe & Bobby Woolf authored a good book on Enterprise Integration Patterns (EIP’s). You can also check out Gregor Hohpe’s website for a list of those patterns. One look at the number of EIP and you quickly realize that these patterns are quite prevalent. Yes its possible for all this to be done in plain old Java and you can build your own custom frameworks. But why do it when you can use Apache Camel instead.
Apache Camel describes itself as “a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration”. Camel is a mediation and routing engine that lets you implement EIP’s. It uses a simple URI notation to identify the different transport and messaging models. The best thing I love about Camel is that it can be embedded into your current java application to help you implement integrations. On a larger scale Camel is part of Apache ServiceMix ESB.
Lets take Camel for a drive. Here is a simple use case:
- Incoming customer order in XML message on a JMS Topic (we will use ActiveMQ for our example).
- Based on the message content we need to route the message to a one of two folders. One folder stores orders for preferred customers and the other for regular.
In this simple example we need to route calls between a messaging system to a file system. Not only that, we need to implement content-based routing of the message to specific folders. This routing information can be recorded using a Domain Specific Language (DSL) in Java, Groovy, Scala or using Spring XML. For this example I will use the Java DSL
The DSL is defined in the class MessageQueueToFileFolderRoute.java. Here is the main code:
We build our Camel route by extending from the Camel RouterBuilder class. The RouteBuilder provides us an easy to use builder pattern API to define the routes. Refer to the configure method for the routes.
- from: Incoming message topic is ‘customer’.
- choice: Once we receive a message we have a choice to make.
- when: apply a content-based check to see if the customer is of type = P (for preferred)
- to: If the customer is a preferred customer move the message to the ‘preferred’ folder.
- otherwise: move to the regular folder.
To start ActiveMQ run…
apache-activemq-5.7.0/bin/ 2: ./activemq start
You can then get to the ActiveMQ web console at http://localhost:8161/admin. Go to the topic tab and create a topic named ‘customer’.
Next run the class RunSampler.java to execute the camel project. If you are in Eclipse just run it from there. The project will run and wait indefinitely for inputs to the topic (defined on the from dsl). Go into ActiveMQ admin and post the sample XML message to the ActiveMQ topic using the admin page for topics.
12345678 <customer><id>1234567890</id><name>John Doe</name><streetAddress>asasas</streetAddress><city>Reston</city><state>VA</state><type>P</type></customer>
If the router works correctly then Camel would have read the message from the topic, then figured out that this was a preferred customer and send it to the appropriate folder. Verify if the folder configured for preferred customer has the file with the content. Try posting the message as a regular customer type and you will see it go to the ‘regular’ folder.
While I chose to use the Java DSL you have other options such as XML, Scala, etc to write your routes in.
You can download the source from GitHub – https://github.com/thomasma/apache-camel-sample.