activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [1/6] activemq-artemis git commit: adding interceptor client example
Date Fri, 09 Oct 2015 02:55:52 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master e30b983b9 -> fcf18a764


adding interceptor client example


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/1c067a5b
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/1c067a5b
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/1c067a5b

Branch: refs/heads/master
Commit: 1c067a5b9642cc19ed07df364e0d1a81528fddbb
Parents: e30b983
Author: Clebert Suconic <clebertsuconic@apache.org>
Authored: Wed Oct 7 22:38:28 2015 -0400
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Thu Oct 8 14:54:47 2015 -0400

----------------------------------------------------------------------
 .../standard/interceptor-client/pom.xml         | 110 +++++++++++++++++++
 .../standard/interceptor-client/readme.html     |  72 ++++++++++++
 .../artemis/jms/example/InterceptorExample.java |  74 +++++++++++++
 .../artemis/jms/example/SimpleInterceptor.java  |  48 ++++++++
 examples/features/standard/pom.xml              |   2 +
 5 files changed, 306 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/1c067a5b/examples/features/standard/interceptor-client/pom.xml
----------------------------------------------------------------------
diff --git a/examples/features/standard/interceptor-client/pom.xml b/examples/features/standard/interceptor-client/pom.xml
new file mode 100644
index 0000000..5dd979c
--- /dev/null
+++ b/examples/features/standard/interceptor-client/pom.xml
@@ -0,0 +1,110 @@
+<?xml version='1.0'?>
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+   <modelVersion>4.0.0</modelVersion>
+
+   <parent>
+      <groupId>org.apache.activemq.examples.broker</groupId>
+      <artifactId>jms-examples</artifactId>
+      <version>1.1.1-SNAPSHOT</version>
+   </parent>
+
+   <artifactId>interceptor-client</artifactId>
+   <packaging>jar</packaging>
+   <name>ActiveMQ Artemis JMS Interceptor Example</name>
+
+   <properties>
+      <activemq.basedir>${project.basedir}/../../../..</activemq.basedir>
+   </properties>
+
+   <dependencies>
+      <dependency>
+         <groupId>org.apache.activemq</groupId>
+         <artifactId>artemis-jms-client</artifactId>
+         <version>${project.version}</version>
+      </dependency>
+   </dependencies>
+
+   <build>
+      <plugins>
+         <plugin>
+            <groupId>org.apache.activemq</groupId>
+            <artifactId>artemis-maven-plugin</artifactId>
+            <executions>
+               <execution>
+                  <id>create</id>
+                  <goals>
+                     <goal>create</goal>
+                  </goals>
+                  <configuration>
+                     <libList><arg>org.apache.activemq.examples.broker:interceptor:${project.version}</arg></libList>
+                     <ignore>${noServer}</ignore>
+                     <configuration>${basedir}/target/classes/activemq/server0</configuration>
+                  </configuration>
+               </execution>
+               <execution>
+                  <id>start</id>
+                  <goals>
+                     <goal>cli</goal>
+                  </goals>
+                  <configuration>
+                     <ignore>${noServer}</ignore>
+                     <spawn>true</spawn>
+                     <testURI>tcp://localhost:61616</testURI>
+                     <args>
+                        <param>run</param>
+                     </args>
+                  </configuration>
+               </execution>
+               <execution>
+                  <id>runClient</id>
+                  <goals>
+                     <goal>runClient</goal>
+                  </goals>
+                  <configuration>
+                     <clientClass>org.apache.activemq.artemis.jms.example.InterceptorExample</clientClass>
+                  </configuration>
+               </execution>
+               <execution>
+                  <id>stop</id>
+                  <goals>
+                     <goal>cli</goal>
+                  </goals>
+                  <configuration>
+                     <ignore>${noServer}</ignore>
+                     <args>
+                        <param>stop</param>
+                     </args>
+                  </configuration>
+               </execution>
+            </executions>
+            <dependencies>
+               <dependency>
+                  <groupId>org.apache.activemq.examples.broker</groupId>
+                  <artifactId>interceptor-client</artifactId>
+                  <version>${project.version}</version>
+               </dependency>
+            </dependencies>
+         </plugin>
+      </plugins>
+   </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/1c067a5b/examples/features/standard/interceptor-client/readme.html
----------------------------------------------------------------------
diff --git a/examples/features/standard/interceptor-client/readme.html b/examples/features/standard/interceptor-client/readme.html
new file mode 100644
index 0000000..42b1e18
--- /dev/null
+++ b/examples/features/standard/interceptor-client/readme.html
@@ -0,0 +1,72 @@
+<!--
+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.
+-->
+
+<html>
+  <head>
+    <title>ActiveMQ Artemis JMS Interceptor Example</title>
+    <link rel="stylesheet" type="text/css" href="../../../common/common.css" />
+    <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" />
+    <script type="text/javascript" src="../../../common/prettify.js"></script>
+  </head>
+  <body onload="prettyPrint()">
+     <h1>JMS Interceptor Example</h1>
+
+     <pre>To run the example, simply type <b>mvn verify</b> from this directory,
<br>or <b>mvn -PnoServer verify</b> if you want to start and create the
server manually.</pre>
+
+
+     <p>This example shows you how to implement and configure a simple incoming, server-side
interceptor with ActiveMQ Artemis.</p>
+
+     <p>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: </p>
+     <pre class="prettyprint">
+     <code>
+         public interface Interceptor
+         {
+            boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException;
+         }
+     </code>
+     </pre>
+     <p>Once you have your own interceptor class, add it to the broker.xml, as follows:</p>
+     <pre class="prettyprint">
+     <code>
+        &lt;configuration&gt;
+        ...
+           &lt;remoting-incoming-interceptors&gt;
+              &lt;class-name&gt;org.apache.activemq.artemis.jms.example.SimpleInterceptor&lt;/class-name&gt;
+           &lt;/remoting-incoming-interceptors&gt;
+        ...
+        &lt;/configuration&gt;
+     </code>
+     </pre>
+
+     <p>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.</p>
+
+     <p>With our interceptor we always return <code>true</code> from the
<code>intercept</code> method. If we were
+     to return <code>false</code> that signifies that no more interceptors are
to run or the target
+     is not to be called. Return <code>false</code> to abort processing of the
packet.</p>
+  </body>
+</html>
+
+
+
+
+
+

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/1c067a5b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/InterceptorExample.java
----------------------------------------------------------------------
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 0000000..e71f3fd
--- /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();
+         }
+      }
+   }
+}

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/1c067a5b/examples/features/standard/interceptor-client/src/main/java/org/apache/activemq/artemis/jms/example/SimpleInterceptor.java
----------------------------------------------------------------------
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 0000000..5d00f37
--- /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;
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/1c067a5b/examples/features/standard/pom.xml
----------------------------------------------------------------------
diff --git a/examples/features/standard/pom.xml b/examples/features/standard/pom.xml
index 62c8cac..fe56e87 100644
--- a/examples/features/standard/pom.xml
+++ b/examples/features/standard/pom.xml
@@ -54,6 +54,7 @@ under the License.
             <module>expiry</module>
             <module>http-transport</module>
             <module>interceptor</module>
+            <module>interceptor-client</module>
             <module>instantiate-connection-factory</module>
             <module>jms-auto-closeable</module>
             <module>jms-bridge</module>
@@ -112,6 +113,7 @@ under the License.
             <module>expiry</module>
             <module>http-transport</module>
             <module>interceptor</module>
+            <module>interceptor-client</module>
             <module>jms-auto-closeable</module>
             <module>instantiate-connection-factory</module>
             <module>jms-bridge</module>


Mime
View raw message