Retry to establish a JMS connection while ActiveMQ broker is not available

960 views Asked by At

Here is my scenario. I have few ActiveMQ (JBoss-AMQ) producers and consumers installed as services. In a server restart, what is the best practice of handling such a situation where a producer or a consumer service starts before the ActiveMQ broker service. In that case producer/client cannot establish a connection and starts to hang on as it is even after the broker service starts.

here's my code snippet of connection creation:

try {
            connection = connectionFactory.createConnection();
            connection.start();
            LOGGER.info(STARTED_CONNECTION_WITH_THE_DESTINATION + destinationName);
            session = createSession();
            destination = session.createQueue(destinationName);
            LOGGER.info(CREATED_QUEUE_IN_DESTINATION + destinationName);
            if (isImageProcAgent) {
                consumer = createConsumer();
                LOGGER.info(CONSUMER_HAS_BEEN_INITIALIZED);
            } else {
                producer = session.createProducer(destination);
                LOGGER.info(PRODUCER_HAS_BEEN_INITIALIZE);
            }
        } catch (MessagingException e) {
            LOGGER.error(e);
        } catch (JMSException e) {
            LOGGER.error(e);
        }

I'm new to JMS so appreciate your support.

1

There are 1 answers

0
helloworld On

This can be achieved by configuring a failover as this document explains. according to my code snippet, the change I required it:

destination = session.createQueue("failover:"+destinationName);
producer = session.createProducer("failover:"+destination);