使用注解模式绑定交换机与队列,其他模式都可以用注解来进行注册,不过使用注解模式在实现死信队列等功能时会有局限性,所以一般不会采用注解模式进行绑定。
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):消息消费者
*/
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);
}
}
原文:https://www.cnblogs.com/zhangjun9740/p/14829619.html