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:
To run the example, you need to download JBoss AS 5.x and create a configuration for HornetQ.
Please refer to HornetQ Quickstart guide to install it in JBoss AS 5
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/
).
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.
initialContext = new InitialContext();
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
First, we will send a message to the source queue.
Queue sourceQueue = (Queue)initialContext.lookup("/queue/source");
sourceConnection = cf.createConnection(); Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer sourceProducer = sourceSession.createProducer(sourceQueue);
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());
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.
Queue targetQueue = (Queue)initialContext.lookup("/queue/target");
targetConnection = cf.createConnection(); Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);
targetConnection.start();
TextMessage messageReceived = (TextMessage)consumer.receive(5000); System.out.println("Received message: " + messageReceived.getText() + " (" + messageReceived.getJMSMessageID() + ")");
System.out.format("Message ID : %s\n", messageReceived.getJMSMessageID());
HQ_BRIDGE_MSG_ID_LIST
System.out.format("Bridged Message ID : %s\n", messageReceived.getStringProperty("HQ_BRIDGE_MSG_ID_LIST"));
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objectsfinally { if (initialContext != null) { initialContext.close(); } if (sourceConnection != null) { sourceConnection.close(); } if (targetConnection != null) { targetConnection.close(); } }