Search code examples
javajmsactivemq-artemis

How to adapt JMS code to ActiveMQ Artemis code?


I have code that simply reads a queue and receives JMS Message class objects. But this class does not have many getters, such as getPersistenSize and others. I found the answer to my question about obtaining such methods in a custom way, but asking this question makes me curious. Is it possible to rewrite my code as an object of the Message class ActiveMQ Artemis, and not Message JMS. I am also attaching a link to my previous question, which will suddenly give some hint to make it clearer what I mean in this matter. How do I get persistentSize in ActiveMQ Artemis queue via JMS?

My code:

connectQueueBrowser();
Enumeration<Message> messageEnumeration =  queueBrowser.getEnumeration();
ArrayList<Message> messages = new ArrayList<>();
while (messageEnumeration.hasMoreElements()){
   messages.add(messageEnumeration.nextElement());
}
System.out.println(messages.get(0).getBody(String.class));

Solution

  • ActiveMQ Artemis ships the "core" client which implements the "core" API - a full featured messaging API. Of course, it will require you to completely rewrite your application(s) which will nullify the main benefit you get from JMS - the ability to use different client and broker implementations with the same application code.

    A somewhat more reasonable approach would be to combine use of both the "core" and JMS APIs. This is easily possible because the core JMS client (i.e. the JMS client shipped with ActiveMQ Artemis) is actually implemented on top of the core client. For example, the implementation of JMS Message (i.e. ActiveMQMessage) has a method named getCoreMessage which will return the core API's ClientMessage which you can then use to get the core details you want. However, you'll still be sacrificing application portability even with this method.

    Ultimately I wouldn't recommend either option. JMS provides ways to get the information your client really needs and even some implementation-specific details (e.g. via custom properties). It's not clear why you would need specific details from the underlying implementation under normal circumstances.