I'm using Springboot 1.5.3 and amqp-client 4.0.2.
I have a producer that works fine most of the time, but once in a while I get the following error:
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]
Here's the class that calls RabbitTemplate.convertAndSend from the produce(...) method:
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;
});
}
}
}
The properties associated with rabbitmq:
spring.rabbitmq.listener.concurrency=5
spring.rabbitmq.listener.max-concurrency=5