adding interceptor client example
This commit is contained in:
parent
e30b983b97
commit
1c067a5b96
|
@ -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>
|
|
@ -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>
|
||||||
|
<configuration>
|
||||||
|
...
|
||||||
|
<remoting-incoming-interceptors>
|
||||||
|
<class-name>org.apache.activemq.artemis.jms.example.SimpleInterceptor</class-name>
|
||||||
|
</remoting-incoming-interceptors>
|
||||||
|
...
|
||||||
|
</configuration>
|
||||||
|
</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>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -54,6 +54,7 @@ under the License.
|
||||||
<module>expiry</module>
|
<module>expiry</module>
|
||||||
<module>http-transport</module>
|
<module>http-transport</module>
|
||||||
<module>interceptor</module>
|
<module>interceptor</module>
|
||||||
|
<module>interceptor-client</module>
|
||||||
<module>instantiate-connection-factory</module>
|
<module>instantiate-connection-factory</module>
|
||||||
<module>jms-auto-closeable</module>
|
<module>jms-auto-closeable</module>
|
||||||
<module>jms-bridge</module>
|
<module>jms-bridge</module>
|
||||||
|
@ -112,6 +113,7 @@ under the License.
|
||||||
<module>expiry</module>
|
<module>expiry</module>
|
||||||
<module>http-transport</module>
|
<module>http-transport</module>
|
||||||
<module>interceptor</module>
|
<module>interceptor</module>
|
||||||
|
<module>interceptor-client</module>
|
||||||
<module>jms-auto-closeable</module>
|
<module>jms-auto-closeable</module>
|
||||||
<module>instantiate-connection-factory</module>
|
<module>instantiate-connection-factory</module>
|
||||||
<module>jms-bridge</module>
|
<module>jms-bridge</module>
|
||||||
|
|
Loading…
Reference in New Issue