Improving into Stomp Embedded Interceptor example Using the regular maven plugin framework, and adding the interceptors to the regular broker.xml configuration
Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/8bb3d1ce Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/8bb3d1ce Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/8bb3d1ce Branch: refs/heads/master Commit: 8bb3d1ceaa5bd1b3138286c1777805535428d9f2 Parents: 6496be5 Author: Clebert Suconic <clebertsuco...@apache.org> Authored: Fri Sep 25 18:09:08 2015 -0400 Committer: Clebert Suconic <clebertsuco...@apache.org> Committed: Fri Sep 25 18:09:12 2015 -0400 ---------------------------------------------------------------------- examples/protocols/stomp/pom.xml | 2 + .../stomp/stomp-embedded-interceptor/pom.xml | 38 ++++++++++ .../artemis/jms/example/MyStompInterceptor.java | 39 ++++++++++ .../artemis/jms/example/RegularInterceptor.java | 37 ++++++++++ .../StompEmbeddedWithInterceptorExample.java | 75 +++++++------------ .../activemq/server0/artemis-roles.properties | 17 +++++ .../activemq/server0/artemis-users.properties | 17 +++++ .../main/resources/activemq/server0/broker.xml | 76 ++++++++++++++++++++ .../src/main/resources/broker.xml | 48 ------------- 9 files changed, 252 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/pom.xml ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/pom.xml b/examples/protocols/stomp/pom.xml index 179b082..a4ccd18 100644 --- a/examples/protocols/stomp/pom.xml +++ b/examples/protocols/stomp/pom.xml @@ -45,6 +45,7 @@ under the License. <modules> <module>stomp</module> <module>stomp-websockets</module> + <module>stomp-embedded-interceptor</module> <module>stomp1.1</module> <module>stomp1.2</module> </modules> @@ -53,6 +54,7 @@ under the License. <id>examples</id> <modules> <module>stomp</module> + <module>stomp-embedded-interceptor</module> <!-- has to be executed manually <module>stomp-websockets</module> --> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/pom.xml ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/pom.xml b/examples/protocols/stomp/stomp-embedded-interceptor/pom.xml index 22c726c..c8fd1d8 100644 --- a/examples/protocols/stomp/stomp-embedded-interceptor/pom.xml +++ b/examples/protocols/stomp/stomp-embedded-interceptor/pom.xml @@ -69,6 +69,32 @@ under the License. <artifactId>artemis-maven-plugin</artifactId> <executions> <execution> + <id>create</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <!-- need to install the jar for interceptors on the server --> + <libList><arg>org.apache.activemq.examples.stomp:stomp-embedded-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> @@ -77,6 +103,18 @@ under the License. <clientClass>org.apache.activemq.artemis.jms.example.StompEmbeddedWithInterceptorExample</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> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/MyStompInterceptor.java ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/MyStompInterceptor.java b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/MyStompInterceptor.java new file mode 100644 index 0000000..43b6633 --- /dev/null +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/MyStompInterceptor.java @@ -0,0 +1,39 @@ +/** + * 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.core.protocol.stomp.StompConnection; +import org.apache.activemq.artemis.core.protocol.stomp.StompFrame; +import org.apache.activemq.artemis.core.protocol.stomp.StompFrameInterceptor; +import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; + +public class MyStompInterceptor implements StompFrameInterceptor { + + public boolean intercept(StompFrame frame, RemotingConnection remotingConnection) + throws ActiveMQException { + + StompConnection connection = (StompConnection) remotingConnection; + + frame.addHeader("stompIntercepted", "Hello"); + + System.out.println("MyStompInterceptor Intercepted frame " + frame + " on connection " + connection); + + return true; + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/RegularInterceptor.java ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/RegularInterceptor.java b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/RegularInterceptor.java new file mode 100644 index 0000000..8457d68 --- /dev/null +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/RegularInterceptor.java @@ -0,0 +1,37 @@ +/** + * 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.core.protocol.core.Packet; +import org.apache.activemq.artemis.core.protocol.core.impl.wireformat.MessagePacket; +import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; + +public class RegularInterceptor implements Interceptor { + + @Override + public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException { + System.out.println("Regular Intercept on " + packet); + if (packet instanceof MessagePacket) { + MessagePacket messagePacket = (MessagePacket)packet; + messagePacket.getMessage().putStringProperty("regularIntercepted", "HelloAgain"); + } + return true; + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/StompEmbeddedWithInterceptorExample.java ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/StompEmbeddedWithInterceptorExample.java b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/StompEmbeddedWithInterceptorExample.java index e9a9156..420e66e 100644 --- a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/StompEmbeddedWithInterceptorExample.java +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/java/org/apache/activemq/artemis/jms/example/StompEmbeddedWithInterceptorExample.java @@ -16,67 +16,24 @@ */ package org.apache.activemq.artemis.jms.example; -import org.apache.activemq.artemis.api.core.ActiveMQException; -import org.apache.activemq.artemis.api.core.TransportConfiguration; -import org.apache.activemq.artemis.core.config.Configuration; -import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl; -import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration; -import org.apache.activemq.artemis.core.protocol.stomp.StompConnection; -import org.apache.activemq.artemis.core.protocol.stomp.StompFrame; -import org.apache.activemq.artemis.core.protocol.stomp.StompFrameInterceptor; -import org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptorFactory; -import org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS; -import org.apache.activemq.artemis.jms.server.config.ConnectionFactoryConfiguration; -import org.apache.activemq.artemis.jms.server.config.impl.ConnectionFactoryConfigurationImpl; -import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; -import org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManagerImpl; - import javax.jms.Connection; -import javax.jms.ConnectionFactory; +import javax.jms.Message; import javax.jms.MessageConsumer; -import javax.jms.MessageProducer; -import javax.jms.Queue; import javax.jms.Session; -import javax.jms.TextMessage; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.HashSet; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; /** * This example demonstrates how to run an ActiveMQ Artemis embedded with JMS */ public class StompEmbeddedWithInterceptorExample { - public static class MyStompInterceptor - implements StompFrameInterceptor { - - public boolean intercept(StompFrame frame, RemotingConnection remotingConnection) - throws ActiveMQException { - - StompConnection connection = (StompConnection) remotingConnection; - - System.out.println("Intercepted frame " + frame + " on connection " + connection); - - return false; - } - } - private static final String END_OF_FRAME = "\u0000"; public static void main(final String[] args) throws Exception { - EmbeddedJMS jmsServer = new EmbeddedJMS(); - String brokerConfigPath = StompEmbeddedWithInterceptorExample.class.getResource("/broker.xml").toString(); - jmsServer.setConfigResourcePath(brokerConfigPath); - - jmsServer.start(); - jmsServer.getActiveMQServer().getRemotingService().addIncomingInterceptor(new MyStompInterceptor()); - - System.out.println("Started Embedded JMS Server"); - // Step 1. Create a TCP socket to connect to the Stomp port Socket socket = new Socket("localhost", 61616); @@ -95,7 +52,7 @@ public class StompEmbeddedWithInterceptorExample { // jms.queue.exampleQueue address with a text body String text = "Hello World from Stomp 1.2 !"; String message = "SEND\n" + - "destination:jms.queue.exampleQueue\n" + + "destination:jms.queue.exampleQueue" + "\n" + text + END_OF_FRAME; @@ -111,8 +68,28 @@ public class StompEmbeddedWithInterceptorExample { // Step 5. Slose the TCP socket socket.close(); - Thread.sleep(1000); - jmsServer.stop(); + + // It will use a regular JMS connection to show how the injected data will appear at the final message + + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); + Connection connection = factory.createConnection(); + Session session = connection.createSession(); + connection.start(); + + MessageConsumer consumer = session.createConsumer(session.createQueue("exampleQueue")); + Message messageReceived = consumer.receive(5000); + + String propStomp = messageReceived.getStringProperty("stompIntercepted"); + + String propRegular = messageReceived.getStringProperty("regularIntercepted"); + + System.out.println("propStomp is Hello!! - " + propStomp.equals("Hello")); + System.out.println("propRegular is HelloAgain!! - " + propRegular.equals("HelloAgain")); + + session.close(); + connection.close(); + + factory.close(); } private static void sendFrame(Socket socket, String data) throws Exception { http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-roles.properties ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-roles.properties b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-roles.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-roles.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-users.properties ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-users.properties b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-users.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/artemis-users.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/broker.xml b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..132bcec --- /dev/null +++ b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,76 @@ +<?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. +--> + +<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="urn:activemq" + xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd"> + + <jms xmlns="urn:activemq:jms"> + <!--the queue used by the example--> + <queue name="exampleQueue"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>./data/bindings</bindings-directory> + + <journal-directory>./data/journal</journal-directory> + + <large-messages-directory>./data/largemessages</large-messages-directory> + + <paging-directory>./data/paging</paging-directory> + + <queues> + <queue name="jms"> + <address></address> + </queue> + </queues> + + + <remoting-incoming-interceptors> + <class-name>org.apache.activemq.artemis.jms.example.MyStompInterceptor</class-name> + <class-name>org.apache.activemq.artemis.jms.example.RegularInterceptor</class-name> + </remoting-incoming-interceptors> + + <remoting-outgoing-interceptors> + <class-name>org.apache.activemq.artemis.jms.example.RegularInterceptor</class-name> + </remoting-outgoing-interceptors> + + <!-- Acceptors --> + <acceptors> + <acceptor name="artemis">tcp://0.0.0.0:61616?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="#"> + <permission type="createDurableQueue" roles="guest"/> + <permission type="deleteDurableQueue" roles="guest"/> + <permission type="createNonDurableQueue" roles="guest"/> + <permission type="deleteNonDurableQueue" roles="guest"/> + <permission type="consume" roles="guest"/> + <permission type="send" roles="guest"/> + </security-setting> + </security-settings> + + </core> +</configuration> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/8bb3d1ce/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/broker.xml ---------------------------------------------------------------------- diff --git a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/broker.xml b/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/broker.xml deleted file mode 100644 index 474a897..0000000 --- a/examples/protocols/stomp/stomp-embedded-interceptor/src/main/resources/broker.xml +++ /dev/null @@ -1,48 +0,0 @@ -<?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. ---> - -<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns="urn:activemq" - xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd"> - - <core xmlns="urn:activemq:core"> - - <persistence-enabled>false</persistence-enabled> - - <acceptors> - <acceptor name="artemis">tcp://127.0.0.1:61616?protocols=STOMP</acceptor> - </acceptors> - - <!-- Other config --> - - <security-settings> - <!--security for example queue--> - <security-setting match="#"> - <permission type="createDurableQueue" roles="guest"/> - <permission type="deleteDurableQueue" roles="guest"/> - <permission type="createNonDurableQueue" roles="guest"/> - <permission type="deleteNonDurableQueue" roles="guest"/> - <permission type="consume" roles="guest"/> - <permission type="send" roles="guest"/> - </security-setting> - </security-settings> - - </core> -</configuration>