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 E7D1D18A52 for ; Fri, 7 Aug 2015 19:31:21 +0000 (UTC) Received: (qmail 4155 invoked by uid 500); 7 Aug 2015 19:31:21 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 4035 invoked by uid 500); 7 Aug 2015 19:31:21 -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 3381 invoked by uid 99); 7 Aug 2015 19:31:21 -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; Fri, 07 Aug 2015 19:31:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D2D39E0F79; Fri, 7 Aug 2015 19:31:20 +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: Fri, 07 Aug 2015 19:31:30 -0000 Message-Id: In-Reply-To: <5a43a527d6804226867677a3653161d5@git.apache.org> References: <5a43a527d6804226867677a3653161d5@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [11/18] activemq-artemis git commit: ARTEMIS-180 removing -Pexample and some other improvements around the examples http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/expiry/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/expiry/readme.html b/examples/jms/expiry/readme.html index acbbe4b..65cb534 100644 --- a/examples/jms/expiry/readme.html +++ b/examples/jms/expiry/readme.html @@ -26,6 +26,7 @@ under the License.

JMS Expiration 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 configure ActiveMQ Artemis so messages are expipired after a certain time.

Messages can be retained in the messaging system for a limited period of time before being removed. @@ -44,7 +45,7 @@ under the License. <expiry-address>jms.queue.expiryQueue</expiry-address> </address-setting> - +

This configuration will moved expired messages from the exampleQueue to the expiryQueue

ActiveMQ Artemis allows to specify either a Queue by prefixing the expiry-address with jms.queue. or a Topic by prefixing with jms.topic..
@@ -56,151 +57,5 @@ under the License. <entry name="/queue/expiryQueue"/> </queue> -

-

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

-
    -
  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2. -
    -           InitialContext initialContext = getContext();
    -        
    - -
  3. We look up the JMS queue object from JNDI
  4. -
    -           Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
    -        
    - -
  5. We look up the JMS connection factory object from JNDI
  6. -
    -           ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
    -        
    - -
  7. We create a JMS connection
  8. -
    -           connection = cf.createConnection();
    -        
    - -
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages
  10. -
    -           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    -        
    - -
  11. We create a JMS message producer on the session. This will be used to send the messages
  12. -
    -          MessageProducer messageProducer = session.createProducer(topic);
    -       
    - -
  13. Messages sent by this producer will be retained for 1s (1000ms) before expiration
  14. -
    -           producer.setTimeToLive(1000);
    -       
    - -
  15. We create a text messages
  16. -
    -            TextMessage message = session.createTextMessage("this is a text message");
    -        
    - -
  17. We send the message to the queue
  18. -
    -            producer.send(message);
    -        
    - -
  19. We sleep a little bit to let the message expire
  20. -
    -            Thread.sleep(5000);
    -        
    - -

    We will now try to consume the message from the queue but it won't be there since it has expired

    - -
  21. We create a JMS message consumer on the queue
  22. -
    -            MessageConsumer messageConsumer = session.createConsumer(queue);
    -        
    - -
  23. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  24. -
    -           connection.start();
    -        
    - -
  25. We try to receive a message from the queue. Since there is none, the call will timeout after 5000ms and messageReceived will be null -
    -           TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
    -           System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);
    -        
    - -

    However, we have configured ActiveMQ Artemis to send any expired messages to the expiryQueue. - We will now consume messages from this expiry queue and receives the expired message.

    - -
  26. We look up the JMS expiry queue object from JNDI
  27. -
    -           Queue expiryQueue = (Queue)initialContext.lookup("/queue/expiryQueue");
    -        
    - -
  28. We create a JMS message consumer on the expiry queue
  29. -
    -            MessageConsumer expiryConsumer = session.createConsumer(expiryQueue);
    -        
    - -
  30. We consume a message from the expiry queue:
  31. -
    -            messageReceived = (TextMessage)expiryConsumer.receive(5000);
    -        
    - -
  32. The message consumed from the expiry queue has the same content than the message which was sent to the queue -
    -            System.out.println("Received message from " + expiryQueue.getQueueName() + ": " + messageReceived.getText());
    -        
    - -

    JMS does not specify the notion of expiry queue. From JMS point of view, the message received from the expiry queue - is a different message than the message expired from the queue: the two messages have the same content (properties and body) but - their JMS headers differ.
    - ActiveMQ Artemis defines additional properties to correlate the message received from the expiry queue with the - message expired from the queue

    - -
  33. The expired message's destination is the expiry queue
  34. -
    -            System.out.println("Destination of the expired message: " + ((Queue)messageReceived.getJMSDestination()).getQueueName());
    -        
    - -
  35. The expired message has its own expiration time (its time to live in the expiry queue)
  36. -
    -            System.out.println("Expiration time of the expired message (relative to the expiry queue): " + messageReceived.getJMSExpiration());
    -        
    - -

    As we have not defined a time-to-live for the expiry queue, messages sent to the expiry queue will be kept forever (their JMS Expiration value is 0)

    - -
  37. The origin destination is stored in the _HORNETQ_ORIG_DESTINATION property -
    -            System.out.println("*Origin destination* of the expired message: " + messageReceived.getStringProperty("_HORNETQ_ORIG_DESTINATION"));
    -        
    - -
  38. The actual expiration time (when the message was expired from the queue) is stored in the _HORNETQ_ACTUAL_EXPIRY property -
    -            System.out.println("*Actual expiration time* of the expired message: " + messageReceived.getLongProperty("_HORNETQ_ACTUAL_EXPIRY"));
    -        
    - -

    -
  39. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  40. - -
    -           finally
    -           {
    -              if (initialContext != null)
    -              {
    -                initialContext.close();
    -              }
    -              if (connection != null)
    -              {
    -                 connection.close();
    -              }
    -           }
    -        
    -
- -

More information

- - http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/ha-policy-autobackup/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/ha-policy-autobackup/pom.xml b/examples/jms/ha-policy-autobackup/pom.xml index cace530..9e33342 100644 --- a/examples/jms/ha-policy-autobackup/pom.xml +++ b/examples/jms/ha-policy-autobackup/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -42,65 +43,61 @@ under the License. ${project.version} - org.apache.geronimo.specs - geronimo-jms_2.0_spec + org.apache.activemq + artemis-jms-client + ${project.version} - - - example - - - - org.apache.activemq - artemis-maven-plugin - - - create - - create - - - ${basedir}/target/server0 - ${basedir}/target/classes/activemq/server0 - - - - create2 - - create - - - ${basedir}/target/server1 - ${basedir}/target/classes/activemq/server1 - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.HAPolicyAutoBackupExample - - ${basedir}/target/server0 - ${basedir}/target/server1 - - - - - - - org.apache.activemq.examples.jms - ha-policy-autobackup - ${project.version} - - - - - - - + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${basedir}/target/server0 + ${basedir}/target/classes/activemq/server0 + + + + create2 + + create + + + ${basedir}/target/server1 + ${basedir}/target/classes/activemq/server1 + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.HAPolicyAutoBackupExample + + ${basedir}/target/server0 + ${basedir}/target/server1 + + + + + + + org.apache.activemq.examples.jms + ha-policy-autobackup + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/ha-policy-autobackup/src/main/java/org/apache/activemq/artemis/jms/example/HAPolicyAutoBackupExample.java ---------------------------------------------------------------------- diff --git a/examples/jms/ha-policy-autobackup/src/main/java/org/apache/activemq/artemis/jms/example/HAPolicyAutoBackupExample.java b/examples/jms/ha-policy-autobackup/src/main/java/org/apache/activemq/artemis/jms/example/HAPolicyAutoBackupExample.java index d561750..93d3d53 100644 --- a/examples/jms/ha-policy-autobackup/src/main/java/org/apache/activemq/artemis/jms/example/HAPolicyAutoBackupExample.java +++ b/examples/jms/ha-policy-autobackup/src/main/java/org/apache/activemq/artemis/jms/example/HAPolicyAutoBackupExample.java @@ -16,12 +16,6 @@ */ package org.apache.activemq.artemis.jms.example; -import org.apache.activemq.artemis.api.core.TransportConfiguration; -import org.apache.activemq.artemis.api.core.client.ClusterTopologyListener; -import org.apache.activemq.artemis.api.core.client.TopologyMember; -import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; -import org.apache.activemq.artemis.util.ServerUtil; - import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.MessageConsumer; @@ -29,13 +23,18 @@ import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; -import javax.naming.InitialContext; import java.util.ArrayList; -import java.util.Hashtable; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import org.apache.activemq.artemis.api.core.TransportConfiguration; +import org.apache.activemq.artemis.api.core.client.ClusterTopologyListener; +import org.apache.activemq.artemis.api.core.client.TopologyMember; +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.activemq.artemis.util.ServerUtil; + /** * A simple example that demonstrates server side load-balancing of messages between the queue instances on different * nodes of the cluster. @@ -52,33 +51,17 @@ public class HAPolicyAutoBackupExample Connection connection1 = null; - InitialContext ic0 = null; - - InitialContext ic1 = null; - try { server0 = ServerUtil.startServer(args[0], HAPolicyAutoBackupExample.class.getSimpleName() + "0", 0, 5000); server1 = ServerUtil.startServer(args[1], HAPolicyAutoBackupExample.class.getSimpleName() + "1", 1, 5000); - // Step 1. Get an initial context for looking up JNDI from server 0 and 1 - Hashtable properties = new Hashtable(); - properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); - properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61616?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1"); - properties.put("queue.queue/exampleQueue", "exampleQueue"); - ic0 = new InitialContext(properties); - - properties = new Hashtable(); - properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); - properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1"); - ic1 = new InitialContext(properties); - // Step 2. Look-up the JMS Queue object from JNDI - Queue queue = (Queue) ic0.lookup("queue/exampleQueue"); + Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); - // Step 3. Look-up a JMS Connection Factory object from JNDI on server 0 and 1 - ConnectionFactory cf0 = (ConnectionFactory) ic0.lookup("ConnectionFactory"); - ConnectionFactory cf1 = (ConnectionFactory) ic1.lookup("ConnectionFactory"); + // Step 3. new connection factories towards server 0 and 1 + ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1"); + ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1"); // Step 6. We create JMS Connections to server 0 and 1 connection0 = cf0.createConnection(); @@ -129,7 +112,7 @@ public class HAPolicyAutoBackupExample // Step 15.now kill server1, messages will be scaled down to server0 ServerUtil.killServer(server1); - Thread.sleep(40000); + Thread.sleep(5000); // Step 16. we now receive the messages that were on server1 but were scaled down to server0 for (int i = 0; i < numMessages / 2; i++) http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/http-transport/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/http-transport/pom.xml b/examples/jms/http-transport/pom.xml index 0d9c91d..6e2914b 100644 --- a/examples/jms/http-transport/pom.xml +++ b/examples/jms/http-transport/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -27,7 +28,7 @@ under the License. 1.0.1-SNAPSHOT - artemis-jms-http-transport-example + http-transport jar ActiveMQ Artemis JMS Http Transport Example @@ -37,71 +38,82 @@ under the License. - org.apache.geronimo.specs - geronimo-jms_2.0_spec + org.apache.activemq + artemis-jms-client + ${project.version} - example - - - - org.apache.activemq - artemis-maven-plugin - - - create - - create - - - - start - - cli - - - true - tcp://localhost:8080?http-enabled=true - - run - - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.HttpTransportExample - - - - stop - - cli - - - - stop - - - - - - - org.apache.activemq.examples.jms - artemis-jms-http-transport-example - ${project.version} - - - - - + + noServer + + true + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + ${noServer} + ${basedir}/target/classes/activemq/server0 + + + + start + + cli + + + ${noServer} + true + tcp://localhost:8080?http-enabled=true + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.HttpTransportExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.jms + http-transport + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/http-transport/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/http-transport/readme.html b/examples/jms/http-transport/readme.html index 9cc3f8c..a0f0005 100644 --- a/examples/jms/http-transport/readme.html +++ b/examples/jms/http-transport/readme.html @@ -27,14 +27,17 @@ under the License.

JMS HTTP 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 configure ActiveMQ Artemis to use the HTTP protocol as its transport layer.

- +

ActiveMQ Artemis supports a variety of network protocols to be its underlying transport without any specific code change.

- +

This example is taken from the queue example without any code change. By changing the configuration file, one can get ActiveMQ Artemis working with HTTP transport.

All you need to do is open the server0/broker.xml and enable HTTP like the following

- - + +
       
       <connector name="netty-connector">tcp://localhost:8080?httpEnabled=true</connector>
@@ -44,84 +47,6 @@ under the License.
       <acceptor name="netty-acceptor">tcp://localhost:8080 </acceptor>
       
       
- -

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

- -
    -
  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2. -
    -           InitialContext initialContext = getContext();
    -        
    - -
  3. We look-up the JMS queue object from JNDI
  4. -
    -           Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
    -        
    - -
  5. We look-up the JMS connection factory object from JNDI
  6. -
    -           ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
    -        
    - -
  7. We create a JMS connection
  8. -
    -           connection = cf.createConnection();
    -        
    - -
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10. -
    -           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    -        
    - -
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12. -
    -          MessageProducer messageProducer = session.createProducer(topic);
    -       
    - -
  13. We create a JMS text message that we are going to send.
  14. -
    -           TextMessage message = session.createTextMessage("This is a text message");
    -        
    - -
  15. We send message to the queue
  16. -
    -           messageProducer.send(message);
    -        
    - -
  17. We create a JMS Message Consumer to receive the message.
  18. -
    -           MessageConsumer messageConsumer = session.createConsumer(queue);
    -        
    - -
  19. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  20. -
    -           connection.start();
    -        
    - -
  21. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  22. -
    -           TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
    -        
    - -
  23. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  24. - -
    -           finally
    -           {
    -              if (initialContext != null)
    -              {
    -                initialContext.close();
    -              }
    -              if (connection != null)
    -              {
    -                 connection.close();
    -              }
    -           }
    -        
    - - -
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/http-transport/src/main/java/org/apache/activemq/artemis/jms/example/HttpTransportExample.java ---------------------------------------------------------------------- diff --git a/examples/jms/http-transport/src/main/java/org/apache/activemq/artemis/jms/example/HttpTransportExample.java b/examples/jms/http-transport/src/main/java/org/apache/activemq/artemis/jms/example/HttpTransportExample.java index cd2e175..9ae1cba 100644 --- a/examples/jms/http-transport/src/main/java/org/apache/activemq/artemis/jms/example/HttpTransportExample.java +++ b/examples/jms/http-transport/src/main/java/org/apache/activemq/artemis/jms/example/HttpTransportExample.java @@ -25,6 +25,9 @@ import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + /** * A simple JMS Queue example that uses HTTP protocol. */ @@ -33,17 +36,13 @@ public class HttpTransportExample 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. Perfom a lookup on the queue - Queue queue = (Queue)initialContext.lookup("queue/exampleQueue"); + Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); // Step 3. Perform a lookup on the Connection Factory - ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory"); + ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:8080?http-enabled=true"); // Step 4.Create a JMS Connection connection = cf.createConnection(); @@ -75,15 +74,9 @@ public class HttpTransportExample System.out.println("Received message: " + messageReceived.getText()); - initialContext.close(); } finally { - // Step 12. 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/dd820318/examples/jms/http-transport/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/jms/http-transport/src/main/resources/activemq/server0/broker.xml b/examples/jms/http-transport/src/main/resources/activemq/server0/broker.xml index ea39ccd..d1a4d2a 100644 --- a/examples/jms/http-transport/src/main/resources/activemq/server0/broker.xml +++ b/examples/jms/http-transport/src/main/resources/activemq/server0/broker.xml @@ -29,13 +29,13 @@ under the License. - ${data.dir:../data}/bindings + ./data/bindings - ${data.dir:../data}/journal + ./data/journal - ${data.dir:../data}/largemessages + ./data/largemessages - ${data.dir:../data}/paging + ./data/paging http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/http-transport/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/jms/http-transport/src/main/resources/jndi.properties b/examples/jms/http-transport/src/main/resources/jndi.properties deleted file mode 100644 index 15dae0b..0000000 --- a/examples/jms/http-transport/src/main/resources/jndi.properties +++ /dev/null @@ -1,20 +0,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. - -java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory -connectionFactory.ConnectionFactory=tcp://localhost:8080?http-enabled=true -queue.queue/exampleQueue=exampleQueue http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/instantiate-connection-factory/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/instantiate-connection-factory/pom.xml b/examples/jms/instantiate-connection-factory/pom.xml index 774b2b2..1f4d2e6 100644 --- a/examples/jms/instantiate-connection-factory/pom.xml +++ b/examples/jms/instantiate-connection-factory/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -27,7 +28,7 @@ under the License. 1.0.1-SNAPSHOT - artemis-jms-instantiate-connection-factory-example + instantiate-connection-factory jar ActiveMQ Artemis JMS Instantiate Connection Factory Example @@ -35,83 +36,85 @@ under the License. ${project.basedir}/../../.. + org.apache.activemq - artemis-core-client - ${project.version} - - - org.apache.activemq artemis-jms-client ${project.version} - - org.apache.geronimo.specs - geronimo-jms_2.0_spec - - example - - - - org.apache.activemq - artemis-maven-plugin - - - create - - create - - - - start - - cli - - - true - tcp://localhost:61616 - - run - - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.InstantiateConnectionFactoryExample - - - - stop - - cli - - - - stop - - - - - - - org.apache.activemq.examples.jms - artemis-jms-instantiate-connection-factory-example - ${project.version} - - - - - + + noServer + + true + + + + + 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.InstantiateConnectionFactoryExample + + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.jms + instantiate-connection-factory + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/instantiate-connection-factory/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/instantiate-connection-factory/readme.html b/examples/jms/instantiate-connection-factory/readme.html index ff4e17a..fa062ea 100644 --- a/examples/jms/instantiate-connection-factory/readme.html +++ b/examples/jms/instantiate-connection-factory/readme.html @@ -26,7 +26,9 @@ under the License.

JMS Instantiate Connection Factory 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.
+

Usually, JMS Objects such as ConnectionFactories, Queue and Topic instances are looked up from JNDI before being used by the client code. This objects are called "administered objects" in JMS specification terminology.

@@ -43,95 +45,5 @@ under the License. are that it wishes to connect to, or for specifying live-backup pairs of servers for failover.

For more information on instantiating ConnectionFactories directly please consult the user manual and javadoc.

- -

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

- -
    -
  1. Instead of looking it up from JNDI we directly instantiate the JMS Queue object. We - pass in the name of the JMS Queue in the constructor. The actual JMS Queue must already be deployed on - the server.
  2. -
    -           
    -     Queue queue = new ActiveMQQueue("exampleQueue");
    -        
    - -
  3. Instantiate the TransportConfiguration object. The TransportConfiguration instance encapsulates - the connection details of the server we're connecting to. In this case we're using Netty as a transport, and - we're specifying to connect on port 61617.
  4. -
    -           
    -     Map connectionParams = new HashMap();
    -
    -     connectionParams.put(PORT_PROP_NAME, 61617);
    -
    -     TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
    -                                                                                connectionParams);
    -           
    -           
    -        
    - -
  5. Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration.
  6. -
    -           
    -     ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(transportConfiguration);
    -           
    -        
    - -
  7. We create a JMS connection
  8. -
    -           connection = cf.createConnection();
    -        
    - -
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10. -
    -           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    -        
    - -
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12. -
    -          MessageProducer messageProducer = session.createProducer(topic);
    -       
    - -
  13. We create a JMS text message that we are going to send.
  14. -
    -           TextMessage message = session.createTextMessage("This is a text message");
    -        
    - -
  15. We send message to the queue
  16. -
    -           messageProducer.send(message);
    -        
    - -
  17. We create a JMS Message Consumer to receive the message.
  18. -
    -           MessageConsumer messageConsumer = session.createConsumer(queue);
    -        
    - -
  19. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  20. -
    -           connection.start();
    -        
    - -
  21. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  22. -
    -           TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
    -        
    - -
  23. And finally, always remember to close your resources after use, in a finally block.
  24. - -
    -           finally
    -           {
    -              if (connection != null)
    -              {
    -                 connection.close();
    -              }
    -           }
    -        
    - - - -
- + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/instantiate-connection-factory/src/main/java/org/apache/activemq/artemis/jms/example/InstantiateConnectionFactoryExample.java ---------------------------------------------------------------------- diff --git a/examples/jms/instantiate-connection-factory/src/main/java/org/apache/activemq/artemis/jms/example/InstantiateConnectionFactoryExample.java b/examples/jms/instantiate-connection-factory/src/main/java/org/apache/activemq/artemis/jms/example/InstantiateConnectionFactoryExample.java index b0acce4..9b1e46e 100644 --- a/examples/jms/instantiate-connection-factory/src/main/java/org/apache/activemq/artemis/jms/example/InstantiateConnectionFactoryExample.java +++ b/examples/jms/instantiate-connection-factory/src/main/java/org/apache/activemq/artemis/jms/example/InstantiateConnectionFactoryExample.java @@ -23,14 +23,9 @@ import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; -import java.util.HashMap; -import java.util.Map; -import org.apache.activemq.artemis.api.core.TransportConfiguration; import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; -import org.apache.activemq.artemis.api.jms.JMSFactoryType; -import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory; -import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; /** * @@ -49,17 +44,8 @@ public class InstantiateConnectionFactoryExample // Step 1. Directly instantiate the JMS Queue object. Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); - // Step 2. Instantiate the TransportConfiguration object which contains the knowledge of what transport to use, - // The server port etc. - - Map connectionParams = new HashMap(); - connectionParams.put(TransportConstants.PORT_PROP_NAME, 61616); - - TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), - connectionParams); - - // Step 3 Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration - ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration); + // Starting with Artemis 1.0.1 you can just use the URI to instantiate the object directly + ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Step 4.Create a JMS Connection connection = cf.createConnection(); http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/interceptor/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/interceptor/pom.xml b/examples/jms/interceptor/pom.xml index 530a7b9..63ab338 100644 --- a/examples/jms/interceptor/pom.xml +++ b/examples/jms/interceptor/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -27,7 +28,7 @@ under the License. 1.0.1-SNAPSHOT - artemis-jms-interceptor-example + interceptor jar ActiveMQ Artemis JMS Interceptor Example @@ -41,72 +42,79 @@ under the License. artemis-jms-client ${project.version} - - org.apache.geronimo.specs - geronimo-jms_2.0_spec - - example - - - - org.apache.activemq - artemis-maven-plugin - - - create - - create - - - - start - - cli - - - true - tcp://localhost:61616 - - run - - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.InterceptorExample - - - - stop - - cli - - - - stop - - - - - - - org.apache.activemq.examples.jms - artemis-jms-interceptor-example - ${project.version} - - - - - + + noServer + + true + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + org.apache.activemq.examples.jms:interceptor:${project.version} + ${noServer} + ${basedir}/target/classes/activemq/server0 + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.InterceptorExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.jms + interceptor + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/interceptor/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/interceptor/readme.html b/examples/jms/interceptor/readme.html index a680c12..e4d3601 100644 --- a/examples/jms/interceptor/readme.html +++ b/examples/jms/interceptor/readme.html @@ -27,6 +27,9 @@ under the License.

JMS Interceptor 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 implement and configure a simple incoming, server-side interceptor with ActiveMQ Artemis.

ActiveMQ Artemis allows an application to use an interceptor to hook into the messaging system. All that needs to do is to implement the @@ -59,113 +62,6 @@ under the License.

With our interceptor we always return true from the intercept method. If we were to return false that signifies that no more interceptors are to run or the target is not to be called. Return false to abort processing of the packet.

- -

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

- -
    -
  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2. -
    -           InitialContext initialContext = getContext(0);
    -        
    - -
  3. We look-up the JMS queue object from JNDI
  4. -
    -           Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
    -        
    - -
  5. We look-up the JMS connection factory object from JNDI
  6. -
    -           ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
    -        
    - -
  7. We create a JMS connection
  8. -
    -           connection = cf.createConnection();
    -        
    - -
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10. -
    -           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    -        
    - -
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12. -
    -          MessageProducer messageProducer = session.createProducer(topic);
    -       
    - -
  13. We create a JMS text message that we are going to send.
  14. -
    -           TextMessage message = session.createTextMessage("This is a text message");
    -        
    - -
  15. We send message to the queue
  16. -
    -           messageProducer.send(message);
    -        
    - -
  17. We create a JMS Message Consumer to receive the message.
  18. -
    -           MessageConsumer messageConsumer = session.createConsumer(queue);
    -        
    - -
  19. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  20. -
    -           connection.start();
    -        
    - -
  21. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  22. -
    -           TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
    -        
    - -
  23. We print out the message and the new property that has been added by the interceptor
  24. -
    -           System.out.println("Received message [" + messageReceived.getText() + "] with String property: " + messageReceived.getStringProperty("newproperty"));
    -        
    - -
  25. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  26. - -
    -           
    -           finally
    -           {
    -              if (initialContext != null)
    -              {
    -                initialContext.close();
    -              }
    -              if (connection != null)
    -              {
    -                 connection.close();
    -              }
    -           }
    -           
    -        
    -
-
    -
  1. The SimpleInterceptor:
  2. -
    -           
    -           public class SimpleInterceptor implements Interceptor
    -           {
    -              public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException
    -              {
    -                 System.out.println("SimpleInterceptor gets called!");
    -                 System.out.println("Packet: " + packet.getClass().getName());
    -                 System.out.println("RemotingConnection: " + connection.getRemoteAddress());
    -
    -                 if (packet instanceof SessionSendMessage)
    -                 {
    -                    SessionSendMessage realPacket = (SessionSendMessage)packet;
    -                    Message msg = realPacket.getServerMessage();
    -                    msg.putStringProperty(new SimpleString("newproperty"), new SimpleString("Hello from interceptor!"));
    -                 }
    -                 return true;
    -              }
    -           }
    -           
    -        
    -
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/jms-auto-closeable/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/jms-auto-closeable/pom.xml b/examples/jms/jms-auto-closeable/pom.xml index 17376ab..3066fa5 100644 --- a/examples/jms/jms-auto-closeable/pom.xml +++ b/examples/jms/jms-auto-closeable/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -27,7 +28,7 @@ under the License. 1.0.1-SNAPSHOT - artemis-jms-auto-closeable-example + auto-closeable jar ActiveMQ Artemis JMS Auto Closable Example @@ -37,71 +38,78 @@ under the License. - org.apache.geronimo.specs - geronimo-jms_2.0_spec + org.apache.activemq + artemis-jms-client + ${project.version} - example - - - - org.apache.activemq - artemis-maven-plugin - - - create - - create - - - - start - - cli - - - true - tcp://localhost:61616 - - run - - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.JMSAutoCloseableExample - - - - stop - - cli - - - - stop - - - - - - - org.apache.activemq.examples.jms - artemis-jms-auto-closeable-example - ${project.version} - - - - - + + noServer + + true + + + + + org.apache.activemq + artemis-maven-plugin + + + create + + create + + + + start + + cli + + + ${noServer} + true + tcp://localhost:61616 + + run + + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.JMSAutoCloseableExample + + + + stop + + cli + + + ${noServer} + + stop + + + + + + + org.apache.activemq.examples.jms + auto-closeable + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/jms-auto-closeable/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/jms-auto-closeable/readme.html b/examples/jms/jms-auto-closeable/readme.html index 48cc246..7e71553 100644 --- a/examples/jms/jms-auto-closeable/readme.html +++ b/examples/jms/jms-auto-closeable/readme.html @@ -27,11 +27,12 @@ under the License.

JMS Auto Closable 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 JMS resources, such as connections, sessions and consumers, in JMS 2 can be automatically closed on error.

In this instance we auto close a connection after a subsequent call to a JMS producer send fails

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2. http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/jms-auto-closeable/src/main/java/org/apache/activemq/artemis/jms/example/JMSAutoCloseableExample.java ---------------------------------------------------------------------- diff --git a/examples/jms/jms-auto-closeable/src/main/java/org/apache/activemq/artemis/jms/example/JMSAutoCloseableExample.java b/examples/jms/jms-auto-closeable/src/main/java/org/apache/activemq/artemis/jms/example/JMSAutoCloseableExample.java index 555e8ff..a4f13e8 100644 --- a/examples/jms/jms-auto-closeable/src/main/java/org/apache/activemq/artemis/jms/example/JMSAutoCloseableExample.java +++ b/examples/jms/jms-auto-closeable/src/main/java/org/apache/activemq/artemis/jms/example/JMSAutoCloseableExample.java @@ -16,9 +16,12 @@ */ package org.apache.activemq.artemis.jms.example; -import javax.jms.*; -import javax.naming.InitialContext; -import java.lang.Exception; +import javax.jms.JMSContext; +import javax.jms.JMSProducer; +import javax.jms.Queue; + +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; /** * A simple JMS example that shows how AutoCloseable is used by JMS 2 resources. @@ -27,45 +30,24 @@ public class JMSAutoCloseableExample { public static void main(final String[] args) throws Exception { - InitialContext initialContext = null; + // Step 2. Perfom a lookup on the queue + Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); + + // Step 4.Create a JMS Context using the try-with-resources statement try + ( + // Even though ConnectionFactory is not closeable it would be nice to close an ActiveMQConnectionFactory + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(); + JMSContext jmsContext = cf.createContext() + ) { - // Step 1. Create an initial context to perform the JNDI lookup. - initialContext = new InitialContext(); - - // Step 2. Perfom a lookup on the queue - Queue queue = (Queue)initialContext.lookup("queue/exampleQueue"); + // Step 5. create a jms producer + JMSProducer jmsProducer = jmsContext.createProducer(); - // Step 3. Perform a lookup on the Connection Factory - ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory"); + // Step 6. Try sending a message, we don't have the appropriate privileges to do this so this will throw an exception + jmsProducer.send(queue, "A Message from JMS2!"); - // Step 4.Create a JMS Context using the try-with-resources statement - try - ( - JMSContext jmsContext = cf.createContext() - ) - { - // Step 5. create a jms producer - JMSProducer jmsProducer = jmsContext.createProducer(); - - // Step 6. Try sending a message, we don't have the appropriate privileges to do this so this will throw an exception - jmsProducer.send(queue, "this message will fail security!"); - } - catch(JMSRuntimeException e) - { - //Step 7. we can handle the new JMSRuntimeException if we want or let the exception get handled elsewhere, the - //JMSCcontext will have been closed by the time we get to this point - System.out.println("expected exception from jmsProducer.send: " + e.getMessage()); - } - } - finally - { - // Step 8. Be sure to close our Initial Context, note that we don't have to close the JMSContext as it is auto closeable - //and closed by the vm - if (initialContext != null) - { - initialContext.close(); - } + System.out.println("Received:" + jmsContext.createConsumer(queue).receiveBody(String.class)); } } } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/jms-bridge/pom.xml ---------------------------------------------------------------------- diff --git a/examples/jms/jms-bridge/pom.xml b/examples/jms/jms-bridge/pom.xml index ac21a8c..9627051 100644 --- a/examples/jms/jms-bridge/pom.xml +++ b/examples/jms/jms-bridge/pom.xml @@ -18,7 +18,8 @@ specific language governing permissions and limitations under the License. --> - + 4.0.0 @@ -27,7 +28,7 @@ under the License. 1.0.1-SNAPSHOT - artemis-jms-bridge-example + bridge jar ActiveMQ Artemis JMS Bridge Example @@ -42,115 +43,126 @@ under the License. ${project.version} - org.apache.geronimo.specs - geronimo-jms_2.0_spec + org.apache.activemq + artemis-jms-client + ${project.version} - example - - - - org.apache.activemq - artemis-maven-plugin - - - create0 - - create - - - ${basedir}/target/server0 - ${basedir}/target/classes/activemq/server0 - - - - create1 - - create - - - ${basedir}/target/server1 - ${basedir}/target/classes/activemq/server1 - - - - start0 - - cli - - - true - ${basedir}/target/server0 - tcp://localhost:61616 - - run - - server0 - - - - start1 - - cli - - - true - ${basedir}/target/server1 - tcp://localhost:61617 - - run - - server1 - - - - runClient - - runClient - - - org.apache.activemq.artemis.jms.example.JMSBridgeExample - - - - stop0 - - cli - - - ${basedir}/target/server0 - - stop - - - - - stop1 - - cli - - - ${basedir}/target/server1 - - stop - - - - - - - org.apache.activemq.examples.jms - artemis-jms-bridge-example - ${project.version} - - - - - + + noServer + + true + + + + + org.apache.activemq + artemis-maven-plugin + + + create0 + + create + + + ${noServer} + ${basedir}/target/server0 + ${basedir}/target/classes/activemq/server0 + + + + create1 + + create + + + ${noServer} + ${basedir}/target/server1 + ${basedir}/target/classes/activemq/server1 + + + + start0 + + cli + + + ${noServer} + true + ${basedir}/target/server0 + tcp://localhost:61616 + + run + + server0 + + + + start1 + + cli + + + ${noServer} + true + ${basedir}/target/server1 + tcp://localhost:61617 + + run + + server1 + + + + runClient + + runClient + + + org.apache.activemq.artemis.jms.example.JMSBridgeExample + + + + stop0 + + cli + + + ${noServer} + ${basedir}/target/server0 + + stop + + + + + stop1 + + cli + + + ${noServer} + ${basedir}/target/server1 + + stop + + + + + + + org.apache.activemq.examples.jms + bridge + ${project.version} + + + + + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/dd820318/examples/jms/jms-bridge/readme.html ---------------------------------------------------------------------- diff --git a/examples/jms/jms-bridge/readme.html b/examples/jms/jms-bridge/readme.html index 973abfd..2ded751 100644 --- a/examples/jms/jms-bridge/readme.html +++ b/examples/jms/jms-bridge/readme.html @@ -26,6 +26,9 @@ under the License.

    JMS Bridge 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 create a JMS Bridge between two ActiveMQ Artemis servers.

    The example will use two ActiveMQ Artemis servers: