【发布时间】:2020-01-16 09:39:07
【问题描述】:
我开发了一个 Spring Boot 应用程序,它使用 JMS 在 Activemq 中发送和监听消息,但是在运行应用程序时,JMS 没有被 Spring Boot 启动
这是主类,Application.java
@SpringBootApplication
@EnableJms
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
配置类:Config.java
@Configuration
public class Config {
@Bean
public Queue queue() {
return new ActiveMQQueue("inmemory.queue");
}
@Bean
public JmsTemplate jmsTemplate() {
return new JmsTemplate(activeMQConnectionFactory());
}
}
Listener类用于监听队列:Listener.java
@Component
public class Listener {
@JmsListener(destination = "inmemory.queue")
public void listener(String message) {
System.out.println("message received" + message);
}
}
Producer 类用于从控制器向队列发送消息:Producer.java
@RestController
public class Producer {
@Autowired
Queue queue;
@Autowired
JmsTemplate jmstemplate;
@RequestMapping(method = RequestMethod.GET, path = "/test3/{message}")
public String test3(@PathVariable String message) {
jmstemplate.convertAndSend(queue, message);
return "teste3" + message;
}
}
application.properties
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
server.port=8081
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
This is a screenshot of application starting without starting JMS
【问题讨论】:
-
您好,显示的错误是什么?你能补充一下你的问题吗?
-
没有错误,我是spring boot的新手。我正在使用eclipse。问题是程序正在运行,但 JMS 没有启动
-
请提供您的 pom.xml
-
@YogeshPrajapati pom.xml 添加..请帮助
-
@JonathanJohx .. 请看看
标签: spring-boot jms activemq