首页 > 编程语言 > 详细

springboot实现主题模式

时间:2021-05-31 11:49:01      阅读:18      评论:0      收藏:0      [点我收藏+]

主题模式

使用注解模式绑定交换机与队列,其他模式都可以用注解来进行注册,不过使用注解模式在实现死信队列等功能时会有局限性,所以一般不会采用注解模式进行绑定。

主题模式也可以用配置形式进行绑定,这里只是做一个注解绑定的测试类。

package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.topic;
?
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
?
/**
* 主题模式(topic):消息消费者
*/
@Component
@RabbitListener(bindings = @QueueBinding(
       value = @Queue(value = "duanxin.topic.queue", durable = "true", autoDelete = "false"),
       exchange = @Exchange(value = "topic_order_exchange", type = ExchangeTypes.TOPIC),
       key = "#.duanxin.#"
))
public class DuanxinTopicConsumer {
?
   @RabbitHandler
   public void reviceMessage(String message){
       System.out.println("topic duanxin--消息为-->" + message);
  }
}
?
package com.zhang.rabbitmq.springbootrabbitmqproducer.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
* 主题模式(topic):消息生产者
*/
@Component
public class OrderTopicService {

@Autowired
private RabbitTemplate rabbitTemplate;

public void topic(String userId, String producerId, int number){
String orderId = UUID.randomUUID().toString();
System.out.println("发送成功:"+orderId);
String exchangeName = "topic_order_exchange";
//配置路由
String routingKey = "com.email.duanxin.xxx";
/**
* #.duanxin.# 短信
* *.email.# 邮箱
* com.# sms
*/
rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
}
}
 

 

springboot实现主题模式

原文:https://www.cnblogs.com/zhangjun9740/p/14829619.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!