How can I get all messages from an ActiveMQ broker?

So, I have an ActiveMQ broker and some producers who put some home-made objects into the broker. I also have some consumers who receive these messages (serialized objects) without problems. But I want to create a tool that connects to a broker and displays all messages (serialized objects).

I tried to do this 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 in 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 the ActiveMQObjectMessage object has a null object set.

Is this approach good, and if so, what am I doing wrong? How can I get an object from a broker?

+8
jms activemq
source share
1 answer

I think your approach to using queuebrowser is good. If the goal is simply to view the messages in the queue and not provide a user interface, you can also use the JMX support - http://activemq.apache.org/jmx.html and use the jconsole or jvisualvm tool to connect to the JMX listener.

You are probably using a browser / queue monitor on an instance other than your customers or manufacturers, therefore your classes are not available for this instance and why the call to objectMessage.getObject () fails - it will need a class definition so that it is available to convert the serialized object into the object. You can try putting the jars classes in the instance where you use the browser and see if this works.

+2
source share

All Articles