ARTEMIS-1815 adding exclusive-queue example

This commit is contained in:
Pat Fox 2018-04-17 16:22:10 +02:00 committed by Clebert Suconic
parent 6b74e2ce8e
commit 6df09d6e12
9 changed files with 431 additions and 1 deletions

View File

@ -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 ..

View File

@ -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
------------------

View File

@ -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`

View File

@ -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>

View File

@ -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.

View File

@ -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");
}
}
}

View File

@ -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");
}
}
}

View File

@ -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>

View File

@ -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>