我使用的是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]
以下是从product(…(方法调用RabbitTemplate.covertAndSend的类:
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
Boot 1.5.x多年来一直不受支持。
甚至不再支持Spring AMQP 1.7.x(以及Spring framework 4.x(。
有关支持的版本,请参阅此处。
也就是说,该错误只是意味着您的应用程序在应用程序上下文关闭后试图重新连接到RabbitMQ。