一、引入外部依赖 https://mvnrepository.com/
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.4.4</version> </dependency>
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.16.1</version> </dependency>
二、代码
spring.activemq.broker-url=tcp://192.168.1.xxx:61616 spring.activemq.in-memory=true spring.activemq.pool.enabled=false spring.activemq.user=admin spring.activemq.password=admin
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ActiveMQController { @Autowired private JmsTemplate jmsTemplate; @RequestMapping("/activeMQ") @ResponseBody public String tests(){ jmsTemplate.convertAndSend("testname","testMsg"); return "ok"; } }
package com.example.activemq; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class Consumer { @JmsListener(destination = "testName") public void receiveMsg(String text){ System.out.println(text+"....."); } }