Home  >  Spring 5

Spring @JmsListener Example

By Arvind Rai, August 17, 2020
This page will walk through Spring @JmsListener annotation example. The @JmsListener annotation marks a method to listen as JMS message listener for the given destination. To enable @JmsListener, we need to annotate Java configuration class with @EnableJms annotation, and for XML configuration use <jms:annotation-driven/> element. Find the @JmsListener attributes.
destination: Defines the destination name for this resolver. It is required attribute.
@JmsListener(destination = "qa")
Now find the optional attributes of @JmsListener annotation.
concurrency: Defines the concurrency limit for the listener.
containerFactory: Assigns container factory bean name using this attribute. The default container factory bean name is containerFactory.
id: Assigns unique identifier of the container managing this endpoint.
selector: Defines JMS message selector expression.
subscription: Defines the name for the durable subscription.

Find the sample example with containerFactory attribute.
@JmsListener(destination = "qa", containerFactory = "myConFactory")
Now let us create the example for @JmsListener annotation.

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

Creating Listener Endpoint with @JmsListener

Find the sample code to create endpoint with @JmsListener annotation.
@JmsListener(destination = "qa")
public void receiveQaMessage(Employee employee) {
} 
JMS listener methods are allowed to have flexible signatures similar to Spring MessageMapping. The JMS listener methods can have the arguments such as @Payload, @DestinationVariable, @Header etc.

Using @JmsListener and @SendTo

The @SendTo annotation specifies a destination and indicates that method's return value should be converted to a Message and sent to the specified destination. The @SendTo annotation is used at method level with @JmsListener. We can also use @SendTo at class level that will be applicable for all listener methods annotated with @JmsListener in that class.
@JmsListener(destination = "dev")
@SendTo("javaDev")
public Employee receiveSpringMessage(Employee employee) {
	employee.setProfile("Java Developer");
	return employee;
} 

Complete Example with Spring Boot

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> 
QaMsgReceiver.java
package com.concretepage.receiver;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.concretepage.Employee;
@Component
public class QaMsgReceiver {
	@JmsListener(destination = "qa")
	public void receiveQaMessage(Employee employee) {
		System.out.println("Received: " + employee);
	}
} 
DeveloperMsgReceiver.java
package com.concretepage.receiver;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import com.concretepage.Employee;
@Component
public class DeveloperMsgReceiver {
	@JmsListener(destination = "dev")
	@SendTo("javaDev")
	public Employee receiveSpringMessage(Employee employee) {
		employee.setProfile("Java Developer");
		return employee;
	}
}
JavaDevMsgReceiver.java
package com.concretepage.receiver;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.concretepage.Employee;
@Component
public class JavaDevMsgReceiver {
	@JmsListener(destination = "javaDev")
	public void reviewJavaDevMessage(Employee employee) {
		System.out.println("Received: " + employee);
	}
} 
Employee.java
package com.concretepage;
public class Employee {
	private int id;
	private String profile;
	public Employee() {}
	public Employee(int id) {
		this.id = id;
	}
	public Employee(int id, String profile) {
		this.id = id;
		this.profile = profile;
	}	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getProfile() {
		return profile;
	}
	public void setProfile(String profile) {
		this.profile = profile;
	}
	@Override
	public String toString() {
		return id + ", " + profile;
	}
}
JMSConfig.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
@Configuration
@EnableJms
public class JMSConfig {
	@Bean
	public DefaultJmsListenerContainerFactory containerFactory() {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setSessionTransacted(true);
		factory.setMaxMessagesPerTask(1);
		factory.setConcurrency("1-5");
		return factory;
	}
	@Bean
	public MessageConverter jacksonJmsMessageConverter() {
		MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
		converter.setTargetType(MessageType.TEXT);
		converter.setTypeIdPropertyName("_type");
		return converter;
	}
} 
Application.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.core.JmsTemplate;
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

		Employee qaEngineer = new Employee(111, "QA Engineer");
		jmsTemplate.convertAndSend("qa", qaEngineer);

		Employee devloper = new Employee(222);
		jmsTemplate.convertAndSend("dev", devloper);
	}
} 
Find the print screen of the output in Eclipse console.
Spring @JmsListener Example

References

JMS (Java Message Service)
Spring @JmsListener

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us