diff --git a/examples/features/standard/interceptor-client/pom.xml b/examples/features/standard/interceptor-client/pom.xml new file mode 100644 index 0000000000..5dd979c334 --- /dev/null +++ b/examples/features/standard/interceptor-client/pom.xml @@ -0,0 +1,110 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.1.1-SNAPSHOT + + + interceptor-client + jar + ActiveMQ Artemis JMS Interceptor Example + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + org.apache.activemq.examples.broker:interceptor:${project.version} + ${noServer} + ${basedir}/target/classes/activemq/server0 + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.InterceptorExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + interceptor-client + ${project.version} + + + + + + + diff --git a/examples/features/standard/interceptor-client/readme.html b/examples/features/standard/interceptor-client/readme.html new file mode 100644 index 0000000000..42b1e18570 --- /dev/null +++ b/examples/features/standard/interceptor-client/readme.html @@ -0,0 +1,72 @@ + + + + + ActiveMQ Artemis JMS Interceptor Example + + + + + +

JMS Interceptor Example

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ + +

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

+ +

ActiveMQ Artemis 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 broker.xml, as follows:

+
+     
+        <configuration>
+        ...
+           <remoting-incoming-interceptors>
+              <class-name>org.apache.activemq.artemis.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.

+ + + + + + + + diff --git a/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/InterceptorExample.java b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/InterceptorExample.java new file mode 100644 index 0000000000..e71f3fdd7f --- /dev/null +++ b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/InterceptorExample.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.artemis.jms.example; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + +/** + * A simple JMS example that shows how to implement and use interceptors with ActiveMQ Artemis. + */ +public class InterceptorExample { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + try { + ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?incomingInterceptorList=" + SimpleInterceptor.class.getName()); + connection = cf.createConnection(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + Queue queue = session.createQueue("exampleQueue"); + + MessageProducer producer = session.createProducer(queue); + + TextMessage message = session.createTextMessage("This is a text message"); + + System.out.println("Sending message [" + message.getText() + + "] with String property: " + + message.getStringProperty("newproperty")); + + producer.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")); + + if (messageReceived.getStringProperty("newproperty") == null) { + throw new IllegalStateException("Check your configuration as the example interceptor wasn't actually called!"); + } + } + finally { + if (connection != null) { + connection.close(); + } + } + } +} diff --git a/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/SimpleInterceptor.java b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/SimpleInterceptor.java new file mode 100644 index 0000000000..5d00f37900 --- /dev/null +++ b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/SimpleInterceptor.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.artemis.jms.example; + +import org.apache.activemq.artemis.api.core.ActiveMQException; +import org.apache.activemq.artemis.api.core.Interceptor; +import org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.core.protocol.core.Packet; +import org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionReceiveMessage; +import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; + +/** + * A simple Interceptor implementation + */ +public class SimpleInterceptor implements Interceptor { + + public boolean intercept(final Packet packet, final 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 SessionReceiveMessage) { + SessionReceiveMessage realPacket = (SessionReceiveMessage) packet; + Message msg = realPacket.getMessage(); + msg.putStringProperty(new SimpleString("newproperty"), new SimpleString("Hello from interceptor!")); + } + // We return true which means "call next interceptor" (if there is one) or target. + // If we returned false, it means "abort call" - no more interceptors would be called and neither would + // the target + return true; + } + +} diff --git a/examples/features/standard/pom.xml b/examples/features/standard/pom.xml index 62c8cac9a7..fe56e8764f 100644 --- a/examples/features/standard/pom.xml +++ b/examples/features/standard/pom.xml @@ -54,6 +54,7 @@ under the License. expiry http-transport interceptor + interceptor-client instantiate-connection-factory jms-auto-closeable jms-bridge @@ -112,6 +113,7 @@ under the License. expiry http-transport interceptor + interceptor-client jms-auto-closeable instantiate-connection-factory jms-bridge