Spring 代理连接中断时的 AMQP 消息弹性

Spring AMQP message resiliency in case broker connectivity down

我有一个在 RabbitMQ 服务器连接中断的情况下管理 Spring AMQP 客户端消息弹性的用例,

我用过的一样Spring重试

RabbitTemplate template = // get template from some bean 
RetryTemplate retryTemplate = new RetryTemplate();
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
// 10 sec 
backOffPolicy.setInitialInterval(10000);
// 1 hr
backOffPolicy.setMultiplier(360.0);
// 1 hr max interval
backOffPolicy.setMaxInterval(3600001);
retryTemplate.setBackOffPolicy(backOffPolicy);
template.setRetryTemplate(retryTemplate);

template.convertAndSend("Direct-Exchange",
        "Test.Routing.Key", AMQPMessage);

但是当尝试测试它并关闭代理时,它挂在 template.convertAndSend() 并且即使 RabbitMQ 代理连接恢复也无法恢复

您的重试配置相当极端。你等了一个小时了吗?

对我来说很好用...

@SpringBootApplication
public class So44300651Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(So44300651Application.class, args).close();
    }

    @Autowired
    private RabbitTemplate template;

    @Override
    public void run(String... args) throws Exception {
        template.convertAndSend("foo");
    }

    @Bean
    public RabbitTemplate template(ConnectionFactory cf) {
        RabbitTemplate template = new RabbitTemplate(cf);
        RetryTemplate retry = new RetryTemplate();
        ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy();
        backOff.setInitialInterval(10_000);
        backOff.setMultiplier(1.5);
        backOff.setMaxInterval(15_000);
        retry.setBackOffPolicy(backOff);
        template.setRetryTemplate(retry);
        return template;
    }

}

结果:

09:19:45.592 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=0
09:19:45.603 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 10000
09:19:55.607 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=1
09:19:55.607 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=1
09:19:55.608 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 15000
09:20:10.610 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=2
09:20:10.610 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=2
09:20:10.654 [main] INFO  o.s.a.r.c.CachingConnectionFactory - Created new connection: SimpleConnection@13cf7d52 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 56958]