activemq-artemisjmscontext

Closing JMSContext on failed connection


I have a Java SE application (jms client) that works with Artemis ActiveMQ. In the ActiveMQInitialContextFactory configuration, I have set reconnectAttempts = -1 so that, when the connection to the server is dropped, the connection is automatically reestablished.

Everything works fine and the connection is restored as expected. However, I noticed that when I try to call JMSContext::stop() / JMSContext::close() while the connection is broken (i.e., the client is in reconnect mode), the call blocks until the client manages to connect.

If I stop the application without calling JMSContext::stop() / JMSContext::close() when the connection is broken, it stops cleanly (apparently the implementation uses daemon threads) but this approach does not work for me, the service is used in a dialog and I want to release the occupied resources when it is closed.

this roughly illustrates the problem

java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.ConnectionFactory=tcp://192.168.0.1:1883?retryInterval=200&reconnectAttempts=-1
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

JMSContext jmsContext = cf.createContext();
jmsContext.setExceptionListener(this::handleJMSException);

Topic topic = jmsContext.createTopic("topicName");
jmsContext.createConsumer(topic).setMessageListener(this::onMessage);

...

setOnCloseRequest(dialogEvent -> {
    if(jmsContext != null) {
        jmsContext.close();
    }
});

Solution

  • ActiveMQ Artemis JMS client tries to send a closing message to the server for every active consumer, producer and session and waits for a closing message response. After the callTimeout the client gives up but during a reconnect attempt it also waits for the callFailoverTimeout.

    The default value for the callTimeout and callFailoverTimeout is 30000 milliseconds. Reducing it speeds up the context closure but also increases the risk of getting an error during a network glitch.

    connectionFactory.ConnectionFactory=tcp://192.168.0.1:1883?retryInterval=200&reconnectAttempts=-1&callTimeout=5000&callFailoverTimeout=5000