JMS / Spring boot / Wildfly
Spring 4+JMS+ActiveMQ example with @JmsListener & @EnableJms
How connect to WildFly 10.1.0.Final ActiveMQ Artemis using JNDI and Spring?
Configure a Spring JMS application with Spring Boot and annotation support
Code Example
Wildfly Queue
java:/jms/queue/QueueTest
MessageProducerConfiguration.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
@Component
public class MessageProducerConfiguration {
private final JmsTemplate jmsTemplate;
@Autowired
public MessageProducerConfiguration(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void send(String message) {
jmsTemplate.convertAndSend("QueueTest", message);
}
}
MessageReceiver.java
import javax.jms.JMSException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
static final Logger LOG = LoggerFactory.getLogger(MessageReceiver.class);
@JmsListener(destination = "QueueTest")
public void receiveMessage(final String message) throws JMSException {
LOG.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
LOG.info("Application : response received : {}", message);
LOG.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
}
Sending Message
@Autowired
private MessageProducerConfiguration messagingProducerConfiguration;
@RequestMapping("/")
public String home(){
messagingProducerConfiguration.send("123");
return "Welcome come Spring Boot with Wildfly";
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.jms.test</groupId>
<artifactId>JmsTestSpringBoot</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>JmsTestSpringBoot</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>jmstestspringboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
webapp/WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/jmstestspringboot</context-root>
</jboss-web>