0

I have created a Durable subscriber for topic in ActiveMQ, But whenever activeMQ server restart I have to restart my java services to make durable subscriber available. Is there any way to make subscriber available automatically whenever activemq restarts happen??

Below is sample code I am using.

@Bean
public ConnectionFactory connectionFactory() throws JMSException{
    ConnectionFactory factory= new ActiveMQConnectionFactory(brokerURL);
    Connection conn = factory.createConnection();
    conn.setClientID(ClientId);

    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(exampleTopic);
    MessageConsumer messageConsumer = session.createDurableSubscriber(topic, 
    subscriberName);
    messageConsumer.setMessageListener(consumerMessageListner);
    conn.start();

    return factory;
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
JayShah
  • 1
  • 1

1 Answers1

0

Since you are using Spring, you should use Spring's built-in JMS support (in spring-jms jar) instead of writing your own JMS code. See the documentation. The DefaultMessageListenerContainer will keep trying to reconnect for you...

@Bean
public DefaultMessageListenerContainer container(ConnectionFactory connectionFactory,
        MessageListener messageListener) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setDestinationName("myTopic");
    container.setPubSubDomain(true);
    container.setSubscriptionDurable(true);
    container.setSubscriptionName("mySubscriber");
    container.setClientId("myClientId");
    container.setSessionTransacted(true);
    container.setMessageListener(messageListener);
    return container;
}

@Bean
public MessageListener listener() {
    return System.out::println;
}

@Bean
public ApplicationRunner runner(JmsTemplate template) {
    return args -> {
        while (true) {
            template.convertAndSend("foo");
            System.in.read();
        }
    };
}

and...

ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:gollum.local-64266-1509806046256-1:2:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:gollum.local-64266-1509806046256-1:2:1:1, destination = topic://myTopic, transactionId = null, expiration = 0, timestamp = 1509806046424, arrival = 0, brokerInTime = 1509806046425, brokerOutTime = 1509806046475, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@34691ad3, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = foo}

2017-11-04 10:34:12.813 WARN 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer : Setup of JMS message listener invoker failed for destination 'myTopic' - trying to recover. Cause: java.io.EOFException

2017-11-04 10:34:12.816 ERROR 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer : Could not refresh JMS Connection for destination 'myTopic' - retrying using FixedBackOff{interval=5000, currentAttempts=0, maxAttempts=unlimited}. Cause: Could not connect to broker URL: tcp://10.0.0.3:61616. Reason: java.net.ConnectException: Connection refused (Connection refused)

2017-11-04 10:34:17.823 ERROR 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer : Could not refresh JMS Connection for destination 'myTopic' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Could not connect to broker URL: tcp://10.0.0.3:61616. Reason: java.net.ConnectException: Connection refused (Connection refused)

2017-11-04 10:34:22.827 ERROR 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer : Could not refresh JMS Connection for destination 'myTopic' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: Could not connect to broker URL: tcp://10.0.0.3:61616. Reason: java.net.ConnectException: Connection refused (Connection refused)

2017-11-04 10:34:27.897 INFO 2889 --- [ container-1] o.s.j.l.DefaultMessageListenerContainer : Successfully refreshed JMS Connection

Community
  • 1
  • 1
Gary Russell
  • 166,535
  • 14
  • 146
  • 179