如何使用AmqpTemplate设置消费者优先级



我正在使用spring-boot-starter-amqp库:

  • 无限等待,直到可以接收到消息。
  • 处理消息,可能需要几分钟。
  • 终止程序

我还想设置消费者优先级。然而,我不知道如何设置x-priority消费者参数,因为文档只提到如何使用异步消费者,而不是AmqpTemplate或RabbitTemplate。如有任何帮助,不胜感激。

ApplicationRunner类:

private final AmqpTemplate template;
private final ApplicationContext applicationContext;
...
@Override
public void run(ApplicationArguments args) {
Message message = template.receive("analyses", -1);
if (message == null) throw new IllegalStateException("No message received");
byte[] body = message.getBody();
// todo: process the message
System.out.printf("[x] Received '%s'%n", new String(body, StandardCharsets.UTF_8));
context.close();
}

在配置类中:

@Bean
public RabbitTemplate amqpTemplate(final ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}

目前不支持;消费者是在私有方法createConsumer()中创建的,该方法不向basicConsume()传递任何参数。

请在GitHub上打开一个新的特性问题;这并不难实现,我们计划下周发布。

https://github.com/spring-projects/spring-amqp/issues

最新更新