JMS Interceptor Example

This example shows you how to implement and configure a simple incoming, server-side interceptor with ActiveMQ.

ActiveMQ allows an application to use an interceptor to hook into the messaging system. All that needs to do is to implement the Interceptor interface, as defined below:

     
         public interface Interceptor
         {   
            boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException;
         }
     
     

Once you have your own interceptor class, add it to the activemq-configuration.xml, as follows:

     
        <configuration>
        ...
           <remoting-incoming-interceptors>
              <class-name>org.apache.activemq.jms.example.SimpleInterceptor</class-name>
           </remoting-incoming-interceptors>
        ...
        </configuration>
     
     

With interceptor, you can handle various events in message processing. In this example, a simple interceptor, SimpleInterceptor, is implemented and configured. When the example is running, the interceptor will print out each events that are passed in the interceptor. And it will add a string property to the message being delivered. You can see that after the message is received, there will be a new string property appears in the received message.

With our interceptor we always return true from the intercept method. If we were to return false that signifies that no more interceptors are to run or the target is not to be called. Return false to abort processing of the packet.

Example step-by-step

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

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            InitialContext initialContext = getContext(0);
            
  3. We look-up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We look-up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12.           MessageProducer messageProducer = session.createProducer(topic);
           
  13. We create a JMS text message that we are going to send.
  14.            TextMessage message = session.createTextMessage("This is a text message");
            
  15. We send message to the queue
  16.            messageProducer.send(message);
            
  17. We create a JMS Message Consumer to receive the message.
  18.            MessageConsumer messageConsumer = session.createConsumer(queue);
            
  19. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  20.            connection.start();
            
  21. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  22.            TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
            
  23. We print out the message and the new property that has been added by the interceptor
  24.            System.out.println("Received message [" + messageReceived.getText() + "] with String property: " + messageReceived.getStringProperty("newproperty"));
            
  25. And finally, always remember to close your 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 (connection != null)
                  {
                     connection.close();
                  }
               }
               
            
  1. The SimpleInterceptor:
  2.            
               public class SimpleInterceptor implements Interceptor
               {
                  public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException
                  {
                     System.out.println("SimpleInterceptor gets called!");
                     System.out.println("Packet: " + packet.getClass().getName());
                     System.out.println("RemotingConnection: " + connection.getRemoteAddress());
    
                     if (packet instanceof SessionSendMessage)
                     {
                        SessionSendMessage realPacket = (SessionSendMessage)packet;
                        Message msg = realPacket.getServerMessage();
                        msg.putStringProperty(new SimpleString("newproperty"), new SimpleString("Hello from interceptor!"));
                     }
                     return true;
                  }
               }