我正在使用 Springboot 1.5.3
和 amqp-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, routingKey, messageToBeDelivered,
m -> {
m.getMessageProperties().setCorrelationId("test-id".getBytes());
return m;
});
}
}
}
与rabbitmq相关的属性:
spring.rabbitmq.listener.concurrency=5
spring.rabbitmq.listener.max-concurrency=5
答案 0 :(得分:1)
多年来一直不支持 Boot 1.5.x。
甚至不再支持 Spring AMQP 1.7.x(和 spring 框架 4.x)。
有关支持的版本,请参阅 here。
也就是说,该错误仅表示您的应用程序正在尝试在应用程序上下文关闭后重新连接到 RabbitMQ。