SpringBoot整合Activemq
创始人
2024-04-30 08:50:20
0

目录

一、Pom.xml

二、Spring配置文件

三、队列

四、主题


一、Pom.xml

org.apache.activemqactivemq-all5.10.0com.fasterxml.jackson.corejackson-databind2.10.1org.apache.activemqactivemq-pool5.15.10org.springframeworkspring-jms5.2.1.RELEASEorg.apache.xbeanxbean-spring4.15org.springframeworkspring-aop5.2.1.RELEASEorg.springframeworkspring-core4.3.23.RELEASEorg.springframeworkspring-context4.3.23.RELEASEorg.springframeworkspring-aop4.3.23.RELEASEorg.springframeworkspring-orm4.3.23.RELEASE

二、Spring配置文件



三、队列

(一)生产者

@Service
public class SpringMQ_Produce {@Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");SpringMQ_Produce produce = (SpringMQ_Produce) applicationContext.getBean("springMQ_Produce");
//        produce.jmsTemplate.send(new MessageCreator() {
//            @Override
//            public Message createMessage(Session session) throws JMSException {
//                TextMessage textMessage = session.createTextMessage("spring整合MQ");
//                return textMessage;
//            }
//        });produce.jmsTemplate.send((Session session) -> {TextMessage textMessage = session.createTextMessage("spring整合MQ");return textMessage;});System.out.println("send task over");}
}

 

 

(二)消费者

@Service
public class SpringMQ_Consumer {@Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");SpringMQ_Consumer consumer = (SpringMQ_Consumer) applicationContext.getBean("springMQ_Consumer");String revalue = (String)consumer.jmsTemplate.receiveAndConvert();System.out.println("消费者收到消息:"+revalue);}
}

 

 

四、主题

(一)spring配置文件修改(橙色为修改或者添加)











(二)生产者和消费者

可以看到前面的生产者和消费者的代码中我们并没有指定使用队列还是主题,所以代码无需改动,但是要注意主题需要先启动消费者。

 

 

(三)在spring中实现消费者不启动,直接通过配置监听完成

 上面的主题相当于我们之前的同步阻塞方式,接下来我们实现异步阻塞方式

(1)spring配置文件添加

(2)编写监听类

@Component
public class myMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {if(null != message && message instanceof TextMessage) {TextMessage textMessage = (TextMessage)message;System.out.println("监听到消息:"+textMessage.getText());}}
}

(3)只启动生产者,不启动消费者,自动会监听记录

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...