Spring JMS XML Configuration
May 03, 2024
This page will walk through Spring JMS XML configuration example. We will configure connection factory, message converter, container factory and JmsTemplate
using XML configuration. To enable @JmsListener
annotation, we configure <jms:annotation-driven/>
element.
On this page we will create a complete example for Spring JMS integration using XML configuration.
Contents
Connection Factory
In our example we are using ActiveMQ messaging server. For the connection factory, create the bean forActiveMQConnectionFactory
. To optimize the connection, Spring JMS provides two implementation of
javax.jms.ConnectionFactory
SingleConnectionFactory
and CachingConnectionFactory
. In our example we are using CachingConnectionFactory
.
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="amqConnectionFactory"/> </bean>
Message Converter
Message converter is needed to convert message between domain model objects and JMS message type.<bean id="jacksonJmsMessageConverter" class="org.springframework.jms.support.converter.MappingJackson2MessageConverter"> <property name="typeIdPropertyName" value="_type"/> </bean>
Container Factory
Find the XML configuration for message listener container.<bean id="jmsContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageConverter" ref="jacksonJmsMessageConverter"/> <property name="pubSubDomain" value="true"/> </bean>
DefaultJmsListenerContainerFactory
JmsTemplate
SpringJmsTemplate
is the helper class to send and receive JMS messages.
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageConverter" ref="jacksonJmsMessageConverter"/> </bean>
Enabling @JmsListener
Spring@JmsListener
annotation marks a method to be a message listener. To enable @JmsListener
using XML configuration, add <jms:annotation-driven/>
element as following.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jms https://www.springframework.org/schema/jms/spring-jms.xsd"> <jms:annotation-driven/> ------ </beans>
@JmsListener
annotation.
@JmsListener(destination = "spring", containerFactory = "jmsContainerFactory") public void receiveNews(News news) { ------ }
Complete Example
pom.xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies>
<?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:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jms https://www.springframework.org/schema/jms/spring-jms.xsd"> <jms:annotation-driven/> <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="amqConnectionFactory"/> </bean> <bean id="jacksonJmsMessageConverter" class="org.springframework.jms.support.converter.MappingJackson2MessageConverter"> <property name="typeIdPropertyName" value="_type"/> </bean> <bean id="jmsContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageConverter" ref="jacksonJmsMessageConverter"/> <property name="pubSubDomain" value="true"/> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="messageConverter" ref="jacksonJmsMessageConverter"/> </bean> <bean id="newsReceiver1" class="com.concretepage.receiver.NewsReceiver1"/> <bean id="newsReceiver2" class="com.concretepage.receiver.NewsReceiver2"/> </beans>
package com.concretepage.receiver; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import com.concretepage.News; @Component public class NewsReceiver1 { @JmsListener(destination = "spring", containerFactory = "jmsContainerFactory") public void receiveNews(News news) { System.out.println("Receiver1: " + news); } }
package com.concretepage.receiver; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import com.concretepage.News; @Component public class NewsReceiver2 { @JmsListener(destination = "spring", containerFactory = "jmsContainerFactory") public void receiveMessage(News news) { System.out.println("Receiver2: " + news); } }
package com.concretepage; public class News { private int id; private String title; public News() {} public News(int id, String title) { this.id = id; this.title = title; } //Sets and Gets @Override public String toString() { return id + ", " + title; } }
package com.concretepage; import javax.jms.JMSException; import javax.jms.Topic; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; public class Application { public static void main(String[] args) throws JMSException { AbstractApplicationContext context = new ClassPathXmlApplicationContext("jms-config.xml"); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); Topic springTopic = jmsTemplate.getConnectionFactory().createConnection() .createSession().createTopic("spring"); News news = new News(100, "Latest news on Spring"); jmsTemplate.convertAndSend(springTopic, news); context.registerShutdownHook(); context.close(); } }