RabbitTemplate Error: The ApplicationContext is closed and the ConnectionFactory can no longer create connections

Viewed 295

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
1 Answers

Boot 1.5.x has not been supported for years.

Even Spring AMQP 1.7.x (and spring framework 4.x) is no longer supported.

See here for supported versions.

That said, the error simply means your application is trying to reconnect to RabbitMQ after the application context has been shut down.

Related