admin管理员组

文章数量:1023025

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=.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();
    }
});

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=.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();
    }
});
Share Improve this question asked Nov 19, 2024 at 11:10 mr mcwolfmr mcwolf 2,8792 gold badges17 silver badges33 bronze badges 2
  • The ClientConsumerImpl close method has a timeout of 10 seconds, could you confirm that the JMSContext::close() method returns after 10 seconds when the connection is broken? – Domenico Francesco Bruscino Commented Nov 19, 2024 at 14:23
  • @DomenicoFrancescoBruscino, No, if there is a timeout, it is in minutes (I waited a few minutes to see if the timeout would work, but it didn't). I don't know if it matters, but I'm testing on Linux (I'm using artemis-jms-client version 2.31.0 but I also tested with 2.38.0 and the behavior is the same) – mr mcwolf Commented Nov 19, 2024 at 15:05
Add a comment  | 

1 Answer 1

Reset to default 2

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

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=.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();
    }
});

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=.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();
    }
});
Share Improve this question asked Nov 19, 2024 at 11:10 mr mcwolfmr mcwolf 2,8792 gold badges17 silver badges33 bronze badges 2
  • The ClientConsumerImpl close method has a timeout of 10 seconds, could you confirm that the JMSContext::close() method returns after 10 seconds when the connection is broken? – Domenico Francesco Bruscino Commented Nov 19, 2024 at 14:23
  • @DomenicoFrancescoBruscino, No, if there is a timeout, it is in minutes (I waited a few minutes to see if the timeout would work, but it didn't). I don't know if it matters, but I'm testing on Linux (I'm using artemis-jms-client version 2.31.0 but I also tested with 2.38.0 and the behavior is the same) – mr mcwolf Commented Nov 19, 2024 at 15:05
Add a comment  | 

1 Answer 1

Reset to default 2

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

本文标签: activemq artemisClosing JMSContext on failed connectionStack Overflow