This example shows how to configure and run a JMS Bridge in WildFly.
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:
The JMS Bridge is configured in the "messaging" subsystem.
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/
).
download WildFly 8.0.0.Final from here and install.
set the JBOSS_HOME property to point to the WildFly install directory
type mvn verify
from the example directory to run
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 is configured in the messaging subsystem:
<jms-bridge name="myBridge"> <source> <connection-factory name="ConnectionFactory" /> <destination name="queue/sourceQueue" /> </source> <target> <connection-factory name="ConnectionFactory" /> <destination name="queue/targetQueue" /> </target> <quality-of-service>AT_MOST_ONCE</quality-of-service> <failure-retry-interval>1000</failure-retry-interval> <max-retries>7890</max-retries> <max-batch-size>1</max-batch-size> <max-batch-time>1000</max-batch-time> </jms-bridge>
final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); initialContext = new InitialContext(env);
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
First, we will send a message to the source queue.
Queue sourceQueue = (Queue)initialContext.lookup("jms/queues/sourceQueue");
sourceConnection = cf.createConnection("guest", "password"); 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("jms/queues/targetQueue");
targetConnection = cf.createConnection(); Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);
targetConnection.start();
TextMessage messageReceived = (TextMessage)targetConsumer.receive(15000); System.out.format("\nReceived from %s: %s\n", ((Queue)messageReceived.getJMSDestination()).getQueueName(), messageReceived.getText());
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(); } }