Home  >  Spring 5

Spring JMS XML Configuration Example

By Arvind Rai, September 01, 2020
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.

Technologies Used

Find the technologies being used in our example.
1. Java 11
2. Spring 5.2.8.RELEASE
3. Spring Boot 2.3.2.RELEASE
4. Maven 3.5.2

Connection Factory

In our example we are using ActiveMQ messaging server. For the connection factory, create the bean for ActiveMQConnectionFactory. To optimize the connection, Spring JMS provides two implementation of
javax.jms.ConnectionFactory 
that are 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> 
The container factory
DefaultJmsListenerContainerFactory 
uses plane JMS client APIs. It is designed to work in a native JMS environment as well as in a Java EE environment.

JmsTemplate

Spring JmsTemplate 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> 
Find the sample code to create listener endpoint using @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> 
jms-config.xml
<?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> 
NewsReceiver1.java
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);
	}
} 
NewsReceiver2.java
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);
	}
} 
News.java
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;
	}
} 
Application.java
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();
	}
} 
Find the print screen of the output in Eclipse console.
Spring JMS XML Configuration Example

Reference

JMS (Java Message Service)

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us