Search code examples
javajmsactivemq-artemis

How do I get persistentSize in ActiveMQ Artemis queue via JMS?


I can't figure out how to get persistentSize in an Artemis ActiveMQ queue using JMS. I didn't find any get method in the Message class. There are different methods for getting message ID, lifetime and others, but I didn’t find any for getting persistentSize enter image description here


Solution

  • There is no way to get the ActiveMQ Artemis "persistentSize" value from a JMS Message using the JMS API. This property is not part of JMS. It is specific to ActiveMQ Artemis.

    That said, you can simply look at the message itself to determine how large it is. For example, if the message was a TextMessage then you could use the getText() method to get the String body and then use the length() method to see how long it is. If the message was a BytesMessage you could just use the getBodyLength() method.

    If you really wanted to get the "persistentSize" and you were using the core JMS client shipped with ActiveMQ Artemis then you'd have to cast the JMS Message to the ActiveMQ Artemis implementation object org.apache.activemq.artemis.jms.client.ActiveMQMessage and use the getCoreMessage method to get the org.apache.activemq.artemis.api.core.client.ClientMessage and then invoke getPersistentSize().

    This, of course, is not recommended as it would destroy the portability of your application which is one of the main benefits of using an API like JMS in the first place. In short, you could never use this code with any other JMS client implementation or any JMS other broker implementation and since it's considered internal then it is subject to break without notice during an upgrade.