理论:
参考:https://blog.csdn.net/weixin_34010949/article/details/93101466
安装:
参考:https://blog.csdn.net/zhm3023/article/details/82217222
实现:
Maven
<!-- RabbitMq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
工具类
package mms.shop.biz.mq.rabbit.task;
public class Name {
//设置了过期时间的队列名称
public static final String ordinaryTtlQueueName = "ordinaryTtlQueueName";
//设置了消息最大个数的队列名称
public static final String ordinaryMaxQueueName = "ordinaryMaxQueueName";
//普通队列名名称
public static final String ordinaryQueueName = "ordinaryQueueName";
//普通交换机名称
public static final String ordinaryExchangeName = "exchangeName";
//普通路由key
public static final String ordinaryRouteKey = "ordinaryRouteKey";
//传给ordinaryTtlQueueName 队列 用的路由key
public static final String ordinaryRouteKey1 = "ordinaryRouteKey1";
//传给 ordinaryMaxQueueName 队列用的 路由key
public static final String ordinaryRouteKey2 = "ordinaryRouteKey2";
//死信队列名称
public static final String SXqueueName = "SXqueueName";
//绑定死信队列的交换机的名称
public static final String SXexchangeName = "SXexchangeName";
//传给死信队列消息 所用的 路由 key
public static final String SXrouteKey = "SXrouteKey";
public static final String testQueue = "testQueue";
public static final String testKey = "testKey";
}
实现一:
@Autowired
private RabbitTemplate rabbitTemplate;
//发送
public void mqHandle() {
rabbitTemplate.convertAndSend(Name.ordinaryExchangeName,Name.testKey, JSON.toJSONString(method));
}
//监听
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = Name.testQueue, durable = "true"), exchange = @Exchange(value = Name.ordinaryExchangeName, ignoreDeclarationExceptions = "true"), key = Name.testKey))
public void testQueue(String msg) throws IOException {
System.out.println("================================="+msg);
}
// @RabbitListener(bindings =
// @QueueBinding(
// value = @Queue( value = "liveQueue",
// arguments ={
// @Argument(name = "x-dead-letter-exchange", value = "deadExchange"), //死信队列
// @Argument(name = "x-dead-letter-routing-key", value = "deadKey")//给死信队列发消息用的路由key
// //@Argument(name = "x-message-ttl",value = "10000",type = "java.lang.Integer"), 过期时间
// //@Argument(name = "x-max-length",value = "5",type = "java.lang.Integer")队列最大长度
// }
// ),//可以指定多种属性
// exchange = @Exchange(value = "liveExchange"),
// key = {"info", "error", "warning"} //路由key有可以 可以设置多个
// )
// )
实现二:
package mms.shop.biz.config.mq;
import mms.shop.biz.mq.rabbit.task.Name;
import org.springframework.amqp