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();
    } 
}

Recommended Articles

Secure JMS Destinations in WildFly Application Server with Elytron

Learn how to secure JMS destinations using roles and Elytron Security Domain in WildFly application server. #WildFly #JavaSecurity #JMS

Configure and Run JMS Destinations Across WildFly, EAP 6, AS 6, AS 5, and AS 4

Learn how to configure and run JMS destinations in various versions of application servers. #JMS #WildFly #JBossEAP #Java #MessagingAPI

Sending JMS Messages over XA with WildFly - A Step-by-Step Guide

Learn how to send JMS messages as part of an XA transaction with WildFly, including setting up a Resource Manager and using a specific XA Connection Factory.

Consuming Messages from a Remote WildFly JMS Server: 3 Approaches Compared

Learn how to consume messages from a remote JMS server using WildFly, including configuring a JMS bridge, remote connector, and MDB. Get expert insights and comparison of advantages/disadvantages.