http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/temp-queue/src/main/java/org/apache/activemq/artemis/jms/example/TemporaryQueueExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/temp-queue/src/main/java/org/apache/activemq/artemis/jms/example/TemporaryQueueExample.java b/examples/broker-features/standard/temp-queue/src/main/java/org/apache/activemq/artemis/jms/example/TemporaryQueueExample.java new file mode 100644 index 0000000..5747fae --- /dev/null +++ b/examples/broker-features/standard/temp-queue/src/main/java/org/apache/activemq/artemis/jms/example/TemporaryQueueExample.java @@ -0,0 +1,118 @@ +/* + * 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.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TemporaryQueue; +import javax.jms.TextMessage; +import javax.naming.InitialContext; + +/** + * A simple JMS example that shows how to use temporary queues. + */ +public class TemporaryQueueExample { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + InitialContext initialContext = null; + try { + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. Look-up the JMS connection factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 3. Create a JMS Connection + connection = cf.createConnection(); + + // Step 4. Start the connection + connection.start(); + + // Step 5. Create a JMS session with AUTO_ACKNOWLEDGE mode + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Create a Temporary Queue + TemporaryQueue tempQueue = session.createTemporaryQueue(); + + System.out.println("Temporary queue is created: " + tempQueue); + + // Step 7. Create a JMS message producer + MessageProducer messageProducer = session.createProducer(tempQueue); + + // Step 8. Create a text message + TextMessage message = session.createTextMessage("This is a text message"); + + // Step 9. Send the text message to the queue + messageProducer.send(message); + + System.out.println("Sent message: " + message.getText()); + + // Step 11. Create a message consumer + MessageConsumer messageConsumer = session.createConsumer(tempQueue); + + // Step 12. Receive the message from the queue + message = (TextMessage) messageConsumer.receive(5000); + + System.out.println("Received message: " + message.getText()); + + // Step 13. Close the consumer and producer + messageConsumer.close(); + messageProducer.close(); + + // Step 14. Delete the temporary queue + tempQueue.delete(); + + // Step 15. Create another temporary queue. + TemporaryQueue tempQueue2 = session.createTemporaryQueue(); + + System.out.println("Another temporary queue is created: " + tempQueue2); + + // Step 16. Close the connection. + connection.close(); + + // Step 17. Create a new connection. + connection = cf.createConnection(); + + // Step 18. Create a new session. + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 19. Try to access the tempQueue2 outside its lifetime + try { + messageConsumer = session.createConsumer(tempQueue2); + throw new Exception("Temporary queue cannot be accessed outside its lifecycle!"); + } + catch (JMSException e) { + System.out.println("Exception got when trying to access a temp queue outside its scope: " + e); + } + } + finally { + if (connection != null) { + // Step 20. Be sure to close our JMS resources! + connection.close(); + } + if (initialContext != null) { + // Step 21. Also close the initialContext! + initialContext.close(); + } + } + } +}
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/temp-queue/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/temp-queue/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/temp-queue/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..2dd2df9 --- /dev/null +++ b/examples/broker-features/standard/temp-queue/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,65 @@ +<?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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + <security-settings> + <!--security for example queues --> + <security-setting match="jms.queue.#"> + <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 for JMS temporary queue --> + <security-setting match="jms.tempqueue.#"> + <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/21bf4406/examples/broker-features/standard/temp-queue/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/temp-queue/src/main/resources/jndi.properties b/examples/broker-features/standard/temp-queue/src/main/resources/jndi.properties new file mode 100644 index 0000000..93537c4 --- /dev/null +++ b/examples/broker-features/standard/temp-queue/src/main/resources/jndi.properties @@ -0,0 +1,20 @@ +# 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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +queue.queue/exampleQueue=exampleQueue http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-hierarchies/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-hierarchies/pom.xml b/examples/broker-features/standard/topic-hierarchies/pom.xml new file mode 100644 index 0000000..e3fe3f8 --- /dev/null +++ b/examples/broker-features/standard/topic-hierarchies/pom.xml @@ -0,0 +1,109 @@ +<?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.0.1-SNAPSHOT</version> + </parent> + + <artifactId>topic-hierarchies</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Topic Hierarchies 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> + <ignore>${noServer}</ignore> + </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.TopicHierarchyExample</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>topic-hierarchies</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-hierarchies/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-hierarchies/readme.html b/examples/broker-features/standard/topic-hierarchies/readme.html new file mode 100644 index 0000000..0aa16ca --- /dev/null +++ b/examples/broker-features/standard/topic-hierarchies/readme.html @@ -0,0 +1,42 @@ +<!-- +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 Topic Hierarchy 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>Topic Hierarchy 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>ActiveMQ Artemis supports topic hierarchies. With a topic hierarchy you can register a subscriber with a wild-card + and that subscriber will receive any messages routed to an address that match the wildcard.</p> + <p>ActiveMQ Artemis wild-cards can use the character '#' which means "match any number of words", and + the character '*' which means "match a single word". Words are delimited by the character "."</p> + <p>For example if I subscribe using the wild-card "news.europe.#", then that would match messages sent to the addresses + "news.europe", "news.europe.sport" and "news.europe.entertainment", but it does not match messages sent to the + address "news.usa.wrestling"</p> + <p>For more information on the wild-card syntax please consult the user manual.</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-hierarchies/src/main/java/org/apache/activemq/artemis/jms/example/TopicHierarchyExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-hierarchies/src/main/java/org/apache/activemq/artemis/jms/example/TopicHierarchyExample.java b/examples/broker-features/standard/topic-hierarchies/src/main/java/org/apache/activemq/artemis/jms/example/TopicHierarchyExample.java new file mode 100644 index 0000000..0a720da --- /dev/null +++ b/examples/broker-features/standard/topic-hierarchies/src/main/java/org/apache/activemq/artemis/jms/example/TopicHierarchyExample.java @@ -0,0 +1,121 @@ +/* + * 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.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; + +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; + +/** + * This example demonstrates how a JMS TopicSubscriber can be created to subscribe to a wild-card Topic. + * + * For more information please see the readme.html + */ +public class TopicHierarchyExample { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + InitialContext initialContext = null; + try { + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 3. Perform a lookup on the Connection Factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4. Create a JMS Connection + connection = cf.createConnection(); + + // Step 5. Create a JMS Session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Instantiate a topic representing the wildcard we're going to subscribe to + Topic topicSubscribe = ActiveMQJMSClient.createTopic("news.europe.#"); + + // Step 7. Create a consumer (topic subscriber) that will consume using that wildcard + // The consumer will receive any messages sent to any topic that starts with news.europe + MessageConsumer messageConsumer = session.createConsumer(topicSubscribe); + + // Step 8. Create an anonymous producer + MessageProducer producer = session.createProducer(null); + + // Step 9. Instantiate some more topic objects corresponding to the individual topics + // we're going to send messages to + Topic topicNewsUsaWrestling = ActiveMQJMSClient.createTopic("news.usa.wrestling"); + + Topic topicNewsEuropeSport = ActiveMQJMSClient.createTopic("news.europe.sport"); + + Topic topicNewsEuropeEntertainment = ActiveMQJMSClient.createTopic("news.europe.entertainment"); + + // Step 10. Send a message destined for the usa wrestling topic + TextMessage messageWrestlingNews = session.createTextMessage("Hulk Hogan starts ballet classes"); + + producer.send(topicNewsUsaWrestling, messageWrestlingNews); + + // Step 11. Send a message destined for the europe sport topic + TextMessage messageEuropeSport = session.createTextMessage("Lewis Hamilton joins European synchronized swimming team"); + + producer.send(topicNewsEuropeSport, messageEuropeSport); + + // Step 12. Send a message destined for the europe entertainment topic + TextMessage messageEuropeEntertainment = session.createTextMessage("John Lennon resurrected from dead"); + + producer.send(topicNewsEuropeEntertainment, messageEuropeEntertainment); + + // Step 9. Start the connection + + connection.start(); + + // Step 10. We don't receive the usa wrestling message since we subscribed to news.europe.# and + // that doesn't match news.usa.wrestling. However we do receive the Europe sport message, and the + // europe entertainment message, since these match the wildcard. + + TextMessage messageReceived1 = (TextMessage) messageConsumer.receive(5000); + + System.out.println("Received message: " + messageReceived1.getText()); + + TextMessage messageReceived2 = (TextMessage) messageConsumer.receive(5000); + + System.out.println("Received message: " + messageReceived2.getText()); + + Message message = messageConsumer.receive(1000); + + if (message != null) { + throw new IllegalStateException("Message was not null."); + } + + System.out.println("Didn't received any more message: " + message); + } + finally { + // Step 12. Be sure to close our resources! + if (initialContext != null) { + initialContext.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-hierarchies/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-hierarchies/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/topic-hierarchies/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..9ec66c6 --- /dev/null +++ b/examples/broker-features/standard/topic-hierarchies/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,71 @@ +<?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 topics used by the example--> + + <topic name="news"/> + + <topic name="news.usa"/> + + <topic name="news.usa.wrestling"/> + + <topic name="news.europe"/> + + <topic name="news.europe.sport"/> + + <topic name="news.europe.entertainment"/> + </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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="jms.#"> + <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/21bf4406/examples/broker-features/standard/topic-hierarchies/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-hierarchies/src/main/resources/jndi.properties b/examples/broker-features/standard/topic-hierarchies/src/main/resources/jndi.properties new file mode 100644 index 0000000..5cbe72c --- /dev/null +++ b/examples/broker-features/standard/topic-hierarchies/src/main/resources/jndi.properties @@ -0,0 +1,19 @@ +# 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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example1/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example1/pom.xml b/examples/broker-features/standard/topic-selector-example1/pom.xml new file mode 100644 index 0000000..f883759 --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example1/pom.xml @@ -0,0 +1,109 @@ +<?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.0.1-SNAPSHOT</version> + </parent> + + <artifactId>topic-selector1</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Topic Selector Example 1</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> + <ignore>${noServer}</ignore> + </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.TopicSelectorExample1</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>topic-selector1</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example1/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example1/readme.html b/examples/broker-features/standard/topic-selector-example1/readme.html new file mode 100644 index 0000000..f03f4f5 --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example1/readme.html @@ -0,0 +1,38 @@ +<!-- +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 Topic Selector Example 1</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 Topic Selector Example 1</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 how messages can be consumed from a topic using Message Selectors.</p> + <p>Consumers (or Subscribers) will only consume messages routed to a topic that match the provided selector</p> + <p>Topics and selectors are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p> + + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example1/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample1.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example1/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample1.java b/examples/broker-features/standard/topic-selector-example1/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample1.java new file mode 100644 index 0000000..53aee12 --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example1/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample1.java @@ -0,0 +1,145 @@ +/* + * 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.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; + +/** + * A simple JMS Topic example that creates a producer and consumer on a queue and sends and receives a message. + */ +public class TopicSelectorExample1 { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + + InitialContext initialContext = null; + try { + + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. Look-up the JMS topic + Topic topic = (Topic) initialContext.lookup("topic/exampleTopic"); + + // Step 3. Look-up the JMS connection factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4. Create a JMS connection + connection = cf.createConnection(); + + // Step 5. Create a JMS session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Create a JMS message producer + MessageProducer producer = session.createProducer(topic); + + // Step 7. Create one subscription with a specific Filter for someID=1 + MessageConsumer messageConsumer1 = session.createConsumer(topic, "someID=1", false); + + // Step 8. Create another subscription with a specific Filter for someID=2 + MessageConsumer messageConsumer2 = session.createConsumer(topic, "someID=2", false); + + // Step 9. Create another subscription with no filters, which will receive every message sent to the topic + MessageConsumer messageConsumer3 = session.createConsumer(topic); + + // Step 10. Send 20 messages, 10 with someID=1, 10 with someID=2 + + for (int i = 1; i < 10; i++) { + for (int someID = 1; someID <= 2; someID++) { + // Step 10.1 Create a text message + TextMessage message1 = session.createTextMessage("This is a text message " + i + + " sent for someID=" + + someID); + + // Step 10.1 Set a property + message1.setIntProperty("someID", someID); + + // Step 10.2 Send the message + producer.send(message1); + + System.out.println("Sent message: " + message1.getText()); + } + } + + // Step 11. Start the JMS Connection. This step will activate the subscribers to receive messages. + connection.start(); + + // Step 12. Consume the messages from MessageConsumer1, filtering out someID=2 + + System.out.println("*************************************************************"); + System.out.println("MessageConsumer1 will only receive messages where someID=1:"); + for (;;) { + TextMessage messageReceivedA = (TextMessage) messageConsumer1.receive(1000); + if (messageReceivedA == null) { + break; + } + + System.out.println("messageConsumer1 received " + messageReceivedA.getText() + + " someID = " + + messageReceivedA.getIntProperty("someID")); + } + + // Step 13. Consume the messages from MessageConsumer2, filtering out someID=2 + System.out.println("*************************************************************"); + System.out.println("MessageConsumer2 will only receive messages where someID=2:"); + for (;;) { + TextMessage messageReceivedB = (TextMessage) messageConsumer2.receive(1000); + if (messageReceivedB == null) { + break; + } + + System.out.println("messageConsumer2 received " + messageReceivedB.getText() + + " someID = " + + messageReceivedB.getIntProperty("someID")); + } + + // Step 14. Consume the messages from MessageConsumer3, receiving the complete set of messages + System.out.println("*************************************************************"); + System.out.println("MessageConsumer3 will receive every message:"); + for (;;) { + TextMessage messageReceivedC = (TextMessage) messageConsumer3.receive(1000); + if (messageReceivedC == null) { + break; + } + System.out.println("messageConsumer3 received " + messageReceivedC.getText() + + " someID = " + + messageReceivedC.getIntProperty("someID")); + } + + // Step 15. Close the subscribers + messageConsumer1.close(); + messageConsumer2.close(); + messageConsumer3.close(); + } + finally { + // Step 15. Be sure to close our JMS resources! + if (initialContext != null) { + initialContext.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example1/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example1/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/topic-selector-example1/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..d45eb5d --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example1/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,60 @@ +<?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 topic used by the example--> + <topic name="exampleTopic"/> + </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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example topic--> + <security-setting match="jms.topic.exampleTopic"> + <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/21bf4406/examples/broker-features/standard/topic-selector-example1/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example1/src/main/resources/jndi.properties b/examples/broker-features/standard/topic-selector-example1/src/main/resources/jndi.properties new file mode 100644 index 0000000..54bed6d --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example1/src/main/resources/jndi.properties @@ -0,0 +1,20 @@ +# 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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +topic.topic/exampleTopic=exampleTopic http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example2/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example2/pom.xml b/examples/broker-features/standard/topic-selector-example2/pom.xml new file mode 100644 index 0000000..d5f88c6 --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example2/pom.xml @@ -0,0 +1,109 @@ +<?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.0.1-SNAPSHOT</version> + </parent> + + <artifactId>topic-selector2</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Topic Selector Example 2</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> + <ignore>${noServer}</ignore> + </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.TopicSelectorExample2</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>topic-selector2</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example2/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example2/readme.html b/examples/broker-features/standard/topic-selector-example2/readme.html new file mode 100644 index 0000000..608e3d8 --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example2/readme.html @@ -0,0 +1,47 @@ +<!-- +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 Topic Selector Example 2</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 Topic Selector Example 2</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 selectively consume messages using message selectors with topic consumers.</p> + + <p>Message selectors are strings with special syntax that can be used in creating consumers. Message consumers + that are thus created only receive messages that match its selector. On message delivering, the ActiveMQ + Server evaluates the corresponding message headers of the messages against each selector, if any, and then delivers + the 'matched' messages to its consumer. Please consult the JMS 1.1 specification for full details.</p> + + <p>In this example, three message consumers are created on a topic. The first consumer is created with selector + <code>'color=red'</code>, it only receives messages that + have a 'color' string property of 'red' value; the second is created with selector <code>'color=green'</code>, it + only receives messages who have a 'color' string property of + 'green' value; and the third without a selector, which means it receives all messages. To illustrate, three messages + with different 'color' property values are created and sent.</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example2/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample2.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example2/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample2.java b/examples/broker-features/standard/topic-selector-example2/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample2.java new file mode 100644 index 0000000..464b21b --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example2/src/main/java/org/apache/activemq/artemis/jms/example/TopicSelectorExample2.java @@ -0,0 +1,145 @@ +/* + * 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.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * A simple JMS example that consumes messages using selectors. + */ +public class TopicSelectorExample2 { + + public static void main(final String[] args) throws Exception { + AtomicBoolean result = new AtomicBoolean(true); + Connection connection = null; + InitialContext initialContext = null; + try { + // /Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. perform a lookup on the topic + Topic topic = (Topic) initialContext.lookup("topic/exampleTopic"); + + // Step 3. perform a lookup on the Connection Factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4. Create a JMS Connection + connection = cf.createConnection(); + + // Step 5. Start the Connection + connection.start(); + + // Step 6. Create a JMS Session + Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 7. Create a Message Producer + MessageProducer producer = producerSession.createProducer(topic); + + // Step 8. Prepare two selectors + String redSelector = "color='red'"; + String greenSelector = "color='green'"; + + // Step 9. Create a JMS Message Consumer that receives 'red' messages + Session redSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer redConsumer = redSession.createConsumer(topic, redSelector); + redConsumer.setMessageListener(new SimpleMessageListener("red", result)); + + // Step 10. Create a second JMS message consumer that receives 'green' messages + Session greenSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer greenConsumer = greenSession.createConsumer(topic, greenSelector); + greenConsumer.setMessageListener(new SimpleMessageListener("green", result)); + + // Step 11. Create another JMS message consumer that receives all messages. + Session allSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer allConsumer = allSession.createConsumer(topic); + allConsumer.setMessageListener(new SimpleMessageListener("all", result)); + + // Step 12. Create three messages, each has a color property + TextMessage redMessage = producerSession.createTextMessage("Red"); + redMessage.setStringProperty("color", "red"); + TextMessage greenMessage = producerSession.createTextMessage("Green"); + greenMessage.setStringProperty("color", "green"); + TextMessage blueMessage = producerSession.createTextMessage("Blue"); + blueMessage.setStringProperty("color", "blue"); + + // Step 13. Send the Messages + producer.send(redMessage); + System.out.println("Message sent: " + redMessage.getText()); + producer.send(greenMessage); + System.out.println("Message sent: " + greenMessage.getText()); + producer.send(blueMessage); + System.out.println("Message sent: " + blueMessage.getText()); + + Thread.sleep(5000); + + if (!result.get()) + throw new IllegalStateException(); + } + finally { + // Step 14. Be sure to close our JMS resources! + if (connection != null) { + connection.close(); + } + + // Also the initialContext + if (initialContext != null) { + initialContext.close(); + } + } + } +} + +class SimpleMessageListener implements MessageListener { + + private final String name; + AtomicBoolean result; + + public SimpleMessageListener(final String listener, AtomicBoolean result) { + name = listener; + this.result = result; + } + + public void onMessage(final Message msg) { + TextMessage textMessage = (TextMessage) msg; + try { + String colorProp = msg.getStringProperty("color"); + System.out.println("Receiver " + name + + " receives message [" + + textMessage.getText() + + "] with color property: " + + colorProp); + if (!colorProp.equals(name) && !name.equals("all")) { + result.set(false); + } + } + catch (JMSException e) { + e.printStackTrace(); + result.set(false); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic-selector-example2/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example2/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/topic-selector-example2/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..d45eb5d --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example2/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,60 @@ +<?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 topic used by the example--> + <topic name="exampleTopic"/> + </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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example topic--> + <security-setting match="jms.topic.exampleTopic"> + <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/21bf4406/examples/broker-features/standard/topic-selector-example2/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic-selector-example2/src/main/resources/jndi.properties b/examples/broker-features/standard/topic-selector-example2/src/main/resources/jndi.properties new file mode 100644 index 0000000..54bed6d --- /dev/null +++ b/examples/broker-features/standard/topic-selector-example2/src/main/resources/jndi.properties @@ -0,0 +1,20 @@ +# 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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +topic.topic/exampleTopic=exampleTopic http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic/pom.xml b/examples/broker-features/standard/topic/pom.xml new file mode 100644 index 0000000..7cc643e --- /dev/null +++ b/examples/broker-features/standard/topic/pom.xml @@ -0,0 +1,108 @@ +<?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.0.1-SNAPSHOT</version> + </parent> + + <artifactId>topic</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Topic 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> + <ignore>${noServer}</ignore> + </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.TopicExample</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>topic</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic/readme.html b/examples/broker-features/standard/topic/readme.html new file mode 100644 index 0000000..8bfa903 --- /dev/null +++ b/examples/broker-features/standard/topic/readme.html @@ -0,0 +1,36 @@ +<!-- +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 Topic 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 Topic 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 send and receive a message to a JMS Topic with ActiveMQ Artemis.</p> + <p>Topics are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p> + <p>A Topic is used to send messages using the publish-subscribe model, from a producer to 1 or more consumers.</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic/src/main/java/org/apache/activemq/artemis/jms/example/TopicExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic/src/main/java/org/apache/activemq/artemis/jms/example/TopicExample.java b/examples/broker-features/standard/topic/src/main/java/org/apache/activemq/artemis/jms/example/TopicExample.java new file mode 100644 index 0000000..1c8695b --- /dev/null +++ b/examples/broker-features/standard/topic/src/main/java/org/apache/activemq/artemis/jms/example/TopicExample.java @@ -0,0 +1,94 @@ +/* + * 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.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; + +/** + * A simple JMS Topic example that creates a producer and consumer on a queue and sends and receives a message. + */ +public class TopicExample { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + InitialContext initialContext = null; + try { + // /Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. perform a lookup on the topic + Topic topic = (Topic) initialContext.lookup("topic/exampleTopic"); + + // Step 3. perform a lookup on the Connection Factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4. Create a JMS Connection + connection = cf.createConnection(); + + // Step 5. Create a JMS Session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Create a Message Producer + MessageProducer producer = session.createProducer(topic); + + // Step 7. Create a JMS Message Consumer + MessageConsumer messageConsumer1 = session.createConsumer(topic); + + // Step 8. Create a JMS Message Consumer + MessageConsumer messageConsumer2 = session.createConsumer(topic); + + // Step 9. Create a Text Message + TextMessage message = session.createTextMessage("This is a text message"); + + System.out.println("Sent message: " + message.getText()); + + // Step 10. Send the Message + producer.send(message); + + // Step 11. Start the Connection + connection.start(); + + // Step 12. Receive the message + TextMessage messageReceived = (TextMessage) messageConsumer1.receive(); + + System.out.println("Consumer 1 Received message: " + messageReceived.getText()); + + // Step 13. Receive the message + messageReceived = (TextMessage) messageConsumer2.receive(); + + System.out.println("Consumer 2 Received message: " + messageReceived.getText()); + } + finally { + // Step 14. Be sure to close our JMS resources! + if (connection != null) { + connection.close(); + } + + // Also the initialContext + if (initialContext != null) { + initialContext.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/topic/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/topic/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..fd6671c --- /dev/null +++ b/examples/broker-features/standard/topic/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,59 @@ +<?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 topic used by the example--> + <topic name="exampleTopic"/> + </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> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + <security-settings> + <!--security for example topic--> + <security-setting match="jms.topic.exampleTopic"> + <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/21bf4406/examples/broker-features/standard/topic/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/topic/src/main/resources/jndi.properties b/examples/broker-features/standard/topic/src/main/resources/jndi.properties new file mode 100644 index 0000000..54bed6d --- /dev/null +++ b/examples/broker-features/standard/topic/src/main/resources/jndi.properties @@ -0,0 +1,20 @@ +# 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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +topic.topic/exampleTopic=exampleTopic http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/transactional/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/transactional/pom.xml b/examples/broker-features/standard/transactional/pom.xml new file mode 100644 index 0000000..660d262 --- /dev/null +++ b/examples/broker-features/standard/transactional/pom.xml @@ -0,0 +1,109 @@ +<?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.0.1-SNAPSHOT</version> + </parent> + + <artifactId>transactional</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Transactional 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> + <ignore>${noServer}</ignore> + </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.TransactionalExample</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>transactional</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/transactional/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/transactional/readme.html b/examples/broker-features/standard/transactional/readme.html new file mode 100644 index 0000000..b171c0a --- /dev/null +++ b/examples/broker-features/standard/transactional/readme.html @@ -0,0 +1,40 @@ +<!-- +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 Transactional Session 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 Transactional Session 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 use a transacted Session with ActiveMQ Artemis.</p> + <p>Firstly 2 messages are sent via the transacted sending session before being committed. This ensures that both message + are sent</p> + <p>Secondly the receiving session receives the messages firstly demonstrating a message being redelivered after the session + being rolled back and then acknowledging receipt of the messages via the commit method.</p> + + </body> +</html>
