Searching...
Monday, 12 August 2013

Apache Camel + Spring +Activemq Example

21:02
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.
 

Goals

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 http://activemq.apache.org/download-archives.html

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
 
 
<dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <version>5.10.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
 
 

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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

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

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;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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

Console

Message Sending started

Message sent


image

Download example

https://github.com/kkvinodkumaran/camel

Reference

0 comments: