Camel ActiveMQ route and unit test example

Here’s a simple example to get started with Apache Camel and ActiveMq. The following Camel route will log all messages it receives in the EVENTS queue:

import org.apache.camel.builder.RouteBuilder;
public class DispatcherRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("activemq:queue:EVENTS").to("log:Events?showAll=true");
    }
}

The following junit test will start a message broker, start camel and then send one message to the queue:

import org.apache.activemq.broker.BrokerService;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.BeforeClass;
import org.junit.Test;

public class DispatcherRouteBuilderTest extends CamelTestSupport {
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new DispatcherRouteBuilder();
    }
    @BeforeClass
    public static void setUpClass() throws Exception {
        BrokerService brokerSvc = new BrokerService();
        brokerSvc.setBrokerName("TestBroker");
        brokerSvc.addConnector("tcp://localhost:61616");
        brokerSvc.start();
    }
    @Test
    public void testConfigure() throws Exception {
        template.sendBody("activemq:queue:EVENTS", "HelloWorld!");
        Thread.sleep(3000);
    }
}

You need the following dependencies if you’re using Maven:

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.4.0-fuse-00-00</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test</artifactId>
      <version>2.4.0-fuse-00-00</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
      <version>2.4.0-fuse-00-00</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-camel</artifactId>
      <version>5.4.0-fuse-00-00</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-core</artifactId>
      <version>5.4.0-fuse-00-00</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
blog comments powered by Disqus