Demo: Spring + Apache Camel + Active MQ

I created this project as an illustration to my colleagues that are new to java and J2EE. The project is hosted on github.

This demo project illustrates how to use ActiveMQ in multiple-modular Spring project with shared code. One demo project is greeter-web-app or just greeter, application, that sends greeting message to the listener or listener-web-app.

The most interesting things about this code:


  • Illustration on how to send custom java bean over the JMS by using binary serialization to the StreamMessage.
  • Spring JMS module usage. See GreeterServiceImpl
  • Apache Camel usage. Consuming messages from the ActiveMQ queue and putting them to the custom bean. See ListenerRouteBuilder


  • Loading properties.
  • Using mixed configuration: both annotation-driven and xml-driven configs are used in listener-web-app.
  • Spring MVC web applications.

It is fairly simple to configure JMS:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""

    <context:property-placeholder location="classpath:/"/>

    <bean id="greeterConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL">

    <bean id="greeterJmsOperations" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="greeterConnectionFactory"/>
        <property name="defaultDestinationName" value="${greeter.channelName}"/>

and use it then in the java code:

    @Resource(name = "greeterJmsOperations")
    private JmsOperations jmsOperations;

    public void send(final Greeting greeting) {
        jmsOperations.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                final StreamMessage message = session.createStreamMessage();
                return message;

As for listener part I used Apache Camel, its configuration is fairly simple:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""

    <!--JMS broker, sample for CLUSTER URL: 'failover:(tcp://,tcp://'-->
    <amq:connectionFactory id="listenerAmqConnectionFactory" brokerURL="tcp://"/>

    <!-- JMS channel: alexshabanov.cameldemo.greeter -->

    <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="connectionFactory" ref="listenerAmqConnectionFactory"/>

    <bean id="listenerRouteBuilder" class="com.alexshabanov.cameldemo.listener.route.ListenerRouteBuilder"/>

    <!-- Camel context -->
    <camelContext id="listenerCamelContext" xmlns="">
        <routeBuilder ref="listenerRouteBuilder"/>

    <!-- Camel handlers -->
    <bean id="greetingHandler" class="com.alexshabanov.cameldemo.listener.route.handler.GreetingHandler"/>

the most interesting parts in this config is a route builder, it’s really simple in this sample:

public final class ListenerRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {

and JMS handler (greeting handler) that receives JMS stream message:

public final class GreetingHandler implements Processor {
    private final Logger log = LoggerFactory.getLogger(GreetingHandler.class);

    private GreetingSinkService greetingSinkService;

    public void process(Exchange exchange) throws Exception {"Got exchange id={}", exchange.getExchangeId());
        final StreamMessage message = exchange.getIn().getBody(StreamMessage.class);
        final Greeting greeting = new Greeting(message.readString(), message.readInt());"- Parsed {}", greeting);

Leave a Reply

Your email address will not be published. Required fields are marked *