SpringBoot-ActiveMq 布满荆棘的人生 2022-06-09 12:09 165阅读 0赞 ActiveMq下载: http://activemq.apache.org 下载解压安装,打开安装目录下的bin/win64/activemq: ![Center][] 浏览器输入 http://localhost:8161/,能进入主页面表明安装成功: ![Center 1][] maven依赖: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency> 消息定义: package com.example.demo; import org.springframework.jms.core.MessageCreator; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; public class Msg implements MessageCreator{ @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("这是测试消息"); } } 消息发送及地址定义: package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jms.core.JmsTemplate; @SpringBootApplication public class DemoApplication implements CommandLineRunner{ @Autowired //注入springboot为我们配置好的JmsTemplate的bean JmsTemplate jmsTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... strings) throws Exception { //向my-destination目的地发送MSG消息 jmsTemplate.send("my-destination",new Msg()); } } 消息监听: package com.example.demo; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class Receiver { @JmsListener(destination = "my-destination") public void receiveMsg(String msg){ System.out.println("接收到的消息:"+msg); } } 运行: ![Center 2][] 遇到的问题: 1.关闭activeMq后,再次启动Tomcat,报错: Caused by: java.net.ConnectException: Connection refused: connect 2.使用springboot的内嵌 activemq,启动Tomcat报错: <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency> Caused by: java.net.ConnectException: Connection refused: connect [Center]: /images/20220609/ab82f0d9b7884baba9f8f8af37a73ba4.png [Center 1]: /images/20220609/3eb4a5f500e54c4bb41caf327a0df7b1.png [Center 2]: /images/20220609/a95cd6e238854c63bbbd28c8a6540949.png
还没有评论,来说两句吧...