From 6df09d6e12cc0991285bcdd1dc39dd8d0b558167 Mon Sep 17 00:00:00 2001 From: Pat Fox Date: Tue, 17 Apr 2018 16:22:10 +0200 Subject: [PATCH] ARTEMIS-1815 adding exclusive-queue example --- .../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 ++++++++++++++++++ .../standard/exclusive-queue/readme.md | 30 ++++ .../ExclusiveQueueClientSideExample.java | 108 ++++++++++++++ .../jms/example/ExclusiveQueueExample.java | 86 +++++++++++ .../resources/activemq/server0/broker.xml | 62 ++++++++ examples/features/standard/pom.xml | 1 + 9 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 examples/features/standard/exclusive-queue/pom.xml create mode 100644 examples/features/standard/exclusive-queue/readme.md create mode 100644 examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueClientSideExample.java create mode 100644 examples/features/standard/exclusive-queue/src/main/java/org/apache/activemq/artemis/jms/example/ExclusiveQueueExample.java create mode 100644 examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml diff --git a/artemis-distribution/src/test/scripts/run-examples.sh b/artemis-distribution/src/test/scripts/run-examples.sh index e0051c09cb..8f2910fb30 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 .. diff --git a/docs/user-manual/en/examples.md b/docs/user-manual/en/examples.md index 251eeaa0c1..1f76dd7c1a 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 ------------------ diff --git a/docs/user-manual/en/exclusive-queues.md b/docs/user-manual/en/exclusive-queues.md index 6529c9db33..3dc751c3ad 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` diff --git a/examples/features/standard/exclusive-queue/pom.xml b/examples/features/standard/exclusive-queue/pom.xml new file mode 100644 index 0000000000..7fbab83506 --- /dev/null +++ b/examples/features/standard/exclusive-queue/pom.xml @@ -0,0 +1,134 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 2.6.0-SNAPSHOT + + + exclusive-queue + jar + ActiveMQ Artemis JMS Exclusive Queue Example + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client-all + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + ${basedir}/target/classes/activemq/server0 + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient1 + + runClient + + + org.apache.activemq.artemis.jms.example.ExclusiveQueueExample + + + + runClient2 + + runClient + + + org.apache.activemq.artemis.jms.example.ExclusiveQueueClientSideExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + exclusive-queue + ${project.version} + + + + + org.apache.maven.plugins + maven-clean-plugin + + + + + + release + + + + com.vladsch.flexmark + markdown-page-generator-plugin + + + + + + \ No newline at end of file diff --git a/examples/features/standard/exclusive-queue/readme.md b/examples/features/standard/exclusive-queue/readme.md new file mode 100644 index 0000000000..bcaa7c751a --- /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 + + + true + + +``` + + +## 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 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 0000000000..eb5f7d9abe --- /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. + *

+ * 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"); + + } + } +} 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 0000000000..50e25b756e --- /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"); + + } + } +} 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 0000000000..2a5ee0e3de --- /dev/null +++ b/examples/features/standard/exclusive-queue/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,62 @@ + + + + + + ExclusiveQueueExample + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + + + + true + + + + + diff --git a/examples/features/standard/pom.xml b/examples/features/standard/pom.xml index 7bb1eac6cb..11bfe8068b 100644 --- a/examples/features/standard/pom.xml +++ b/examples/features/standard/pom.xml @@ -57,6 +57,7 @@ under the License. durable-subscription embedded embedded-simple + exclusive-queue expiry http-transport interceptor