springboot整合RabbitMQ3.6.3版本优先队列 队列没有起作用

springboot整合RabbitMQ3.6.3版本优先队列 队列没有起作用

 问题相关代码\ 先创建交换机以及队列等

``` @Configuration public class priorityConfig { //创建优先级队列等 //交换机 public static final String PRIORITYEXCHANGENAME ="priority.exchange"; //队列 public static final String PRIORITYQUEUENAME ="priority.queue"; //信道 public static final String PRIORITYROTINGKEY_NAME ="k1";

//创建交换机
@Bean
public DirectExchange PRIORITYEXCHANGENAME(){
    return  new DirectExchange(PRIORITY_EXCHANGE_NAME);
}
//创建队列
@Bean
public Queue PRIORITYQUEUEENAME(){

// return QueueBuilder.durable(PRIORITYQUEUENAME).maxPriority(10).build(); HashMap map = new HashMap<>(); map.put("x-max-priority",10); return QueueBuilder.durable(PRIORITY QUEUENAME).withArguments(map).build(); } //创建交换机 @Bean public Binding Binding(@Qualifier("PRIORITYEXCHANGENAME") DirectExchange exchange,@Qualifier("PRIORITYQUEUEENAME") Queue queue){ return BindingBuilder.bind(queue).to(exchange).with(PRIORITY ROTINGKEY_NAME); } } ```

然后 编写 消息 生产者以及消费者\ 生产者

``` @RestController @Slf4j public class prioritycontroller { //交换机 public static final String PRIORITYEXCHANGENAME ="priority.exchange"; //队列 public static final String PRIORITYQUEUENAME ="priority.queue"; //信道 public static final String PRIORITYROTINGKEY_NAME ="k1"; @Autowired RabbitTemplate rabbitTemplate;

@GetMapping("/kk")
public void test() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            //1.创建消息属性
            MessageProperties messageProperties = new MessageProperties();
            //2.设置优先级
            messageProperties.setPriority(9);
            //3.创建消息对象
            Message message = new Message(("sendMessage..."+i).getBytes(),messageProperties);
            //4.发送消息
            rabbitTemplate.convertAndSend(PRIORITY_EXCHANGE_NAME,PRIORITY_ROTING_KEY_NAME,message);
        } else {
            //1.创建消息属性
            MessageProperties messageProperties = new MessageProperties();
            //2.设置优先级
            messageProperties.setPriority(1);
            //3.创建消息对象
            Message message = new Message(("sendMessage..."+i).getBytes(),messageProperties);
            rabbitTemplate.convertAndSend(PRIORITY_EXCHANGE_NAME, PRIORITY_ROTING_KEY_NAME, message );
        }
    }
}

} //消费者 @Component @Slf4j public class Consumer { @RabbitListener(queues = "priority.queue") public void receiveConfirmMessages(Message message) { log.info("收到了消息为:" + new String(message.getBody())); } } ```

我先是注销这个消费者模拟一个阻塞状态 消息全部在 队列中 ,生产者发送消息 到队列 我看图形化界面也是 排好序了的 也就是说 第五条消息是在第一位

img

事实证明我是对的图形化界面 第五条消息是在第一 然后 启用消费者 发现 打印出来的消息 不是第一条 是其他消息条数

img

尝试过其他博主的方法 没有成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值