This tutorial will show you how we can run Apache ActiveMQ with Spring in the embedded mode using point-to-point messaging domain. For more information on point-to-point messaging system
please read tutorial https://www.jeejava.com/configure-jms-client-using-glassfish-3/
For running ActiveMQ in embedded mode we do not need to run the ActiveMQ server manually by executing command in command prompt.
Now we will look into the following steps in order to implement point-to-point messaging system
using Spring and embedded ActiveMQ.
Prerequisites
Eclipse Kepler
Maven 3.x
JDK 1.8
Spring 4.x.x
Apache ActiveMQ 5.11.1
1. Create a class called MessageProducer
that will produce message or send message to the destination – Queue.
package com.roytuts.spring.jms.producer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; public class MessageProducer { @Autowired private JmsTemplate jmsTemplate; public void sendMessageToDefaultDestination(final String message) { jmsTemplate.convertAndSend(message); } }
2. Create a class called MessageDefaultConsumer
that will receive message from the destination – Queue.
package com.roytuts.spring.jms.consumer; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class MessageDefaultConsumer implements MessageListener { @Override public void onMessage(Message message) { if (message instanceof TextMessage) { try { String msg = ((TextMessage) message).getText(); System.out.println("Message has been consumed : " + msg); } catch (JMSException ex) { throw new RuntimeException(ex); } } else { throw new IllegalArgumentException( "Message must be of type TextMessage"); } } }
3. Create an XML property file called apache-activemq-properties.xml
that will hold all key/value pairs for the application. Put this file under src/main/resources
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jmsProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" name="jmsProperties"> <property name="order" value="99999" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="properties"> <value> <!-- JMS --> JMS.BROKER.URL=tcp://localhost:61616 JMS.QUEUE.NAME=IN_QUEUE </value> </property> </bean> </beans>
4. Create an XML configuration file called apache-activemq-jms.xml
that will hold all JMS related configuration for the application. Put this file under src/main/resources/jms
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ActiveMQ connection factory --> <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg index="0" value="${JMS.BROKER.URL}" /> </bean> <!-- ConnectionFactory Definition --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="amqConnectionFactory" /> </bean> <!-- Destination Queue --> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="${JMS.QUEUE.NAME}" /> </bean> <!-- JmsTemplate Definition --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="destinationQueue" /> </bean> <!-- Message Producer --> <bean id="messageProducer" class="com.roytuts.spring.jms.producer.MessageProducer" /> <!-- Message Consumer from Default Destination --> <bean id="messageDefaultConsumer" class="com.roytuts.spring.jms.consumer.MessageDefaultConsumer" /> <!-- Message Consumer Container for Default Destination --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destinationName" value="${JMS.QUEUE.NAME}" /> <property name="messageListener" ref="messageDefaultConsumer" /> </bean> </beans>
5. Create an XML file called apache-activemq-context.xml
that will load all other resources and configure support for annotation in the application. Put this file under src/main/resources/spring
<?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" 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"> <context:annotation-config></context:annotation-config> <import resource="classpath:apache-activemq-properties.xml" /> <import resource="classpath:jms/apache-activemq-jms.xml" /> </beans>
6. Create a test class under src/test/java
that will run the MessageProducer
.
import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class EmbeddedMessageProducerTest { private MessageProducer messageProducer; @SuppressWarnings("resource") @Before public void setUp() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[] { "classpath:jms/embedded-activemq-jms-spring.xml", "classpath:spring/apache-activemq-context.xml" }); messageProducer = (MessageProducer) applicationContext .getBean("messageProducer"); } @Test public void testSendMessageToDefaultDestination() { messageProducer .sendMessageToDefaultDestination("Send this message to default destination."); } }
7. Create an XML file called embedded-activemq-jms-spring.xml
and put it under src/main/resources/jms
which will run ActiveMQ server in embedded mode.
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- Embedded ActiveMQ Broker --> <amq:broker id="broker" useJmx="false" persistent="false"> <amq:transportConnectors> <amq:transportConnector uri="tcp://localhost:61616" /> </amq:transportConnectors> </amq:broker> </beans>
8. Run the EmbeddedMessageProducerTest
.
Output
Message has been consumed : Send this message to default destination.
Note: In embedded mode you would not be able to open web console for the destination because the ActiveMQ server running in embedded mode and once the work is done then JVM terminates, so the ActiveMQ server.
Here is the pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.roytuts</groupId> <artifactId>apache-activemq</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>apache-activemq</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.8</jdk.version> <junit.version>4.11</junit.version> <activemq.version>5.11.1</activemq.version> <spring.version>4.1.5.RELEASE</spring.version> </properties> <dependencies> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>${activemq.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <!-- xbean --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.7</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project>
That’s all. Thank you for reading.
Tags: ActiveMQ JMS • ActiveMQ Spring • Embedded ActiveMQ • Spring JMS
Having trouble getting this set up correctly … can you be specific/explicit regarding names and locations for each of the four xml files shown?
Please check I have updated.