Spring Boot 整合ActiveMQ的过程
来源:互联网 发布:编程培训加盟 编辑:程序博客网 时间:2024/06/10 22:10
Spring Boot 整合ActiveMQ的过程
- 安装ActiveMQ服务器,(也可以不安装,如果不安装,会使用内存mq)
- 构建Spring boot项目,增加依赖项,只需要添加这一项即可
<!-- 添加acitivemq依赖 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId></dependency>
- 增加Application类
@SpringBootApplication@EnableScheduling //使用定时任务发送消息public class MqTestApplication { public static void main(String[] args) { SpringApplication.run(MqTestApplication.class, args); }}
- 配置application.yml
spring: activemq: broker-url: tcp://127.0.01:61616 packages: trust-all: true
- 构建一个数据Model,可以发送和消费的数据可以是: String, byte array, Map<tring,?>, Serializable object
// 如果发送的消息是一个对象,必须implements Serializable接口public class TModel implements Serializable { private static final long serialVersionUID = -921008687184331557L; private int count; public TModel(int count) { this.count = count; } @Override public String toString() { return "TModel [count=" + count + "]"; }}
- 构建Producer
@Componentpublic class Producer { // 在Producer中注入JmsTemplate,我们可以通过这个template发送消息 private final JmsTemplate jmsTemplate; private int count = 0; @Autowired public Producer(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } // 这里使用Spring Boot的定时任务发送消息 @Scheduled(fixedRate = 1000) public void create() { // 使用convertAndSend发送消息 jmsTemplate.convertAndSend("queue1", new TModel(count++)); }}
- 构建Consumer
@Componentpublic class Consumer { @JmsListener(destination = "queue1") public void comsume(TModel content) { System.out.println("recive message from queue1 [" + content + "]"); }}
特别备注:如果我们的生产者和消费者在不同的Module中时,最好将要消费的数据抽象成公共Module.程序是通过Serializable来序列化和反序列化对象的。必须保证生产者和消费者的对象模型的serialVersionUID是一致的。
项目地址: https://github.com/ldwqh0/active-mq-spring.git
阅读全文
0 0
- Spring Boot 整合ActiveMQ的过程
- spring boot整合activemq
- spring boot整合activemq rabbitmq
- (八)Spring Boot整合ActiveMQ
- Spring Boot与ActiveMQ整合
- spring boot整合JMS(ActiveMQ实现)
- spring boot整合JMS(ActiveMQ实现)
- 学习activemq(2)Spring boot整合activemq jms
- spring boot的ActiveMQ使用
- activeMQ和spring的整合
- Spring与ActiveMQ的整合
- Spring与ActiveMQ的整合
- spring整合activemq的配置文件
- activemq与spring的整合
- ActiveMQ整合Spring的配置文件
- spring boot与activemq的结合使用
- ActiveMQ(四):Spring ActiveMQ 整合
- Spring整合ActiveMQ与Quartz的实例
- boost 定时器 demo
- 博客第三篇
- .Net文档图像处理工具包GdPicture.NET发布v14.0.29,改进OCR功能
- 数据结构课设之家谱管理系统
- Oracle学习4:distinct、between and、in、like详解
- Spring Boot 整合ActiveMQ的过程
- webpack学习
- 一个简单的图片加载器android
- 高质量子程序特点
- maven多工程应用,spring MAVEN多个子模块之间的调用
- CSS深入理解之margin
- PXE自动化部署操作系统
- SQLite实现用户登录功能
- 让你不再俱怕Fragment State Loss