RabbitMQ如何在AmqpIOException UnknownHostException之后停止重新连接

时间:2019-05-23 07:40:55

标签: spring-boot spring-rabbitmq

对于我们的集成测试配置文件,我们希望禁用Spring的自动重新连接以快速失败。

Attempting to connect to: [rabbitmq-invalid-host:5672]
Error creating consumer; retrying in 5000
org.springframework.amqp.AmqpConnectException: org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: rabbitmq-invalid-host

以下设置尚未生效:

spring.rabbitmq.connection-timeout=1000
spring.rabbitmq.requested-heartbeat=1
spring.rabbitmq.listener.direct.missing-queues-fatal=true
spring.rabbitmq.listener.direct.retry.enabled=false
spring.rabbitmq.template.retry.enabled=false

有什么想法让amqp放弃甚至破坏整个应用环境吗?

1 个答案:

答案 0 :(得分:0)

您可以在事件侦听器中侦听异常...

@SpringBootApplication
public class So56270299Application {

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

    @EventListener
    public void events(ListenerContainerConsumerFailedEvent event) {
        System.out.println(event);
        if (event.getThrowable().getCause() instanceof UnknownHostException) {
            System.exit(1);
        }
    }

    @RabbitListener(queues = "so56270299")
    public void listen(String in) {

    }

}