ActiveMQ Artemis - javax.jms.IllegalStateException: AMQ219019: Session is closed

Viewed 1707

I have two ActiveMQ Artemis brokers running on different machines making a simple cluster. I am using a Java application (very basic) to produce and consume the messages to analyze the behaviour of the cluster. The Java code is as follows:

public void runExample() throws Exception {
    InitialContext initialContext = null;
    Connection connectionA = null;

    try {
        Properties properties = new Properties();
        properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
        properties.put("connectionFactory.ConnectionFactory", "udp://231.7.7.7:9876");
        properties.put("queue.queue/anotherExampleQueue", "anotherExampleQueue"); 

        initialContext = new InitialContext(properties);
        Queue queue = (Queue) initialContext.lookup("queue/anotherExampleQueue");
        ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

        Thread.sleep(5000);
        connectionA = connectionFactory.createConnection("admin", "admin");
        
        Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
        System.out.println("Session A - " + ((ClientSessionInternal)((org.apache.activemq.artemis.jms.client.ActiveMQSession) sessionA).getCoreSession()).getConnection().getRemoteAddress());
        
        MessageProducer producerA = sessionA.createProducer(queue);
        
        final int numMessages = 10;

        for (int i = 0; i < numMessages; i++) {
            TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
            producerA.send(messageA);
            System.out.println("Sent message: " + messageA.getText());
        }

        connectionA.start();
        consume(sessionA, queue, numMessages, "A");
        
    } finally {
        if (connectionA != null) {
            connectionA.close();
        }
        if (initialContext != null) {
            initialContext.close();
        }
    }
}

private static void consume(Session session, Queue queue, int numMessages, String node) throws JMSException {
    MessageConsumer consumer = session.createConsumer(queue);

    for (int i = 0; i < numMessages; i++) {
        TextMessage message = (TextMessage) consumer.receive(2000);
        
        if(message!=null)
            System.out.println("Got message: " + message.getText() + " from node " + node);
    }

    System.out.println("receive other message from node " + node + ": " + consumer.receive(2000));
}

While debugging the above application with a break-point at connectionA.start(). If I stop my master broker then I see that the slave broker took over and all the messages were moved to slave broker as expected. However, at this point, if I continue with my application then it throws javax.jms.IllegalStateException: AMQ219019: Session is closed instead of consuming the messages at the slave broker. The same thing happens when I start my master broker again and continue with debugging. The documentation says that automatic client failover would happen automatically.

Here is the snippet of master broker.xml

<connectors>
   <connector name="clusterConnectorOne">tcp://10.10.170.5:61616</connector>
</connectors>

<discovery-groups>
   <discovery-group name="my-discovery-group">
      <local-bind-address>10.10.170.5</local-bind-address>
      <group-address>231.7.7.7</group-address>
      <group-port>9876</group-port>
      <refresh-timeout>10000</refresh-timeout>
   </discovery-group>
</discovery-groups>

<cluster-connections>
   <cluster-connection name="my-cluster">
      <connector-ref>clusterConnectorOne</connector-ref>
      <retry-interval>500</retry-interval>
      <use-duplicate-detection>true</use-duplicate-detection>
      <message-load-balancing>STRICT</message-load-balancing>
      <max-hops>1</max-hops>
      <discovery-group-ref discovery-group-name="my-discovery-group"/>
   </cluster-connection>
</cluster-connections>
      
<broadcast-groups>
   <broadcast-group name="my-broadcast-group">
      <local-bind-address>10.10.170.5</local-bind-address>
      <local-bind-port>5432</local-bind-port>
      <group-address>231.7.7.7</group-address>
      <group-port>9876</group-port>
      <broadcast-period>2000</broadcast-period>
      <connector-ref>clusterConnectorOne</connector-ref>
   </broadcast-group>
</broadcast-groups>

<ha-policy>
   <replication>
      <master>
        <check-for-live-server>true</check-for-live-server>
      </master>
   </replication>
</ha-policy>

I couldn't figure out whats going wrong here, any suggestions?

1 Answers

Since your connection factory is using udp://231.7.7.7:9876 it is "discovering" the connector that it ultimately needs to use. In your case, the connector tcp://10.10.170.5:61616 is being broadcast by your broker so that is what the client will discover and use. This connector, however, is not configured for HA. I needs to be something like tcp://10.10.170.5:61616?ha=true;reconnectAttempts=-1 in order to tell the client to failover to the backup when connectivity is lost. Update your connector configuration in broker.xml and failover should work fine. A lot of the high availability examples which ship with the broker demonstrate this setting, e.g. transaction-failover.

Related