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
-
- Click on “Browse” button to view messages
Create Spring AMQP Configuration for ActiveMQ
1 2 3 |
<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)
1 2 3 |
<amq:queue id="destination" physicalName="jms/TPActiveMQQueue" /> |
- Declare Producer Connection Factory by using Spring JMS API’s SingleConnectionFactory
1 2 3 4 5 |
<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
1 2 3 4 5 |
<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
1 2 3 4 5 6 |
<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
1 2 3 4 5 |
<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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context" xmlns:jms="https://www.springframework.org/schema/jms" xmlns:amq="https://activemq.apache.org/schema/core" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd https://www.springframework.org/schema/jms https://www.springframework.org/schema/jms/spring-jms.xsd https://activemq.apache.org/schema/core https://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.