2

I have made a simple ActiveMQ application.

It listens to a queue. If a message comes, print out the dataId

Here is the code:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class MQ implements MessageListener {
    private Connection connection = null;
    private Session session = null;
    private Destination destination = null;

    private void errorOnConnection(JMSException e) {
        System.out.println("MQ is having problems. Exception::"+ e);
    }

    private void init() throws JMSException {
        String BROKER_URL = "failover:(tcp://myQueue001:61616,tcp://myQueue002:61616)?randomize=false";  
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);

        connection = connectionFactory.createConnection("user", "password");

        connection.setExceptionListener(
                    new ExceptionListener() { 
                        @Override public void onException(JMSException e) { 
                            errorOnConnection(e);
                        } 
                    });

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

        destination = session.createQueue("myQueue");


        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(this);
    }

    public boolean start() {
        try {
            if(connection==null )
                init();
            connection.start();
        } catch (Exception e) {
            System.out.println("MQListener cannot be started, exception: " + e);
        }
        return true;

    }

    @Override
    public void onMessage(Message msg) {
        try {
            if(msg instanceof MapMessage){
                MapMessage m = (MapMessage)msg;
                int dataId = m.getIntProperty("dataId");
                System.out.println(dataId);
            }
        } catch (JMSException e) {
            System.out.println("Got an exception: " + e);
        }


    }

    public static void main(String[] args) {
        MQ mq = new MQ();
        mq.start();

    }

}

It works fine and does what it is meant to accomplish.

However, the problem is that it can run only for several days. After several days, it just quits silently without any exceptions or error.

The queue I am listening to is from 3rd party. From a guy there, the queue sometimes will be closed or restarted or interrupted.

But I think even if that happen, the default ActiveMQ settings will handle it by consistently reconnect to it, right? (according to http://activemq.apache.org/cms/configuring.html)

So any other possible causes which lead my code to quitting silently?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

1 Answers1

1

Depends on bit on your version. Since you are not doing anything yourself to keep the application running but instead depending on the ActiveMQ code to keep at least one non-deamon thread running. In some ActiveMQ versions the client wasn't always doing this so your application could quite while a failover was occurring. Best bet is to switch to v5.8.0 which I believe had some fixes for this.

You could add some polling code in main to read something from console or what not to ensure that the client stays up until you are sure you want it to go down.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Thank you. To keep it up, do you have better way instead of reading from console? how about Thread.sleep in main or something like that? – Jackson Tale Jun 17 '13 at 14:44
  • My current version is 5.2.0 – Jackson Tale Jun 17 '13 at 14:44
  • Do whatever works for your use case. You could use a separate Session and Consumer to perform a blocking receive on a control topic or something. In the end its up to you to pick something that works for you. – Tim Bish Jun 17 '13 at 16:02