在代理连接断开的情况下,Spring AMQP消息弹性

时间:2017-06-01 07:02:16

标签: rabbitmq spring-amqp spring-rabbit spring-rabbitmq spring-retry

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

同样我使用了Spring Retry

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代理连接恢复时也永远不会恢复

1 个答案:

答案 0 :(得分:2)

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

对我来说很好......

@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]