Return-Path: X-Original-To: apmail-activemq-commits-archive@www.apache.org Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0B31918699 for ; Wed, 12 Aug 2015 03:46:58 +0000 (UTC) Received: (qmail 57538 invoked by uid 500); 12 Aug 2015 03:46:57 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 57456 invoked by uid 500); 12 Aug 2015 03:46:57 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 55416 invoked by uid 99); 12 Aug 2015 03:46:56 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Aug 2015 03:46:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7C6CFE35C6; Wed, 12 Aug 2015 03:46:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: clebertsuconic@apache.org To: commits@activemq.apache.org Date: Wed, 12 Aug 2015 03:47:24 -0000 Message-Id: In-Reply-To: <9f945b5dbf20413381634a841d402097@git.apache.org> References: <9f945b5dbf20413381634a841d402097@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [30/52] [abbrv] [partial] activemq-artemis git commit: This commit has improvements on the examples including: 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 @@ + + + + + + + + + + + + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + + + + + + 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 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.0.1-SNAPSHOT + + + topic-hierarchies + jar + ActiveMQ Artemis JMS Topic Hierarchies Example + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.TopicHierarchyExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + topic-hierarchies + ${project.version} + + + + + + + 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 @@ + + + + + ActiveMQ Artemis Topic Hierarchy Example + + + + + +

Topic Hierarchy Example

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ + +

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.

+

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

+

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"

+

For more information on the wild-card syntax please consult the user manual.

+ + 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + 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 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.0.1-SNAPSHOT + + + topic-selector1 + jar + ActiveMQ Artemis JMS Topic Selector Example 1 + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.TopicSelectorExample1 + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + topic-selector1 + ${project.version} + + + + + + + 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 @@ + + + + + ActiveMQ Artemis JMS Topic Selector Example 1 + + + + + +

JMS Topic Selector Example 1

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ + +

This example shows how messages can be consumed from a topic using Message Selectors.

+

Consumers (or Subscribers) will only consume messages routed to a topic that match the provided selector

+

Topics and selectors are a standard part of JMS, please consult the JMS 1.1 specification for full details.

+ + + 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 @@ + + + + + + + + + + + + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + 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 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.0.1-SNAPSHOT + + + topic-selector2 + jar + ActiveMQ Artemis JMS Topic Selector Example 2 + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.TopicSelectorExample2 + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + topic-selector2 + ${project.version} + + + + + + + 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 @@ + + + + + ActiveMQ Artemis JMS Topic Selector Example 2 + + + + + +

JMS Topic Selector Example 2

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ + +

This example shows you how to selectively consume messages using message selectors with topic consumers.

+ +

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.

+ +

In this example, three message consumers are created on a topic. The first consumer is created with selector + 'color=red', it only receives messages that + have a 'color' string property of 'red' value; the second is created with selector 'color=green', 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.

+ + 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 @@ + + + + + + + + + + + + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + 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 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.0.1-SNAPSHOT + + + topic + jar + ActiveMQ Artemis JMS Topic Example + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.TopicExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + topic + ${project.version} + + + + + + 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 @@ + + + + + ActiveMQ Artemis JMS Topic Example + + + + + +

JMS Topic Example

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ +

This example shows you how to send and receive a message to a JMS Topic with ActiveMQ Artemis.

+

Topics are a standard part of JMS, please consult the JMS 1.1 specification for full details.

+

A Topic is used to send messages using the publish-subscribe model, from a producer to 1 or more consumers.

+ + 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 @@ + + + + + + + + + + + + + ./data/bindings + + ./data/journal + + ./data/largemessages + + ./data/paging + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + 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 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.broker + jms-examples + 1.0.1-SNAPSHOT + + + transactional + jar + ActiveMQ Artemis JMS Transactional Example + + + ${project.basedir}/../../../.. + + + + + org.apache.activemq + artemis-jms-client + ${project.version} + + + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.TransactionalExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.broker + transactional + ${project.version} + + + + + + + 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 @@ + + + + + ActiveMQ Artemis JMS Transactional Session Example + + + + + +

JMS Transactional Session Example

+ +
To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.
+ + +

This example shows you how to use a transacted Session with ActiveMQ Artemis.

+

Firstly 2 messages are sent via the transacted sending session before being committed. This ensures that both message + are sent

+

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.

+ + +