pom.xml
<!-- 配置ActiveMQ启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
创建消息队列
//创建队列 @Bean public Queue queue(){ return new ActiveMQQueue("yhood"); }
发送消息
@Controller @RequestMapping("queue") public class MessageQueueController { //消息发送者 @Autowired private JmsTemplate jmsTemplate; //消息队列(目的地) @Autowired private Destination destination; @RequestMapping("/send/{message}") @ResponseBody public String send(@PathVariable(value = "message")String message){ jmsTemplate.convertAndSend(destination,message); return message; } }
接收消息
@Component public class Consumer { @JmsListener(destination = "yhood") public void reader(String text){ System.out.println("1号接收消息:"+text); } }
原文地址:https://www.cnblogs.com/yhood/p/11536049.html
时间: 2024-11-10 09:38:28