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.
To run the example, simply type mvn verify
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext(0);
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(topic);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message [" + messageReceived.getText() + "] with String property: " + messageReceived.getStringProperty("newproperty"));
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}
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;
}
}