RabbitTemplate 错误:ApplicationContext 已关闭,ConnectionFactory 无法再创建连接

问题描述

我正在使用 Springboot 1.5.3amqp-client 4.0.2

我的制作人大部分时间都可以正常工作,但偶尔我会收到以下错误

java.lang.IllegalStateException: The ApplicationContext is closed and the ConnectionFactory can no longer create connections.
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:561) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1430) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:712) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:813) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:803) ~[spring-rabbit-1.7.2.RELEASE.jar!/:na]  
    at com.my.amqp.producer.MyProducer.produce(MyProducer.java:16) ~[classes!/:0.6.0-SNAPSHOT]

这是从produce(...)方法调用RabbitTemplate.convertAndSend的类:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class MyProducer {
    @Value("${rabbit.exchange.in.name}")
    public String exchangeNameIn;

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void produce(String messagetobedelivered,String routingKey,int messageDelay) throws Exception {
        if (messageDelay > 0) {
            rabbitTemplate.convertAndSend(exchangeNameIn,routingKey,messagetobedelivered,m -> {
                    m.getMessageProperties().setCorrelationId("test-id".getBytes());
                    m.getMessageProperties().setHeader("x-delay",messageDelay);
                    return m;
                });
        } else {
            rabbitTemplate.convertAndSend(exchangeNameIn,m -> {
                    m.getMessageProperties().setCorrelationId("test-id".getBytes());
                    return m;
                });
        }
    }
}

与rabbitmq相关的属性

spring.rabbitmq.listener.concurrency=5
spring.rabbitmq.listener.max-concurrency=5

解决方法

多年来一直不支持 Boot 1.5.x。

甚至不再支持 Spring AMQP 1.7.x(和 spring 框架 4.x)。

有关支持的版本,请参阅 here

也就是说,该错误仅表示您的应用程序正在尝试在应用程序上下文关闭后重新连接到 RabbitMQ。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...