Repository: activemq-artemis Updated Branches: refs/heads/master 6b74e2ce8 -> 54ada0e7a
ARTEMIS-1815 adding exclusive-queue 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/6df09d6e Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/6df09d6e Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/6df09d6e Branch: refs/heads/master Commit: 6df09d6e12cc0991285bcdd1dc39dd8d0b558167 Parents: 6b74e2c Author: Pat Fox <[email protected]> Authored: Tue Apr 17 16:22:10 2018 +0200 Committer: Clebert Suconic <[email protected]> Committed: Tue Apr 17 16:58:30 2018 -0400 ---------------------------------------------------------------------- .../src/test/scripts/run-examples.sh | 1 + docs/user-manual/en/examples.md | 6 + docs/user-manual/en/exclusive-queues.md | 4 +- .../features/standard/exclusive-queue/pom.xml | 134 +++++++++++++++++++ .../features/standard/exclusive-queue/readme.md | 30 +++++ .../ExclusiveQueueClientSideExample.java | 108 +++++++++++++++ .../jms/example/ExclusiveQueueExample.java | 86 ++++++++++++ .../main/resources/activemq/server0/broker.xml | 62 +++++++++ examples/features/standard/pom.xml | 1 + 9 files changed, 431 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/artemis-distribution/src/test/scripts/run-examples.sh ---------------------------------------------------------------------- diff --git a/artemis-distribution/src/test/scripts/run-examples.sh b/artemis-distribution/src/test/scripts/run-examples.sh index e0051c0..8f2910f 100755 --- a/artemis-distribution/src/test/scripts/run-examples.sh +++ b/artemis-distribution/src/test/scripts/run-examples.sh @@ -46,6 +46,7 @@ cd divert; mvn verify; cd .. cd durable-subscription; mvn verify; cd .. cd embedded; mvn verify; cd .. cd embedded-simple; mvn verify; cd .. +cd exclusive-queue; mvn verify; cd .. cd expiry; mvn verify; cd .. cd http-transport; mvn verify; cd .. cd instantiate-connection-factory; mvn verify; cd .. http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/docs/user-manual/en/examples.md ---------------------------------------------------------------------- diff --git a/docs/user-manual/en/examples.md b/docs/user-manual/en/examples.md index 251eeaa..1f76dd7 100644 --- a/docs/user-manual/en/examples.md +++ b/docs/user-manual/en/examples.md @@ -411,6 +411,12 @@ Embedded Simple The `embedded-simple` example shows how to embed a broker within your own code using regular Apache ActiveMQ Artemis XML files. +Exclusive Queue +--------------- + +The `exlusive-queue` example shows you how to use Exclusive Queues, that +route all messages to only one consumer at a time. + Message Expiration ------------------ http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/docs/user-manual/en/exclusive-queues.md ---------------------------------------------------------------------- diff --git a/docs/user-manual/en/exclusive-queues.md b/docs/user-manual/en/exclusive-queues.md index 6529c9d..3dc751c 100644 --- a/docs/user-manual/en/exclusive-queues.md +++ b/docs/user-manual/en/exclusive-queues.md @@ -54,4 +54,6 @@ to configure exclusive queues for a set of addresses (see [here](wildcard-syntax ## Example -See `org.apache.activemq.artemis.tests.integration.jms.client.ExclusiveTest` +See `Exclusive Queue` in [examples](examples.md). + +For additional examples see `org.apache.activemq.artemis.tests.integration.jms.client.ExclusiveTest` http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/exclusive-queue/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/exclusive-queue/pom.xml b/examples/features/standard/exclusive-queue/pom.xml new file mode 100644 index 0000000..7fbab83 --- /dev/null +++ b/examples/features/standard/exclusive-queue/pom.xml @@ -0,0 +1,134 @@ +<?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>2.6.0-SNAPSHOT</version> + </parent> + + <artifactId>exclusive-queue</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Exclusive Queue Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-client-all</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> + <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>runClient1</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.ExclusiveQueueExample</clientClass> + </configuration> + </execution> + <execution> + <id>runClient2</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.ExclusiveQueueClientSideExample</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>exclusive-queue</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-clean-plugin</artifactId> + </plugin> + </plugins> + </build> + <profiles> + <profile> + <id>release</id> + <build> + <plugins> + <plugin> + <groupId>com.vladsch.flexmark</groupId> + <artifactId>markdown-page-generator-plugin</artifactId> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/exclusive-queue/readme.md ---------------------------------------------------------------------- diff --git a/examples/features/standard/exclusive-queue/readme.md b/examples/features/standard/exclusive-queue/readme.md new file mode 100644 index 0000000..bcaa7c7 --- /dev/null +++ b/examples/features/standard/exclusive-queue/readme.md @@ -0,0 +1,30 @@ +# JMS Exclusive Queue Example + +To run the example, simply type **mvn verify** from this directory, or **mvn -PnoServer verify** if you want to start and create the broker manually. + +This example shows you how to configure ActiveMQ Artemis so all messages are delivered to the same consumer + +## ExclusiveQueueExample.java + +The broker is configured (using 'address-settings'), so that the queue is exclusive and will deliver all messages to the same +consumer + +```xml + <address-settings> + <address-setting match="my.exclusive.queue"> + <default-exclusive-queue>true</default-exclusive-queue> + </address-setting> + </address-settings> +``` + + +## ExclusiveQueueClientSideExample.java + +The JMS Queue is auto created from the client code and uses the `exclusive` parameter. + +```java +Queue queue = session.createQueue("client.side.exclusive.queue?exclusive=true"); +``` + +This example also shows that all remaining messages are sent to another consumer when the first consumer (that was receiving +all messages), is closed. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueClientSideExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueClientSideExample.java b/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueClientSideExample.java new file mode 100644 index 0000000..eb5f7d9 --- /dev/null +++ b/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueClientSideExample.java @@ -0,0 +1,108 @@ +/* + * 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.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + +/** + * example shows how to specify Exclusive Queue when auto creating the Queue from client. + * <p> + * Step 11 & 12 also shows that messages will be sent to consumer2 after consumer1 is closed (consumer1 is receiving + * all messages before it is closed) + */ + +public class ExclusiveQueueClientSideExample { + + public static void main(final String[] args) throws Exception { + + // Step 1. Create a JMS Connection factory + ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + + // Step 2. Create a JMS Connection + try (Connection connection = connectionFactory.createConnection()) { + + //Step 3. Create a JMS Session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + //Step 4. Create a Queue Object + Queue queue = session.createQueue("client.side.exclusive.queue?exclusive=true"); + + //Step 5. Create a JMS producer + MessageProducer producer = session.createProducer(queue); + + //Step 6. Create 2 consumers on the queue + MessageConsumer consumer1 = session.createConsumer(queue); + MessageConsumer consumer2 = session.createConsumer(queue); + MessageConsumer consumer3 = session.createConsumer(queue); + + //Step 7. Start the connection + connection.start(); + + //Step 8. send 30 text messages + Message message = session.createTextMessage("My Message"); + for (int i = 0; i < 30; i++) { + producer.send(message); + } + + //Step 9. ensure consumer1 gets first 20 + for (int i = 0; i < 20; i++) { + Message consumer1Message = consumer1.receive(1000); + if (consumer1Message == null) { + throw new RuntimeException("Example FAILED - 'consumer1' should have received 20 messages"); + } + } + + System.out.println(ExclusiveQueueClientSideExample.class.getName() + " 'consumer1' received 20 messages as expected"); + + //Step 10. ensure consumer2 gets no messages yet! + Message consumer2Message = consumer2.receive(1000); + if (consumer2Message != null) { + throw new RuntimeException("Example FAILED - 'consumer2' should have not received any Messages yet!"); + } + + //Step 11. close consumer1 + consumer1.close(); + + //Step 12. ensure consumer2 receives remaining messages + for (int i = 0; i < 10; i++) { + consumer2Message = consumer2.receive(500); + if (consumer2Message == null) { + throw new RuntimeException("Example FAILED - 'consumer2' should have received 10 messages" + "after consumer1 has been closed"); + } + } + + System.out.println(ExclusiveQueueClientSideExample.class.getName() + " 'consumer2' received 10 messages " + "as expected, after 'consumer1' has been closed"); + + //Step 13. ensure consumer3 gets no messages yet! + Message consumer3Message = consumer3.receive(500); + if (consumer3Message != null) { + throw new RuntimeException("Example FAILED - 'consumer3' should have not received any Messages yet!"); + } + + System.out.println(ExclusiveQueueClientSideExample.class.getName() + " 'consumer3' received 0 messages " + "as expected"); + + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueExample.java b/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueExample.java new file mode 100644 index 0000000..50e25b7 --- /dev/null +++ b/examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueExample.java @@ -0,0 +1,86 @@ +/* + * 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.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + +/** + * example showing Exclusive Queue, broker is configured with "default-exclusive-queue" to true for the matching + * address setting. All messages from that queue are routed to the same consumer + */ + +public class ExclusiveQueueExample { + + public static void main(final String[] args) throws Exception { + + // Step 1. Create a JMS Connection factory + ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + + // Step 2. Create a JMS Connection + try (Connection connection = connectionFactory.createConnection()) { + + //Step 3. Create a JMS Session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + //Step 4. Create a Queue Object + Queue queue = session.createQueue("my.exclusive.queue"); + + //Step 5. Create a JMS producer + MessageProducer producer = session.createProducer(queue); + + //Step 6. Create 2 consumers on the queue + MessageConsumer consumer1 = session.createConsumer(queue); + MessageConsumer consumer2 = session.createConsumer(queue); + + //Step 7. Start the connection + connection.start(); + + //Step 8. send 10 text messages + Message message = session.createTextMessage("My Message"); + for (int i = 0; i < 10; i++) { + producer.send(message); + } + + //Step 9. ensure consumer1 gets all 10 messages + for (int i = 0; i < 10; i++) { + Message consumer1Message = consumer1.receive(1000); + if (consumer1Message == null) { + throw new RuntimeException("Example FAILED - 'consumer1' should have received all 10 messages"); + } + } + + System.out.println(ExclusiveQueueExample.class.getName() + " 'consumer1' received 10 messages as expected"); + + //Step10. ensure consumer2 gets no messages + Message consumer2Message = consumer2.receive(1000); + if (consumer2Message != null) { + throw new RuntimeException("Example FAILED - 'consumer2' should have not received any Messages"); + } + + System.out.println(ExclusiveQueueExample.class.getName() + " 'consumer2' received 0 messages as expected"); + + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml b/examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..2a5ee0e --- /dev/null +++ b/examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,62 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- +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="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd"> + <core xmlns="urn:activemq:core"> + + <name>ExclusiveQueueExample</name> + + <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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <security-setting match="#"> + <permission type="createNonDurableQueue" roles="guest"/> + <permission type="deleteNonDurableQueue" roles="guest"/> + <permission type="createDurableQueue" roles="guest"/> + <permission type="deleteDurableQueue" roles="guest"/> + <permission type="createAddress" roles="guest"/> + <permission type="deleteAddress" roles="guest"/> + <permission type="consume" roles="guest"/> + <permission type="browse" roles="guest"/> + <permission type="send" roles="guest"/> + </security-setting> + </security-settings> + + <address-settings> + <address-setting match="my.exclusive.queue"> + <default-exclusive-queue>true</default-exclusive-queue> + </address-setting> + </address-settings> + + </core> +</configuration> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6df09d6e/examples/features/standard/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/pom.xml b/examples/features/standard/pom.xml index 7bb1eac..11bfe80 100644 --- a/examples/features/standard/pom.xml +++ b/examples/features/standard/pom.xml @@ -57,6 +57,7 @@ under the License. <module>durable-subscription</module> <module>embedded</module> <module>embedded-simple</module> + <module>exclusive-queue</module> <module>expiry</module> <module>http-transport</module> <module>interceptor</module>
