如何使用 spring rabbitmq 在不同队列中配置超时、重试或最大尝试
how configure timeouts, retries or max-attempts in differents queues with spring rabbitmq
是否可以为每个队列进行这些设置?我有一些重要的队列,所以我需要更多的重试次数,但有一些不太重要的队列,我不想配置重试、尝试等
public Queue newQueue(String name) {
return new Queue(name, durable, exclusive, autoDelete, arguments);
}
我看到在队列 class 中,可以将参数映射作为最后一个参数传递,但我不知道它是在这里还是通过属性传递。
这些东西不是队列的属性,它们是添加到侦听器容器的重试建议的属性。为每个队列使用不同的 container/advice。请参阅 Spring AMQP 参考手册。
在我的例子中,我必须创建一个带有重试拦截器的侦听器工厂,在重试拦截器中我设置了最大尝试次数的值。
也许适合你:
@Autowired
private ConnectionFactory connectionFactory;
@Autowired
private SomeService someService;
@RabbitListener(id = "queueListener", queues = "queueName",
containerFactory = "listenerContainerFactory")
@RabbitHandler
public void notifyLegacyListener(SomeObject obj) {
someService.doSomething(obj);
}
@Bean
public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(jsonMessageConverter());
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
factory.setAdviceChain(new Advice[] {retries()});
return factory;
}
@Bean
public RetryOperationsInterceptor retries() {
return RetryInterceptorBuilder.stateless().maxAttempts(Queues.QUEUE_LEGACY.getMaxAttempts())
.backOffOptions(1000,
3.0, 10000)
.recoverer(new RejectAndDontRequeueRecoverer()).build();
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
我能够在这样的配置中更改最大重试次数和退避策略:
spring:
rabbitmq:
listener:
simple:
retry:
enabled: true
initial-interval: 1000
max-attempts: 3
max-interval: 10000
multiplier: 2.0
仍然失败的消息将被丢弃,并带有此配置的警告。参见 this article for configuring dead letter queue
是否可以为每个队列进行这些设置?我有一些重要的队列,所以我需要更多的重试次数,但有一些不太重要的队列,我不想配置重试、尝试等
public Queue newQueue(String name) {
return new Queue(name, durable, exclusive, autoDelete, arguments);
}
我看到在队列 class 中,可以将参数映射作为最后一个参数传递,但我不知道它是在这里还是通过属性传递。
这些东西不是队列的属性,它们是添加到侦听器容器的重试建议的属性。为每个队列使用不同的 container/advice。请参阅 Spring AMQP 参考手册。
在我的例子中,我必须创建一个带有重试拦截器的侦听器工厂,在重试拦截器中我设置了最大尝试次数的值。
也许适合你:
@Autowired
private ConnectionFactory connectionFactory;
@Autowired
private SomeService someService;
@RabbitListener(id = "queueListener", queues = "queueName",
containerFactory = "listenerContainerFactory")
@RabbitHandler
public void notifyLegacyListener(SomeObject obj) {
someService.doSomething(obj);
}
@Bean
public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(jsonMessageConverter());
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
factory.setAdviceChain(new Advice[] {retries()});
return factory;
}
@Bean
public RetryOperationsInterceptor retries() {
return RetryInterceptorBuilder.stateless().maxAttempts(Queues.QUEUE_LEGACY.getMaxAttempts())
.backOffOptions(1000,
3.0, 10000)
.recoverer(new RejectAndDontRequeueRecoverer()).build();
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
我能够在这样的配置中更改最大重试次数和退避策略:
spring:
rabbitmq:
listener:
simple:
retry:
enabled: true
initial-interval: 1000
max-attempts: 3
max-interval: 10000
multiplier: 2.0
仍然失败的消息将被丢弃,并带有此配置的警告。参见 this article for configuring dead letter queue