How to move JMS messages between destinations in WildFly

In this article, we will learn how to move messages from one JMS destination into another running on WildFly application server. We will also be using a filter to move messages selectively.

Firstly, let’s start create the following example JMS Destinations:

jms-queue add --queue-address=queueA --entries=java:/jms/queue/queueA 
jms-queue add --queue-address=queueB --entries=java:/jms/queue/queueB

Next, in order to move messages from queueA into queueB, you can use the following CLI command:

/subsystem=messaging-activemq/server=default/jms-queue=queueA:move-messages(other-queue-name=queueB)

Finally, consider that it is possible to use a filter when moving messages, using a message property as filter:

/subsystem=messaging-activemq/server=default/jms-queue=queueA:move-messages(filter="DEP_ID='12A'",other-queue-name=queueB)

Please notice that one common use case is moving messages from the Dead Letter Queue to another Queue. In this case, in order to move selectively only messages that were destined to one destination (for example, queueA), you can filter through the property “_AMQ_ORIG_QUEUE“.

Here is, for example, how to restore messages from the DLQ (whose destination was queueA) into the queueB:

/subsystem=messaging-activemq/server=default/jms-queue=DLQ:move-messages(filter="_AMQ_ORIG_QUEUE='queueA'",other-queue-name=queueB)

You can achieve the same result, by listening to the DLQ, and filtering messages to be moved within the onMessage method:

public void onMessage(Message message) {
   try {
        String queueDestination = "queueB";
            if (message.getStringProperty("_AMQ_ORIG_QUEUE").equals("queueA")) {
               Queue queue = session.createQueue(queueDestination);
               MessageProducer messageProducer = session.createProducer(queue);
               messageProducer.send(message);
            }

    } catch (JMSException e) {
        e.printStackTrace();
    } 
}
Found the article helpful? if so please follow us on Socials