Core Bridge Example

This example demonstrates a core bridge deployed on one server, which consumes messages from a local queue and forwards them to an address on a second server.

Core bridges are used to create message flows between any two ActiveMQ servers which are remotely separated. Core bridges are resilient and will cope with temporary connection failure allowing them to be an ideal choice for forwarding over unreliable connections, e.g. a WAN.

They can also be configured with an optional filter expression, and will only forward messages that match that filter.

Furthermore they can be configured to use an optional Transformer class. A user-defined Transformer class can be specified which is called at forwarding time. This gives the user the opportunity to transform the message in some ways, e.g. changing its properties or body

ActiveMQ also includes a JMS Bridge. This is similar to a core bridge, but uses the JMS API and can be used to bridge between any two JMS 1.1 compliant messaging systems. The core bridge is limited to bridging between ActiveMQ instances, but may provide better performance than the JMS bridge. The JMS bridge is covered in a separate example.

For more information on bridges, please see the ActiveMQ user manual.

In this example we will demonstrate a simple sausage factory for aardvarks.

We have a JMS queue on server 0 named sausage-factory, and we have a JMS queue on server 1 named mincing-machine

We want to forward any messages that are sent to the sausage-factory queue on server 0, to the mincing-machine on server 1.

We only want to make aardvark sausages, so we only forward messages where the property "name" is set to "aardvark". It is known that other things, such are Sasquatches are also sent to the sausage-factory and we want to reject those.

Moreover it is known that Aardvarks normally wear blue hats, and it's important that we only make sausages using Aardvarks with green hats, so on the way we are going transform the property "hat" from "green" to "blue".

Here's a snippet from activemq-configuration.xml showing the bridge configuration

     
     <bridge name="my-bridge">
          <queue-name>jms.queue.sausage-factory</queue-name>
          <forwarding-address>jms.queue.mincing-machine</forwarding-address>
          <filter string="name='aardvark'"/>
          <transformer-class-name>org.apache.activemq.jms.example.HatColourChangeTransformer</transformer-class-name>
          <reconnect-attempts>-1</reconnect-attempts>
          <static-connectors>
             <connector-ref>remote-connector</connector-ref>
          </static-connectors>
     </bridge>
     
     

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. We create an initial context for looking up JNDI on node 0
  2.            
       ic0 = getContext(0);
       
            
  3. We look up the sausage-factory queue from node 0
  4.            Queue sausageFactory = (Queue)ic0.lookup("/queue/sausage-factory");
            
  5. We look up a JMS ConnectionFactory object from node 0
  6.            ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
            
  7. We create an initial context for looking up JNDI on node 1
  8.            ic1 = getContext(1);
            
  9. We look up the mincing-machine queue on node 1
  10.            Queue mincingMachine = (Queue)ic1.lookup("/queue/mincing-machine");
               
            
  11. We look up a JMS ConnectionFactory object from node 1
  12.           
       ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
              
            
  13. We create a JMS Connection connection0 which is a connection to server 0
  14.           
       connection0 = cf0.createConnection();
              
            
  15. We create a JMS Connection connection1 which is a connection to server 1
  16.           
       connection1 = cf1.createConnection();
              
            
  17. We create a JMS Session on server 0
  18.            
       Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
               
            
  19. We create a JMS Session on server 1
  20.            
       Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
                
            
  21. We start the connection to ensure delivery occurs on them
  22.            
       connection1.start();
               
            
  23. We create a JMS MessageConsumer object on server 1
  24.            
       MessageConsumer consumer = session1.createConsumer(mincingMachine);
               
            
  25. We create a JMS MessageProducer object on server 0.
  26.            
       MessageProducer producer = session0.createProducer(sausageFactory);
            
  27. We create and send a message representing an aardvark with a green hat to the sausage-factory on node 0
  28.            
             Message message = session0.createMessage();
    
             message.setStringProperty("name", "aardvark");
    
             message.setStringProperty("hat", "green");
    
             producer.send(message);
               
            
  29. We successfully receive the aardvark message from the mincing-machine one node 1. The aardvark's hat is now blue since it has been transformed!
  30.            
            Message receivedMessage = consumer.receive(5000);
               
            
  31. We create and send another message, this time representing a sasquatch with a mauve hat to the sausage-factory on node 0. This won't be bridged to the mincing-machine since we only want aardvarks, not sasquatches.
  32.            
             message = session0.createMessage();
    
             message.setStringProperty("name", "sasquatch");
    
             message.setStringProperty("hat", "mauve");
    
             producer.send(message);
               
            
  33. We don't receive the sasquatch message since it's not an aardvark!
  34.            
             receivedMessage = (TextMessage)consumer.receive(1000);
               
            
  35. And finally (no pun intended), always remember to close your JMS resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  36.            
    	finally
    	{
    	      if (connection0 != null)
             {
                connection0.close();
             }
    
             if (connection1 != null)
             {
                connection1.close();
             }
    
             if (ic0 != null)
             {
                ic0.close();
             }
    
             if (ic1 != null)
             {
                ic1.close();
             }
    	}