Spring AMQP ActiveMQ Tutorial (Part 1)

Filed Under: JMS

Welcome to Spring AMQP ActiveMQ Tutorial. Earlier we looked into installing Apache ActiveMQ server. Today we will create a spring application to work with ActiveMQ JMS queues.

Spring AMQP ActiveMQ Tutorial

The Spring AMQP ActiveMQ Tutorial is divided into following sections.

  • Introduction
  • Spring AMQP Tag Libraries
  • Steps to Develop and Test Spring AMQP with ActiveMQ
  • Benefits and Drawbacks of Spring AMQP with ActiveMQ
  • Create JMS Queues in ActiveMQ Server
  • Create Spring AMQP Configuration for ActiveMQ

Introduction

We have already discussed some “Spring AMQP Basics” Theoretically and “How to install and setup ActiveMQ Server” in my previous posts. Please refer them in the following:

In this post and my next coming post, we are going to develop a Spring AMQP ActiveMQ Messaging application using Queues that is One-to-One Messaging Application. Let us start it now.

Initially I thought of delivering one post for this whole example. However, after preparing whole steps I feel that its too lengthy post. So its divided into two posts. In this post, we are going to discuss some Apache ActiveMQ Basics, then how to configure our application queues then do some Spring Framework AMQP and ActiveMQ API XML Configuration. In my next post, we will develop actual Java programs and test it.

Please click here “Spring AMQP ActiveMQ Messaging Example” to access part-2 post.

Spring AMQP Tag Libraries

Spring Framework has provided two kinds of AMQP tag library to work with the following AMQP Servers:

  • RabbitMQ AMQP Tag Library
  • Spring RabbitMQ AMQP Tag Library is used to develop Messaging applications for RabbitMQ Server using Spring AMQP API and Spring RabbitMQ API.

    Spring RabbitMQ AMQP Tag Library is defined in “spring-rabbit.xsd” file.

  • ActiveMQ AMQP Tag Library
  • Spring ActiveMQ AMQP Tag Library is used to develop Messaging applications for Apache ActiveMQ Server using Spring AMQP API and Apache ActiveMQ API.

    Spring RabbitMQ AMQP Tag Library is defined in “activemq-core.xsd” file.

Steps to Develop and Test Spring AMQP with ActiveMQ

In this section, I will just list out what are all the steps we need to do to develop and test a Spring AMQP Messaging application with Apache ActiveMQ Server. We will discuss discuss these steps in depth with example in coming sections and coming post.

Please follow these steps to develop and test Spring AMQP Messaging application with Apache ActiveMQ Server.

  • Install Apache ActiveMQ Server (refer ActiveMQ section)
  • Create a Mavenized Java project in Eclipse
  • Create Spring AMQP Configuration for ActiveMQ
  • Create JMS Queues in ActiveMQ Server
  • Develop Spring AMQP Messaging Application With ActiveMQ
  • Test Spring AMQP Messaging Application With ActiveMQ

These are the brief steps to develop and test Spring AMQP Messaging application with Apache ActiveMQ Server. If you don’t understand them well, no worries. We will discuss these steps one by one in coming sections.

Benefits and Drawbacks of Spring AMQP with ActiveMQ

In this section, we will discuss: Compare to “Spring AMQP With RabbitMQ Server” combination, what are the benefits and drawbacks of “Spring AMQP With ActiveMQ Server” combination

“Spring AMQP With ActiveMQ” combination have the following benefits:

  • Apache ActiveMQ Server supports XA Transactions very well.
  • We can easily embedded Apache ActiveMQ MQ Broker into Java applications.
  • It is very easy to integrate “Spring AMQP With ActiveMQ Application” with Apache Camel.

Even though “Spring AMQP With ActiveMQ” combination have many benefits, it has one major drawback:

  • ActiveMQ needs more memory to maintain the application.

Create JMS Queues in ActiveMQ Server

  • Create JMS Queue by using new ActiveMQ Admin console
  • Click on “+Create” button, provide details and click on “Create Queue” button
  • Spring AMQP ActiveMQ Tutorial

  • Click on “Browse” button to view messages
  • spring activemq tutorial

Create Spring AMQP Configuration for ActiveMQ

  • Configure Spring AMQP XML Configuration for ActiveMQ Server
  • Configure JMS Connection Factory by providing ActiveMQ Provider URL

    
    <amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />
    

    Here URL contains protocol, host name and port number.By default, this application needs tcp protocol. Host name is localhost. We are using default port number 61616.

  • Configure destination (Here we are using queue)
  • 
    <amq:queue id="destination" physicalName="jms/TPActiveMQQueue" />
    
  • Declare Producer Connection Factory by using Spring JMS API’s SingleConnectionFactory
  • 
    <bean id="jmsProducerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    	<property name="targetConnectionFactory" ref="jmsFactory" />
    </bean>
    
  • Declare Consumer Connection Factory by using Spring JMS API’s SingleConnectionFactory
  • 
    <bean id="jmsConsumerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    	<property name="targetConnectionFactory" ref="jmsFactory" />
    </bean>
    

    NOTE:-
    Both Producer and Consumer should refer to the previously created JMS Factory object as shown in the above Spring AMQP XML Configuration.

  • Configure Spring JmsTemplate for JMS Producer to send messages to Destination
  • 
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    	<property name="connectionFactory" ref="producerJmsConnectionFactory" />
    	<property name="defaultDestination" ref="destination" />
    </bean>
    
  • Configure JMS Listener to consume messages
  • 
    <jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto">
    <jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" />
    </jms:listener-container>
    
  • Complete Spring Configuration file:
  • 
    <?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:context="http://www.springframework.org/schema/context"
           xmlns:jms="http://www.springframework.org/schema/jms"
           xmlns:amq="http://activemq.apache.org/schema/core"
           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://www.springframework.org/schema/jms 
           http://www.springframework.org/schema/jms/spring-jms.xsd
           http://activemq.apache.org/schema/core 
           http://activemq.apache.org/schema/core/activemq-core.xsd">
    
    <context:component-scan base-package="com.tp.jms.activemq" />
    <amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />
    <amq:queue id="destination" physicalName="jms/TPActiveMQQueue" />
    
    <bean id="producerJmsConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    	<property name="targetConnectionFactory" ref="jmsFactory" />
    </bean>
    
    <bean id="consumerJmsConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    	<property name="targetConnectionFactory" ref="jmsFactory" />
    </bean>
    
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    	<property name="connectionFactory" ref="producerJmsConnectionFactory" />
    	<property name="defaultDestination" ref="destination" />
    </bean>
    
    <jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto">
    <jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" />
    </jms:listener-container>
    
    <bean id="counter" class="java.util.concurrent.atomic.AtomicInteger"/>
    </beans>
    

That’s it all about Spring XML Configuration to develop a Spring AMQP ActiveMQ Messaging Example. Please head over to Spring ActiveMQ Example for the second part of this tutorial.

Comments

  1. Mitali Jena says:

    Hi,

    I have doubt regarding how the Spring XML Configuration has been initialized.
    I mean we have write something like below:

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(“spring.xml”);

    Please clarify this to me.

  2. jit says:

    Thanks for a nice article. Request you to provide a link , when you say my previous article. Thanks a lot

  3. vivek says:

    Hi ,

    i want to configure eventbusterminal with activemq like this : @Bean
    EventBusTerminal terminal() {
    SpringAMQPTerminal terminal = new SpringAMQPTerminal();
    terminal.setConnectionFactory(connectionFactory);
    terminal.setExchangeName(exchangeName);
    terminal.setDurable(true);
    terminal.setTransactional(true);
    terminal.setSerializer(axonJsonSerializer());
    //terminal.setSerializer(xmlSerializer());
    terminal.setListenerContainerLifecycleManager(listenerContainerLifecycleManager());
    return terminal;
    }

    but in axon connectionFcatory is tightly binded to rabbitmq bit i want to use activemq : SO how can i use and connect to eventbus

  4. Gauty says:

    Hi, Thanks for such a nice tutorial. One small request, Can you provide the link for source code developed for this whole JMS series. It would be very helpful for hands on.

    1. Rambabu says:

      Hi, thanks your comments. Yes, will do it and update the posts ASAP.

      Ram

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages