Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Embellish the tutorial with more detail, explaining at each stage what is happening.
  • Link to specific sections of Camel documentation when referring to components.
  • Attach completed example project.
  • Explain in more detail what is happening when the JMS component is being defined in camel-server.xml
  • Show how logging can be introduced to monitor exchange body contents.
  • Detail how time-outs can be configured.
  • Show how to catch lost messages.
  • Show how to use a wiretap.
  • Can we make this ActiveMQ embedded so that it does not require the reader to download and start it manually?
  • EIP Patterns used
  • Components used
  • Advanced bean bindings in routing

Prerequisites

This tutorial uses Maven to setup the Camel project and for dependencies for artifacts.

...

First we need to have dependencies for the core Camel jars and , its spring and , jms components and finally ActiveMQ as the message broker.

Wiki Markup
{snippet:id=e1|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/pom.xml}

...

And dependencies for the AOP enable server example:. These dependencies are of course only needed if you need fullblown AOP stuff using AspejctJ with bytecode instrumentation.

Wiki Markup
{snippet:id=e3|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/pom.xml}

...

This defines a Camel route from the JMS queue named numbers to the Spring bean named multiplier. Camel will create a consumer to the JMS queue which forwards all received messages onto the the Spring bean, using the method named multiply.

...

The Spring config file is placed under META-INF/spring as this is the default location used by the Camel Maven Plugin, which we will later use to run our server.
First we need to do the standard scheme declarations in the top. In the camel-server.xml we are using spring beans as the default bean: namespace and springs context:. For configuring ActiveMQ we use broker: and for Camel we of course have :camel.

Wiki Markup
{snippet:id=e1|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}

We use Spring annotations for doing IoC dependencies and its component-scan features comes to the resource rescue as it scans for spring annotations in the given package name:

...

The ActiveMQ JMS broker is also configured in this xml file. We set it up to listen on TCP port 61616.

...

As this examples uses JMS then Camel needs a JMS component that is connected with the ActiveMQ broker. This is configured as shown below:

Wiki Markup
{snippet:id=e5|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server.xml}

Notice: The JMS component is configured in standard Spring beans, but the gem is that the bean id can be reference in referenced from Camel routes so - meaning we can do routing with using the JMS Component by just using jms: prefix in the URIs and route URI. What happens is that Camel will find this bean in the Registry and use this JMS Component. If you don't like jms you can name the bean id as you like (eg activemq, myjmsbroker, messaging etc). Spring Registry for a bean with the id="jms". Since the bean id can have arbitrary name you could have named it id="jmsbroker" and then referenced to it in the routing as {{from="jmsbroker:queue:numbers).to("multiplier");
We use the vm protocol to connect to the ActiveMQ server as its embedded in this application.

...

The example has an enhanced Server example that uses fullblown Aspejct AspejctJ AOP for doing a audit tracking of invocations of the business service.

We leverage Spring AOP support in the {{camel-server-aop.xml} configuration file. First we must declare the correct XML schemas schema's to use:

Wiki Markup
{snippet:id=e1|lang=xml|url=activemq/camel/trunk/examples/camel-example-spring-jms/src/main/resources/META-INF/spring/camel-server-aop.xml}

...

And the gem is that we inject the AuditTracker aspect bean with a Camel endpoint that defines where the audit should be stored. Noticed how easy it is to setup as we have just defined an endpoint URI that is file based, meaning that we stored the audit tracks as files. We can change this tore to any Camel component components as we wish. To store it on a JMS queue simply change the URI to jms:queue:audit.

...

  • as a standard java main application - just start the org.apache.camel.spring.Main class
  • using maven jave:exec
  • using camel:run

In this sample as there are two servers (with and without AOP) we have prepared some profiles in maven to start the Server of your choice.
The server is started with:
mvn compile exec:java -PCamelServer

...

Code Block
titlepom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-maven-plugin</artifactId>
    </plugin>      
  </plugins>
</build>

All that is required is a new plugin definition in your Maven POM. As we have already placed our Camel config in the default location (camel-server.xml has been placed in META-INF/spring/) we do not need to tell the plugin where the route definitions are located. Simply run mvn camel:run.

...