spring boot整合activeMQ,实现ptp和topic两者消息模式
来源:互联网 发布:淘宝网商城玉镯 编辑:程序博客网 时间:2024/05/17 13:44
如何下载安装MQ我就不说了,百度一大把,老规矩先上一下项目目录结构:
先看一下配置文件,主要是中间件的配置:
ps:1.主要注意的是activeMQ默认提供ptp模式,若要使用topic模式需要假如最后一个配置为true
spring.activemq.broker-url=tcp://localhost:61616spring.activemq.in-memory=true spring.activemq.pool.enabled=true#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置spring.jms.pub-sub-domain=true
看一下pom用到哪些包吧,其实除了spring boot基本的两个包以外在引入两个activemq的依赖就行了:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> <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> </dependencies>
然后就是像spring容器声明配置类:
ps:
1.其实学习一段时间spring boot有点小心得了,当我们使用外部插件时需要自己声明并且写配置类,加@Bean注解来为我们需要提供服务的类进行注入,以往传统的springMVC就是在配置文件配置bean,指定class,properties之类的属性等等
2.ptp和topic两种消息模式我也不赘述了,很好理解,可以理解为qq的私聊和群聊的关系
@Configuration@EnableJmspublic class MsgListeners { /** * 点对点 * @return */ @Bean public Queue queue(){ return new ActiveMQQueue("zh-queue"); } /** * 发布/订阅 * @return */ @Bean public Topic topic(){ return new ActiveMQTopic("zh-topic"); }}
接下来是controller层接受请求
@RestController@RequestMapping("/activeMq")public class ActiveMqController { @Autowired private Queue queue; @Autowired private Topic topic; @Autowired private ProductService productService; @GetMapping("/queue/{msg}") public void sendQueue(@PathVariable("msg") String msg){ productService.sendMessage(this.queue,msg); } @GetMapping("/topic/{msg}") public void sendTopic(@PathVariable("msg")String msg){ productService.sendMessage(this.topic,msg); }}
然后是生产者service接口和其实现类:
ps:1.第一个方法实现了接口,把接收到的消息和消息模式放到了队列里或者主题里就是queue或topic里,然后只要写消费者加上@JmsListener监听队列消息就可以自动获取
2.这里的第二个方法加了监听注解就可以收到消费者反馈的信息,前提消费者要加@SendTo注解,具体看下面消费者类
public interface ProductService { void sendMessage(Destination destination,String message);}@Servicepublic class ProductServiceImpl implements ProductService{ @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Override public void sendMessage(Destination destination, String message) { jmsMessagingTemplate.convertAndSend(destination,message); } @JmsListener(destination = "return-queue") public void Message(String message){ System.out.println("Product收到:"+message); }}
消费者类,2个
@Servicepublic class Consumer { // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 @JmsListener(destination = "zh-topic") public void receiveQueue(String text) { System.out.println("Consumer收到:"+text); }}@Servicepublic class Consumer2 { // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 @JmsListener(destination = "zh-topic") @SendTo("return-queue") public String receiveQueue(String text) { System.out.println("Consumer2收到:"+text); return "Consumer2收到!"; }}
然后使用浏览器或者postman测试一下就好啦,至于如何同时支持ptp模式和topic我还没有办法呢,不知道你有没有呢?
对了,MQ还支持对象,Map,流,字节等传输我还没试过,以后在做记录吧
阅读全文
0 0
- spring boot整合activeMQ,实现ptp和topic两者消息模式
- spring整合activemq发送MQ消息[Topic模式]实例,activemqmq
- 消息中间件-activemq实战整合Spring之Topic模式(五)
- spring boot整合RabbitMQ(Topic模式)
- 利用Spring与ActiveMQ整合发送、接收消息实例(Queue与Topic模式)
- spring boot整合JMS(ActiveMQ实现)
- spring boot整合JMS(ActiveMQ实现)
- 消息中间件之ActiveMQ PTP和PUB/SUB模式实例(三)
- spring boot整合activemq
- Spring Boot 整合 RabbitMQ 之 Topic转发模式 (二)
- Spring整合ActiveMQ 实现消息服务
- ActiveMQ 整合 Spring 发送和接收消息
- 95. Spring Boot之ActiveMQ的topic和queue【从零开始学Spring Boot】
- Spring Boot教程(十四)Spring Boot整合ActiveQ实现消息收发和订阅
- Spring Boot系列(十二)Spring Boot整合ActiveQ实现消息收发和订阅
- ActiveMQ点对点模式(PTP)
- ActiveMQ两种模式PTP和PUB/SUB
- spring boot整合activemq rabbitmq
- js刷新页面方法
- 深入HBase架构解析(二)
- 在SQL中存储过程的一般语法是什么
- JAVA快速排序
- java学习系列4(Java 内存区域和GC机制)
- spring boot整合activeMQ,实现ptp和topic两者消息模式
- 论文代发方法步骤
- Word转Excel技巧之WPS另存法
- Jquery+Json+ajax实现异步查询显示数据
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 开关--并查集
- Socket编程 (连接,发送消息) (Tcp、Udp) ,这个是同步,跟上一篇差不多
- SQL Server查看视图字段列表
- HBase详细概述
- 指定Jboss的JDK