JMS Bridge Example

This example shows how to configure and run a JMS Bridge in JBoss AS 5.
A bridge receives messages from a source JMS destination and forwards them to a target destination.

The source and target destinations can be on different servers, even from different JMS providers. For example, you can use this JMS Bridge to bridge a legacy JMS provider to HornetQ during migration.

This example will show how to configure and run the simplest bridge:

Example configuration

To run the example, you need to download JBoss AS 5.x and create a configuration for HornetQ.

JBoss AS configuration

Please refer to HornetQ Quickstart guide to install it in JBoss AS 5

JMS Bridge configuration

The JMS Bridge is configured using JBoss microcontainer (jms-bridge-jboss-beans.xml contains comments about the various parameters used to configure the bridge).

The Bridge is deployed in the application server when you simply type ./build.sh deploy (or build.bat deploy on windows) (it is copied to ${JBOSS_HOME}/server/default-with-hornetq/deploy/).

Example step-by-step

To deploy and start the server, simply type ./build.sh deploy (or build.bat deploy on windows).

Once the server has started, simply type ./build.sh run (or build.bat run on windows) to run the example.

The example is simple: the application will send a message to the source queue and consume the same message from the target queue.

The bridge was configured in jms-bridge-jboss-beans.xml to bridge these two queues.

  1. First we need to get an initial context so we can look up the JMS resources
  2.              initialContext = new InitialContext();
             
  3. We look up the JMS ConnectionFactory
  4.              ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
             

    First, we will send a message to the source queue.

  5. We look up the JMS source queue
  6.              Queue sourceQueue = (Queue)initialContext.lookup("/queue/source");
             
  7. We create a JMS connection, a session and a message producer for the source queue
  8.              sourceConnection = cf.createConnection();
                 Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                 MessageProducer sourceProducer = sourceSession.createProducer(sourceQueue);
             
  9. We create and send a message to the source queue. We also display its Message ID.
  10.              TextMessage message = sourceSession.createTextMessage("this is a text message");
                 sourceProducer.send(message);
                 System.out.format("Sent message to %s: %s\n",
                                   ((Queue)message.getJMSDestination()).getQueueName(),
                                   message.getText());
                 System.out.format("Message ID : %s\n", message.getJMSMessageID());
             
  11. We close the source connection
  12.              sourceConnection.close();
             

    Now that a message has been sent to the source queue, we will consume a message from the target queue.
    If the bridge runs correctly, it will have consumed the message from the source and resent it to the target so that we can consume a message from it.

  13. We look up the JMS target queue
  14.              Queue targetQueue = (Queue)initialContext.lookup("/queue/target");
             
  15. We create a connection, a session and a message consumer for the target queue
  16.              targetConnection = cf.createConnection();
                 Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                 MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);
             
  17. We start the JMS connection to receive messages from the target
  18.              targetConnection.start();
             
  19. We receive a message from the target queue. It has the same content than the message sent to the source queue
  20.              TextMessage messageReceived = (TextMessage)consumer.receive(5000);
                 System.out.println("Received message: " + messageReceived.getText() +
                                             " (" +  messageReceived.getJMSMessageID() + ")");
                 
             
  21. We now display the received message ID. It is not the same than the ID of the message sent to the source queue. The message received from the target queue was sent by the bridge, not by the source message producer
  22.              System.out.format("Message ID         : %s\n", messageReceived.getJMSMessageID());
             
  23. If you need to retrieve the message ID of the message sent to the source, you can use the property HQ_BRIDGE_MSG_ID_LIST
  24.              System.out.format("Bridged Message ID : %s\n", messageReceived.getStringProperty("HQ_BRIDGE_MSG_ID_LIST"));
             
  25. And finally, always remember to close the JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  26.              finally
                 {
                    if (initialContext != null)
                    {
                       initialContext.close();
                    }
                    if (sourceConnection != null)
                    {
                       sourceConnection.close();
                    }
                    if (targetConnection != null)
                    {
                       targetConnection.close();
                    }     
                 }
              

More information