Monday, 12 August 2013

Apache Camel + Spring +Activemq Example

In the previous example (Apache Camel + Activemq) we created Camel Router class manually and started Camel to route messages from one queue to another queue. In this example you will see how to configure Camel Routing in Spring configuration xml file.


1. Start Activemq message broker

2. Setup Camel+Spring project

3. Create Spring configuration file

4. Main program to execute Camel

1. Setup Activemq 

1. Download activemq from

2. Extract activemq zip file and start ActiveMQ (Click on activemq.bat file for windows)
3. Check activemq console using http://localhost:8161/admin  (Use default credentials admin/admin to login)

2. Setup Camel + Spring Project

Create a maven project and add below dependencies

3. Create Spring configuration file (camel-application-context.xml)

In this file we are using the message routing using spring DSL and creating the beans for camel producer template and kms components.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:camel=""

    <camelContext xmlns="">
            <from uri="jms:queue:testQSource" />
            <to uri="jms:queue:testQDestination" />
    <camel:camelContext id="camel-client">
        <camel:template id="camelTemplate" />
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="brokerURL" value="tcp://localhost:61616" />

4. Main program to send message

Here we will load the above defined spring configuration and use the Camel producer template to sending the messages, once the message is reached in the queue , the above routes which is defined in the xml file will start consuming and processing .

package com.vinod.test;
import org.apache.camel.ProducerTemplate;
import org.springframework.context.ApplicationContext;
public class TestCamelSpring {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("camel-application-context.xml");
        ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
        System.out.println("Message Sending started");
        camelTemplate.sendBody("jms:queue:testQSource","Sample Message");
        System.out.println("Message sent");


5. Output

Run the program and see below output in the console and in the Activemq we can see new queues are created and routed the messages


Message Sending started

Message sent


Download example