使用springboot整合ActiveMQ
2024-10-08 06:46:16
结构图
第一步:导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
第二步:创建application.yml配置文件
//消费者的yml
server:
port: 8081
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
//在使用发布订阅的时候的开
#jms:
#pub-sub-domain: true //生产者的yml
server:
port: 8080
#activemq配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
第三步: 创建生产者,通过JMSTemplate模板发送消息
//队列 package com.wish.producer.producermethod;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component
public class QueueProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage() {
//点对点,创建队列
ActiveMQQueue queue = new ActiveMQQueue("boot_queue");
//发送消息
jmsTemplate.convertAndSend(queue, "生产者产生的消息~");
jmsTemplate.setDeliveryMode(2);
jmsTemplate.setExplicitQosEnabled(true);
jmsTemplate.setDeliveryPersistent(true);
}
} //发布订阅
package com.wish.producer.producermethod; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource; @Component
public class TopicProducer {
//注入JMSTemplate模板
@Resource
private JmsTemplate jmsTemplate;
//创建方法
public void sendMessage(){
//发布订阅:创建主题
ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
//springboot默认是queue
jmsTemplate.setPubSubDomain(true);
//发送消息
jmsTemplate.convertAndSend(topic,"生产者产生主题的消息~"); }
}
第四步:创建客户端访问的方法
//队列
package com.wish.producer.controller; import com.wish.producer.producermethod.QueueProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class QueueController {
@Resource
private QueueProducer queueProducer; @RequestMapping("/sendQueueMessage")
public String sendMessage(){
queueProducer.sendMessage();
return "QueueSuccess";
}
} //发布订阅
package com.wish.producer.controller; import com.wish.producer.producermethod.TopicProducer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
@RestController
public class TopicController {
@Resource
private TopicProducer topicProducer; @RequestMapping("/sendTopicMessage")
public String sendMessage(){
topicProducer.sendMessage();
return "TopicSuccess";
}
}
第五步:创建消费者
//队列
@JmsListener(destination = "boot_queue")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
} //发布订阅
@Bean
public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
//这里必须设置为true,false则表示是queue类型
factory.setPubSubDomain(true);
return factory;
} //消费者消费 destination队列或者主题的名字
@JmsListener(destination = "boot_topic",containerFactory = "jmsTopicListenerContainerFactory")
public void getMessage(TextMessage message) throws JMSException {
System.out.println("消费者获取到消息:"+message.getText());
}
最新文章
- Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]
- 【JSP】JSP基础学习记录(一)—— 基础介绍以及3个编译指令
- CSU 1328: 近似回文词
- 1.JS设计模式-this,call&;apply
- Spring IoC原理详解
- 62. Divide Two Integers
- 3D全景!这么牛!!
- EF Code First教程-02 约定配置
- treap 1286郁闷的出纳员.cpp
- 同一台电脑启动两个或多个tomcat
- Server.MapPath() 解析
- Keil C51里关于堆栈指针的处理
- Lucene全文搜索 分组,精确查找,模糊查找
- Jdk1.6 JUC源码解析(13)-LinkedBlockingQueue
- makefile在编译的过程中出现“except class name”
- 天猫精灵X1智能音箱使用感想
- 【吐槽向】iOS 中的仿射变换
- [蓝桥杯]PREV-27.历届试题_蚂蚁感冒
- Go Web:数据存储(2)——CSV文件
- OSI的七层模型介绍