You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ev...@apache.org on 2014/01/03 17:43:06 UTC
git commit: Examples for Rackspace Cloud Queues
Updated Branches:
refs/heads/master f16743ddf -> 9dc24d239
Examples for Rackspace Cloud Queues
Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/9dc24d23
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/9dc24d23
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/9dc24d23
Branch: refs/heads/master
Commit: 9dc24d2391f716dc9eceac633aa8e0f0ff651b55
Parents: f16743d
Author: Everett Toews <ev...@rackspace.com>
Authored: Tue Dec 24 11:56:19 2013 -0600
Committer: Everett Toews <ev...@rackspace.com>
Committed: Fri Jan 3 10:41:39 2014 -0600
----------------------------------------------------------------------
rackspace/README.md | 5 +
rackspace/pom.xml | 10 +
.../jclouds/examples/rackspace/SmokeTest.java | 7 +
.../rackspace/cloudqueues/Constants.java | 46 ++++
.../rackspace/cloudqueues/ProducerConsumer.java | 220 ++++++++++++++++++
.../rackspace/cloudqueues/PublishSubscribe.java | 231 +++++++++++++++++++
.../rackspace/cloudqueues/StreamMessages.java | 190 +++++++++++++++
7 files changed, 709 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/README.md
----------------------------------------------------------------------
diff --git a/rackspace/README.md b/rackspace/README.md
index f63e3fa..ad2e876 100644
--- a/rackspace/README.md
+++ b/rackspace/README.md
@@ -71,6 +71,11 @@ The [clouddatabases package](https://github.com/jclouds/jclouds-examples/tree/ma
* [TestDatabase.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddatabases/TestDatabase.java) - An example of connecting to the database from the public Internet and making a simple request.
* Other examples include deleting instances, databases, and users, and granting root access.
+The [cloudqueues package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues) demonstrates how to accomplish common tasks for working with queues in the cloud.
+
+ * [ProducerConsumer.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/ProducerConsumer.java) - An example of the Producer/Consumer pattern.
+ * [PublishSubscribe.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/PublishSubscribe.java) - An example of the Publish/Subscribe pattern.
+ * [StreamMessages.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/StreamMessages.java) - An example of streaming messages off of a queue.
## Command Line
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/pom.xml
----------------------------------------------------------------------
diff --git a/rackspace/pom.xml b/rackspace/pom.xml
index 940749d..ffbca61 100644
--- a/rackspace/pom.xml
+++ b/rackspace/pom.xml
@@ -74,6 +74,11 @@
<artifactId>rackspace-clouddatabases-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <artifactId>rackspace-cloudqueues-us</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
<!-- UK -->
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
@@ -107,6 +112,11 @@
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
+ <artifactId>rackspace-cloudqueues-uk</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs</groupId>
<artifactId>rackspace-autoscale</artifactId>
<version>${jclouds.version}</version>
</dependency>
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
index e711831..97a6a73 100644
--- a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
@@ -24,6 +24,9 @@ import org.jclouds.examples.rackspace.cloudblockstorage.*;
import org.jclouds.examples.rackspace.clouddns.*;
import org.jclouds.examples.rackspace.cloudfiles.*;
import org.jclouds.examples.rackspace.cloudloadbalancers.*;
+import org.jclouds.examples.rackspace.cloudqueues.ProducerConsumer;
+import org.jclouds.examples.rackspace.cloudqueues.PublishSubscribe;
+import org.jclouds.examples.rackspace.cloudqueues.StreamMessages;
import org.jclouds.examples.rackspace.cloudservers.*;
import org.jclouds.examples.rackspace.clouddatabases.*;
import org.jclouds.examples.rackspace.autoscale.*;
@@ -111,5 +114,9 @@ public class SmokeTest {
CreateWebhook.main(args);
ExecuteWebhook.main(args);
AutoscaleCleanup.main(args);
+
+ ProducerConsumer.main(args);
+ PublishSubscribe.main(args);
+ StreamMessages.main(args);
}
}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/Constants.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/Constants.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/Constants.java
new file mode 100644
index 0000000..5ccd8be
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/Constants.java
@@ -0,0 +1,46 @@
+/*
+ * 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.jclouds.examples.rackspace.cloudqueues;
+
+import java.util.UUID;
+
+/**
+ * Constants used by the Rackspace Examples.
+ *
+ * @author Everett Toews
+ */
+public interface Constants {
+ // The provider configures jclouds To use the Rackspace Cloud (US)
+ // To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudqueues-uk"
+ final String PROVIDER = System.getProperty("provider.cbs", "rackspace-cloudqueues-us");
+ final String ZONE = System.getProperty("zone", "IAD");
+
+ final UUID PRODUCER_ID = UUID.fromString("3381af92-2b9e-11e3-b191-71861300734a");
+ final UUID CONSUMER_ID = UUID.fromString("3381af92-2b9e-11e3-b191-71861300734b");
+ final UUID PUBLISHER_ID = UUID.fromString("3381af92-2b9e-11e3-b191-71861300734c");
+ final UUID SUBSCRIBER_ID = UUID.fromString("3381af92-2b9e-11e3-b191-71861300734d");
+ final String NAME = "jclouds-example";
+
+ final int NUM_THREADS = 3;
+
+ final String PRODUCER_NAME = "producer.name";
+ final String PUBLISHER_NAME = "publisher.name";
+ final String MESSAGE_TEXT = "message.text";
+ final String MESSAGE_NUM = "message.num";
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/ProducerConsumer.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/ProducerConsumer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/ProducerConsumer.java
new file mode 100644
index 0000000..c578298
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/ProducerConsumer.java
@@ -0,0 +1,220 @@
+/*
+ * 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.jclouds.examples.rackspace.cloudqueues;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.Closeables;
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.marconi.v1.MarconiApi;
+import org.jclouds.openstack.marconi.v1.domain.CreateMessage;
+import org.jclouds.openstack.marconi.v1.domain.Message;
+import org.jclouds.openstack.marconi.v1.features.ClaimApi;
+import org.jclouds.openstack.marconi.v1.features.MessageApi;
+import org.jclouds.openstack.marconi.v1.features.QueueApi;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import static java.lang.String.format;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.CONSUMER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_NUM;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_TEXT;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.NUM_THREADS;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PRODUCER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PRODUCER_NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.ZONE;
+
+/**
+ * Setting up a Producer/Consumer model in Cloud Queues consists of posting messages to your queue, consumers claiming
+ * messages from that queue, and then deleting the completed message.
+ *
+ * The producer-consumer mode has the following characteristics:
+ *
+ * 1. Messages are acted upon by one (and only one) worker.
+ * 2. Worker must delete message when done.
+ * 3. TTL restores message to unclaimed state if worker never finishes.
+ * 4. Ideal for dispatching jobs to multiple processors.
+ *
+ * This mode is ideal for dispatching jobs to multiple processors.
+ *
+ * @author Everett Toews
+ */
+public class ProducerConsumer implements Closeable {
+ private final MarconiApi marconiApi;
+ private final QueueApi queueApi;
+
+ /**
+ * To get a username and API key see
+ * http://apache.jclouds.org/documentation/quickstart/rackspace/
+ *
+ * The first argument (args[0]) must be your username
+ * The second argument (args[1]) must be your API key
+ */
+ public static void main(String[] args) throws IOException {
+ ProducerConsumer producerConsumer = new ProducerConsumer(args[0], args[1]);
+
+ try {
+ producerConsumer.createQueue();
+ producerConsumer.produceAndConsume();
+ producerConsumer.deleteQueue();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ producerConsumer.close();
+ }
+ }
+
+ public ProducerConsumer(String username, String apiKey) {
+ // If this application we're running *inside* the Rackspace Cloud, you would want to use the InternalUrlModule
+ // as below to have all of the Cloud Queues traffic go over the internal Rackspace Cloud network.
+ // Iterable<Module> modules = ImmutableSet.<Module> of(new InternalUrlModule());
+
+ marconiApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ // .modules(modules)
+ .buildApi(MarconiApi.class);
+ queueApi = marconiApi.getQueueApiForZoneAndClient(ZONE, PRODUCER_ID);
+ }
+
+ private void createQueue() {
+ queueApi.create(NAME);
+ }
+
+ private void produceAndConsume() throws ExecutionException, InterruptedException {
+ System.out.format("Producer Consumer%n");
+
+ ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
+
+ executorService.execute(new Consumer("1"));
+ executorService.execute(new Consumer("2"));
+
+ Future producerFuture = executorService.submit(new Producer("1"));
+ producerFuture.get();
+
+ executorService.shutdown();
+ }
+
+ private void deleteQueue() {
+ queueApi.delete(NAME);
+ }
+
+ /**
+ * Always close your service when you're done with it.
+ *
+ * Note that closing quietly like this is not necessary in Java 7.
+ * You would use try-with-resources in the main method instead.
+ */
+ public void close() throws IOException {
+ Closeables.close(marconiApi, true);
+ }
+
+ private void sleep(long millis) {
+ try {
+ Thread.sleep(millis);
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public class Producer implements Runnable {
+ private final String producerName;
+ private final MessageApi messageApi;
+
+ protected Producer(String producerName) {
+ this.producerName = producerName;
+ messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, PRODUCER_ID, NAME);
+ }
+
+ public void run() {
+ for (int i = 0; i < 32; i++) {
+ messageApi.create(produce(i));
+ sleep(250);
+ }
+ }
+
+ private List<CreateMessage> produce(int messageNum) {
+ StringBuilder bodyBuilder = new StringBuilder();
+ bodyBuilder.append(format("%s=%s%n", PRODUCER_NAME, producerName))
+ .append(format("%s=%d%n", MESSAGE_NUM, messageNum))
+ .append(format("%s=%s%n", MESSAGE_TEXT, "Queue This Way"));
+
+ CreateMessage message = CreateMessage.builder().ttl(300).body(bodyBuilder.toString()).build();
+
+ System.out.format(" Producer %s Message %s:%d%n", producerName, producerName, messageNum);
+
+ return ImmutableList.of(message);
+ }
+ }
+
+ public class Consumer implements Runnable {
+ private final String consumerName;
+ private final MessageApi messageApi;
+ private final ClaimApi claimApi;
+
+ protected Consumer(String consumerName) {
+ this.consumerName = consumerName;
+ messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, CONSUMER_ID, NAME);
+ claimApi = marconiApi.getClaimApiForZoneAndClientAndQueue(ZONE, CONSUMER_ID, NAME);
+ }
+
+ public void run() {
+ for (int i = 0; i < 32; i++) {
+ List<Message> messages = claimApi.claim(120, 60, 2);
+ consume(messages);
+ sleep(300);
+ }
+ }
+
+ private void consume(List<Message> messages) {
+ for (Message message : messages) {
+ Properties props = loadStringProperties(message.getBody());
+
+ System.out.format(" Consumer %s Message %s:%s (%s)%n", consumerName,
+ props.getProperty(PRODUCER_NAME), props.getProperty(MESSAGE_NUM), props.getProperty(MESSAGE_TEXT));
+
+ messageApi.deleteByClaim(message.getId(), message.getClaimId().get());
+ }
+ }
+
+ private Properties loadStringProperties(String body) {
+ Properties properties = new Properties();
+
+ try {
+ properties.load(new StringReader(body));
+ }
+ catch (IOException e) {
+ // IOException will never occur here because we're loading directly from a String
+ }
+
+ return properties;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/PublishSubscribe.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/PublishSubscribe.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/PublishSubscribe.java
new file mode 100644
index 0000000..63fd70d
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/PublishSubscribe.java
@@ -0,0 +1,231 @@
+/*
+ * 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.jclouds.examples.rackspace.cloudqueues;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.Closeables;
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.marconi.v1.MarconiApi;
+import org.jclouds.openstack.marconi.v1.domain.CreateMessage;
+import org.jclouds.openstack.marconi.v1.domain.Message;
+import org.jclouds.openstack.marconi.v1.domain.MessageStream;
+import org.jclouds.openstack.marconi.v1.features.MessageApi;
+import org.jclouds.openstack.marconi.v1.features.QueueApi;
+import org.jclouds.openstack.marconi.v1.options.StreamMessagesOptions;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import static java.lang.String.format;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_NUM;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_TEXT;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.NUM_THREADS;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PUBLISHER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PUBLISHER_NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.SUBSCRIBER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.ZONE;
+import static org.jclouds.openstack.marconi.v1.options.StreamMessagesOptions.Builder.limit;
+
+/**
+ * Characteristics of the Publish/Subscribe model in Cloud Queues are:
+ *
+ * 1. All subscribers listen to the messages on the queue.
+ * 2. Messages are not claimed.
+ * 3. Subscribers can send a marker/cursor to skip messages already seen.
+ * 4. TTL deletes messages eventually.
+ *
+ * Ideal for notification of events to multiple listeners at once.
+ *
+ * @author Everett Toews
+ */
+public class PublishSubscribe implements Closeable {
+ private final MarconiApi marconiApi;
+ private final QueueApi queueApi;
+
+ /**
+ * To get a username and API key see
+ * http://apache.jclouds.org/documentation/quickstart/rackspace/
+ *
+ * The first argument (args[0]) must be your username
+ * The second argument (args[1]) must be your API key
+ */
+ public static void main(String[] args) throws IOException {
+ PublishSubscribe publishSubscribe = new PublishSubscribe(args[0], args[1]);
+
+ try {
+ publishSubscribe.createQueue();
+ publishSubscribe.publishAndSubscribe();
+ publishSubscribe.deleteQueue();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ publishSubscribe.close();
+ }
+ }
+
+ public PublishSubscribe(String username, String apiKey) {
+ // If this application we're running *inside* the Rackspace Cloud, you would want to use the InternalUrlModule
+ // as below to have all of the Cloud Queues traffic go over the internal Rackspace Cloud network.
+ // Iterable<Module> modules = ImmutableSet.<Module> of(new InternalUrlModule());
+
+ marconiApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ // .modules(modules)
+ .buildApi(MarconiApi.class);
+ queueApi = marconiApi.getQueueApiForZoneAndClient(ZONE, PUBLISHER_ID);
+ }
+
+ private void createQueue() {
+ queueApi.create(NAME);
+ }
+
+ private void publishAndSubscribe() throws ExecutionException, InterruptedException {
+ System.out.format("Publisher Subcriber%n");
+
+ ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
+
+ executorService.execute(new Subscriber("1"));
+ executorService.execute(new Subscriber("2"));
+
+ Future publisherFuture = executorService.submit(new Publisher("1"));
+ publisherFuture.get();
+
+ executorService.shutdown();
+ }
+
+ private void deleteQueue() {
+ queueApi.delete(NAME);
+ }
+
+ /**
+ * Always close your service when you're done with it.
+ *
+ * Note that closing quietly like this is not necessary in Java 7.
+ * You would use try-with-resources in the main method instead.
+ */
+ public void close() throws IOException {
+ Closeables.close(marconiApi, true);
+ }
+
+ private void sleep(long millis) {
+ try {
+ Thread.sleep(millis);
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public class Publisher implements Runnable {
+ private final String publisherName;
+ private final MessageApi messageApi;
+
+ protected Publisher(String publisherName) {
+ this.publisherName = publisherName;
+ messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, PUBLISHER_ID, NAME);
+ }
+
+ public void run() {
+ for (int i = 0; i < 32; i++) {
+ messageApi.create(publish(i));
+ sleep(200);
+ }
+ }
+
+ private List<CreateMessage> publish(int messageNum) {
+ StringBuilder bodyBuilder = new StringBuilder();
+ bodyBuilder.append(format("%s=%s%n", PUBLISHER_NAME, publisherName))
+ .append(format("%s=%d%n", MESSAGE_NUM, messageNum))
+ .append(format("%s=%s%n", MESSAGE_TEXT, "Read all about it"));
+
+ CreateMessage message = CreateMessage.builder().ttl(300).body(bodyBuilder.toString()).build();
+
+ System.out.format(" Publisher %s Message %s:%d%n", publisherName, publisherName, messageNum);
+
+ return ImmutableList.of(message);
+ }
+ }
+
+ public class Subscriber implements Runnable {
+ private final String subscriberName;
+ private final MessageApi messageApi;
+ private int consecutiveSleepCount = 0;
+
+ protected Subscriber(String subscriberName) {
+ this.subscriberName = subscriberName;
+ messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, SUBSCRIBER_ID, NAME);
+ }
+
+ /**
+ * Process messages off the queue until we haven't seen any messages 3 times in a row.
+ */
+ public void run() {
+ StreamMessagesOptions streamMessagesOptions = limit(2);
+ MessageStream stream = messageApi.stream(streamMessagesOptions);
+
+ while (consecutiveSleepCount < 3) {
+ if (stream.nextMarker().isPresent()) {
+ process(stream);
+ consecutiveSleepCount = 0;
+ streamMessagesOptions = stream.nextStreamOptions();
+ }
+ else {
+ sleep(150);
+ consecutiveSleepCount++;
+ // leave the streamMessagesOptions from the previous loop as is so it can be used in the next loop
+ }
+
+ stream = messageApi.stream(streamMessagesOptions);
+ }
+ }
+
+ private void process(MessageStream messageStream) {
+ for (Message message : messageStream) {
+ Properties props = loadStringProperties(message.getBody());
+
+ System.out.format(" Subscriber %s Message %s:%s (%s)%n", subscriberName,
+ props.getProperty(PUBLISHER_NAME), props.getProperty(MESSAGE_NUM), props.getProperty(MESSAGE_TEXT));
+ }
+ }
+
+ private Properties loadStringProperties(String body) {
+ Properties properties = new Properties();
+
+ try {
+ properties.load(new StringReader(body));
+ }
+ catch (IOException e) {
+ // IOException will never occur here because we're loading directly from a String
+ }
+
+ return properties;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/9dc24d23/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/StreamMessages.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/StreamMessages.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/StreamMessages.java
new file mode 100644
index 0000000..d096ad4
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudqueues/StreamMessages.java
@@ -0,0 +1,190 @@
+/*
+ * 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.jclouds.examples.rackspace.cloudqueues;
+
+import com.google.common.collect.Lists;
+import com.google.common.io.Closeables;
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.marconi.v1.MarconiApi;
+import org.jclouds.openstack.marconi.v1.domain.CreateMessage;
+import org.jclouds.openstack.marconi.v1.domain.Message;
+import org.jclouds.openstack.marconi.v1.domain.MessageStream;
+import org.jclouds.openstack.marconi.v1.features.MessageApi;
+import org.jclouds.openstack.marconi.v1.features.QueueApi;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+
+import static java.lang.String.format;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.CONSUMER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_NUM;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.MESSAGE_TEXT;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PRODUCER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PRODUCER_NAME;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.PUBLISHER_ID;
+import static org.jclouds.examples.rackspace.cloudqueues.Constants.ZONE;
+import static org.jclouds.openstack.marconi.v1.options.StreamMessagesOptions.Builder.marker;
+
+/**
+ * Stream messages off of a queue. In a very active queue it's possible that you could continuously stream messages
+ * indefinitely.
+ *
+ * You can also resume where you left off by remembering the marker.
+ *
+ * @author Everett Toews
+ */
+public class StreamMessages implements Closeable {
+ private final MarconiApi marconiApi;
+ private final QueueApi queueApi;
+
+ /**
+ * To get a username and API key see
+ * http://apache.jclouds.org/documentation/quickstart/rackspace/
+ *
+ * The first argument (args[0]) must be your username
+ * The second argument (args[1]) must be your API key
+ */
+ public static void main(String[] args) throws IOException {
+ StreamMessages streamMessages = new StreamMessages(args[0], args[1]);
+
+ try {
+ streamMessages.createQueue();
+ streamMessages.createMessages();
+ streamMessages.streamMessages();
+ streamMessages.deleteQueue();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ streamMessages.close();
+ }
+ }
+
+ public StreamMessages(String username, String apiKey) {
+ // If this application we're running *inside* the Rackspace Cloud, you would want to use the InternalUrlModule
+ // as below to have all of the Cloud Queues traffic go over the internal Rackspace Cloud network.
+ // Iterable<Module> modules = ImmutableSet.<Module> of(new InternalUrlModule());
+
+ marconiApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ // .modules(modules)
+ .buildApi(MarconiApi.class);
+ queueApi = marconiApi.getQueueApiForZoneAndClient(ZONE, PUBLISHER_ID);
+ }
+
+ private void createQueue() {
+ queueApi.create(NAME);
+ }
+
+ private void createMessages() throws ExecutionException, InterruptedException {
+ System.out.format("Create Messages%n");
+
+ MessageApi messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, PRODUCER_ID, NAME);
+ List<CreateMessage> createMessages = Lists.newArrayList();
+
+ for (int i=0; i < 10; i++) {
+ for (int j=0; j < 10; j++) {
+ StringBuilder bodyBuilder = new StringBuilder();
+ bodyBuilder.append(format("%s=%s%n", PRODUCER_NAME, PRODUCER_ID))
+ .append(format("%s=%d%n", MESSAGE_NUM, i*10+j))
+ .append(format("%s=%s%n", MESSAGE_TEXT, "Hear Ye! Hear Ye!"));
+
+ CreateMessage createMessage = CreateMessage.builder().ttl(300).body(bodyBuilder.toString()).build();
+ createMessages.add(createMessage);
+ }
+
+ messageApi.create(createMessages);
+
+ System.out.format(" Created %d messages%n", createMessages.size());
+
+ createMessages.clear();
+ }
+ }
+
+ private void streamMessages() {
+ System.out.format("Stream Messages%n");
+
+ MessageApi messageApi = marconiApi.getMessageApiForZoneAndClientAndQueue(ZONE, CONSUMER_ID, NAME);
+ MessageStream stream = messageApi.stream();
+ String marker = "";
+
+ while(stream.nextMarker().isPresent()) {
+ for (Message message: stream) {
+ Properties messageProps = loadStringProperties(message.getBody());
+ int messageNum = Integer.valueOf(messageProps.getProperty(MESSAGE_NUM));
+
+ System.out.format(" Read message %d%n", messageNum);
+
+ if (messageNum == 49) {
+ System.out.format(" Breaking at message %d%n", messageNum);
+ // Breaking here to illustrate how to resume using the marker below
+ break;
+ }
+ }
+
+ marker = stream.nextStreamOptions().getMarker();
+ stream = messageApi.stream(stream.nextStreamOptions());
+ }
+
+ stream = messageApi.stream(marker(marker));
+
+ while(stream.nextMarker().isPresent()) {
+ for (Message message: stream) {
+ Properties messageProps = loadStringProperties(message.getBody());
+ int messageNum = Integer.valueOf(messageProps.getProperty(MESSAGE_NUM));
+
+ System.out.format(" Read message %d%n", messageNum);
+ }
+
+ stream = messageApi.stream(stream.nextStreamOptions());
+ }
+ }
+
+ private void deleteQueue() {
+ queueApi.delete(NAME);
+ }
+
+ private Properties loadStringProperties(String body) {
+ Properties properties = new Properties();
+
+ try {
+ properties.load(new StringReader(body));
+ }
+ catch (IOException e) {
+ // IOException will never occur here because we're loading directly from a String
+ }
+
+ return properties;
+ }
+
+ /**
+ * Always close your service when you're done with it.
+ *
+ * Note that closing quietly like this is not necessary in Java 7.
+ * You would use try-with-resources in the main method instead.
+ */
+ public void close() throws IOException {
+ Closeables.close(marconiApi, true);
+ }
+}