Stringboot实现RabbitMQ

理论:
参考: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值