Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all messages from a ActiveMQ broker?

Tags:

jms

activemq

So I have an ActiveMQ broker and some producers that puts some self made objects in the broker. I also have some consumers that receive those messages(serialized objects) without problem. But I want to create a tool that connects to the broker and displays all messages (serialized objects).

I tried to do that using and ActiveMQConnection :

Set<ActiveMQQueue> currentMessageQueues = activeMQConnection.getDestinationSource().getQueues();
Iterator<ActiveMQQueue> messageQueueIterator = currentMessageQueues.iterator();

while (messageQueueIterator.hasNext()) {
    ActiveMQQueue currentQueue = messageQueueIterator.next();
    QueueSession queueSession = activeMQConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
    QueueBrowser browser = queueSession.createBrowser(currentQueue);
    Enumeration<?> messagesInQueue = browser.getEnumeration();

    while (messagesInQueue.hasMoreElements()) {
       Message queueMessage = (Message) messagesInQueue.nextElement();

       if (queueMessage instanceof ActiveMQObjectMessage) {
          ActiveMQObjectMessage objectMessage = (ActiveMQObjectMessage) queueMessage;
          objectMessage.getObject();
       }
    }
 }

With this code I get an exception on objectMessage.getObject():

javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: MyMessage

I used debug mode and ActiveMQObjectMessage has the object set to null.

Is this approach good and If yes what I am doing wrong? How can I also receive the object from the broker?

like image 234
telebog Avatar asked Oct 11 '11 11:10

telebog


1 Answers

I think your approach of using a queuebrowser is good. If the purpose is to just view the messages on the queue and not provide a UI to it, you can also use the JMX support - http://activemq.apache.org/jmx.html and use a tool like jconsole or jvisualvm to connect to the JMX listener.

You are probably running your queue browser/monitor on an instance different from your consumers or producers, this is the reason why your classes are not available to this instance and why the objectMessage.getObject() call fails - it would need the class definition to be available for it to transform the serialized object into an object. You can try putting the class jars in the instance where you are running your browser and see if that works.

like image 197
Biju Kunjummen Avatar answered Oct 20 '22 03:10

Biju Kunjummen