栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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())); } } ```

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号