I'm trying to simulate a reconnect with this scenario:

  1. Client 1 connects to ActiveMQ
  2. Client 2 connects to ActiveMQ with failover/reconnection with same clientId as client 1 - expected to throw already connected error
  3. Kill client 1 (doesn't call close or properly clean up the connection)
  4. Expect that client 2 reconnects after awhile

Client 2 somehow cannot reconnect and leaves a dead connection to the broker. Rerunning client 1 or 2 after the scenario throws already connected error too.

Client 1 test case:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "guest", "guest",
                "tcp://localhost:61616" );
        connectionFactory.setClientID( "guest" );
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Thread.sleep( Long.MAX_VALUE );

Client 2 test case:

        String clientId = "guest";
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "guest", "guest",
                "failover:(tcp://localhost:61616)" );
        connectionFactory.setClientID( clientId );
        SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory( connectionFactory );
        singleConnectionFactory.setClientId( clientId );
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory( singleConnectionFactory );
        container.setDestinationName( "topic://test" );
        container.afterPropertiesSet();
        container.start();
        Thread.sleep( Long.MAX_VALUE );

Both clients connect to the same broker and has a maxInactivityDuration set to 10000. I expect ActiveMQ to clean up client 1 after 10s with this parameter set but doesn't work too.

Exception thrown by client 2:


30/05/19 10:23:11.735 INFO  [DefaultMessageListenerContainer] JMS message listener invoker needs to establish shared Connection
30/05/19 10:23:11.746 ERROR [DefaultMessageListenerContainer] Could not refresh JMS Connection for destination 'topic://test' - retrying using FixedBackOff{interval=5000, currentAttempts=0, maxAttempts=unlimited}. Cause: Broker: localhost - Client: guest already connected from tcp://127.0.0.1:64437
30/05/19 10:23:16.753 ERROR [DefaultMessageListenerContainer] Could not refresh JMS Connection for destination 'topic://test' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Broker: localhost - Client: guest already connected from tcp://127.0.0.1:64455
30/05/19 10:23:21.761 ERROR [DefaultMessageListenerContainer] Could not refresh JMS Connection for destination 'topic://test' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: Broker: localhost - Client: guest already connected from tcp://127.0.0.1:64455
... (and so on)

Observations:

  • If client 2 uses a SingleConnectionFactory/CachingConnectionFactory, it cannot reconnect.
  • If client 2 uses ActiveMQConnectionFactory on its container, it's able to reconnect. (showed Successfully refreshed JMS Connection)
  • Based on the exception log, it first says that client 1 is connected - port:64437, after stopping client 1, it says that there is another client connected - port:64455

Spring version: 4.3.24.RELEASE ActiveMQ JMS client library version: 5.15.9 ActiveMQ broker version: 5.15.9

Comment From: danieljohngomez

Update: Sometimes client 2 can reconnect but most of the time it can't.

Comment From: danieljohngomez

Resetting the connection upon getting the InvalidClientIDException seems to work. I added this on client 2:

    container.setExceptionListener( exception -> {
            if ( exception instanceof InvalidClientIDException  )
                singleConnectionFactory.resetConnection();
        } );

Well this is a just a workaround since it may call the resetConnection() even if it's not necessary.

Comment From: snicoll

Can you share why you're using SingletonConnectionFactory, rather than the ActiveMQConnectionFactory directly? Also, this may be fixed in the meantime, see #29115.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.