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())); } } ```
我先是注销这个消费者模拟一个阻塞状态 消息全部在 队列中 ,生产者发送消息 到队列 我看图形化界面也是 排好序了的 也就是说 第五条消息是在第一位
事实证明我是对的图形化界面 第五条消息是在第一 然后 启用消费者 发现 打印出来的消息 不是第一条 是其他消息条数
尝试过其他博主的方法 没有成功