Skip to content
Permalink
Browse files
Factored out Client ID into MarconiApi.
  • Loading branch information
Everett Toews committed Dec 13, 2013
1 parent 6b24a3c commit e219faf8d9e823e562c2d45991a5e5aa541b0cbd
Showing 8 changed files with 111 additions and 128 deletions.
@@ -49,33 +49,46 @@ public interface MarconiApi extends Closeable {
/**
* Provides access to Queue features.
*
* @param zone The zone where this queue will live.
* @param zone The zone where this queue will live.
* @param clientId A UUID for each client instance. The UUID must be submitted in its canonical form (for example,
* 3381af92-2b9e-11e3-b191-71861300734c). The client generates the Client-ID once. Client-ID
* persists between restarts of the client so the client should reuse that same Client-ID. All
* message-related operations require the use of Client-ID in the headers to ensure that messages
* are not echoed back to the client that posted them, unless the client explicitly requests this.
*/
@Delegate
QueueApi getQueueApiForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone,
@HeaderParam("Client-ID") UUID clientId);

/**
* Provides access to Message features.
*
* @param zone The zone where this queue lives.
* @param clientId A UUID for each client instance. The UUID must be submitted in its canonical form (for example,
* 3381af92-2b9e-11e3-b191-71861300734c). The client generates the Client-ID once. Client-ID
* persists between restarts of the client so the client should reuse that same Client-ID. All
* message-related operations require the use of Client-ID in the headers to ensure that messages
* are not echoed back to the client that posted them, unless the client explicitly requests this.
* @param name Name of the queue.
*/
@Delegate
@Path("/queues/{name}")
MessageApi getMessageApiForZoneAndQueue(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("name") String name);
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone,
@HeaderParam("Client-ID") UUID clientId,
@PathParam("name") String name);

/**
* Provides access to Claim features.
*
* @param zone The zone where this queue lives.
* @param zone The zone where this queue lives.
* @param clientId A UUID for each client instance. The UUID must be submitted in its canonical form (for example,
* 3381af92-2b9e-11e3-b191-71861300734c). The client generates the Client-ID once. Client-ID
* persists between restarts of the client so the client should reuse that same Client-ID. All
* message-related operations require the use of Client-ID in the headers to ensure that messages
* are not echoed back to the client that posted them, unless the client explicitly requests this.
* @param name Name of the queue.
* @param name Name of the queue.
*/
@Delegate
@Path("/queues/{name}")
@@ -38,14 +38,12 @@
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.UUID;

import static org.jclouds.Fallbacks.EmptyListOnNotFoundOr404;
import static org.jclouds.Fallbacks.FalseOnNotFoundOr404;
@@ -63,11 +61,6 @@ public interface MessageApi {
/**
* Create message(s) on a queue.
*
* @param clientId A UUID for each client instance. The UUID must be submitted in its canonical form (for example,
* 3381af92-2b9e-11e3-b191-71861300734c). The client generates the Client-ID once. Client-ID
* persists between restarts of the client so the client should reuse that same Client-ID. All
* message-related operations require the use of Client-ID in the headers to ensure that messages
* are not echoed back to the client that posted them, unless the client explicitly requests this.
* @param messages The messages created on the queue. The number of messages allowed in one request are configurable
* by your cloud provider. Consult your cloud provider documentation to learn the maximum.
*/
@@ -76,14 +69,12 @@ public interface MessageApi {
@Path("/messages")
@ResponseParser(ParseMessagesCreated.class)
@Fallback(NullOnNotFoundOr404.class)
MessagesCreated create(@HeaderParam("Client-ID") UUID clientId,
@BinderParam(BindToJsonPayload.class) List<CreateMessage> messages);
MessagesCreated create(@BinderParam(BindToJsonPayload.class) List<CreateMessage> messages);

/**
* Streams the messages off of a queue. In a very active queue it's possible that you could continuously stream
* messages indefinitely.
*
* @param clientId A UUID for each client instance.
* @param options Options for streaming messages to your client.
*/
@Named("message:stream")
@@ -92,13 +83,11 @@ MessagesCreated create(@HeaderParam("Client-ID") UUID clientId,
@Consumes(MediaType.APPLICATION_JSON)
@Fallback(EmptyPaginatedCollectionOnNotFoundOr404.class)
@Path("/messages")
MessageStream stream(@HeaderParam("Client-ID") UUID clientId,
StreamMessagesOptions... options);
MessageStream stream(StreamMessagesOptions... options);

/**
* Lists specific messages. Unlike the stream method, a client's own messages are always returned in this operation.
*
* @param clientId A UUID for each client instance.
* @param ids Specifies the IDs of the messages to list.
*/
@Named("message:list")
@@ -107,13 +96,11 @@ MessageStream stream(@HeaderParam("Client-ID") UUID clientId,
@Consumes(MediaType.APPLICATION_JSON)
@Path("/messages")
@Fallback(EmptyListOnNotFoundOr404.class)
List<Message> list(@HeaderParam("Client-ID") UUID clientId,
@BinderParam(BindIdsToQueryParam.class) Iterable<String> ids);
List<Message> list(@BinderParam(BindIdsToQueryParam.class) Iterable<String> ids);

/**
* Gets a specific message. Unlike the stream method, a client's own messages are always returned in this operation.
*
* @param clientId A UUID for each client instance.
* @param id Specific ID of the message to get.
*/
@Named("message:get")
@@ -122,31 +109,27 @@ List<Message> list(@HeaderParam("Client-ID") UUID clientId,
@Consumes(MediaType.APPLICATION_JSON)
@Path("/messages/{message_id}")
@Fallback(NullOnNotFoundOr404.class)
Message get(@HeaderParam("Client-ID") UUID clientId,
@PathParam("message_id") String id);
Message get(@PathParam("message_id") String id);

/**
* Deletes specific messages. If any of the message IDs are malformed or non-existent, they are ignored. The
* remaining valid messages IDs are deleted.
*
* @param clientId A UUID for each client instance.
* @param ids Specifies the IDs of the messages to delete.
*/
@Named("message:delete")
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Path("/messages")
@Fallback(FalseOnNotFoundOr404.class)
boolean delete(@HeaderParam("Client-ID") UUID clientId,
@BinderParam(BindIdsToQueryParam.class) Iterable<String> ids);
boolean delete(@BinderParam(BindIdsToQueryParam.class) Iterable<String> ids);

/**
* The claimId parameter specifies that the message is deleted only if it has the specified claim ID and that claim
* has not expired. This specification is useful for ensuring only one worker processes any given message. When a
* worker's claim expires before it can delete a message that it has processed, the worker must roll back any
* actions it took based on that message because another worker can now claim and process the same message.
*
* @param clientId A UUID for each client instance.
* @param id Specific ID of the message to delete.
* @param claimId Specific claim ID of the message to delete.
*/
@@ -155,7 +138,6 @@ boolean delete(@HeaderParam("Client-ID") UUID clientId,
@Consumes(MediaType.APPLICATION_JSON)
@Path("/messages/{message_id}")
@Fallback(FalseOnNotFoundOr404.class)
boolean deleteByClaim(@HeaderParam("Client-ID") UUID clientId,
@PathParam("message_id") String id,
boolean deleteByClaim(@PathParam("message_id") String id,
@QueryParam("claim_id") String claimId);
}
@@ -18,9 +18,8 @@

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import org.jclouds.collect.IterableWithMarker;
import org.jclouds.collect.internal.Arg0ToPagedIterable;
import org.jclouds.collect.internal.ArgsToPagedIterable;
import org.jclouds.openstack.marconi.v1.MarconiApi;
import org.jclouds.openstack.marconi.v1.domain.Queue;
import org.jclouds.openstack.marconi.v1.features.QueueApi;
@@ -29,6 +28,8 @@
import org.jclouds.openstack.v2_0.options.PaginationOptions;

import javax.inject.Inject;
import java.util.List;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.openstack.marconi.v1.options.ListQueuesOptions.Builder.queryParameters;
@@ -37,7 +38,7 @@
* @author Everett Toews
*/
@Beta
public class QueuesToPagedIterable extends Arg0ToPagedIterable.FromCaller<Queue, QueuesToPagedIterable> {
public class QueuesToPagedIterable extends ArgsToPagedIterable.FromCaller<Queue, QueuesToPagedIterable> {

private final MarconiApi api;

@@ -47,10 +48,11 @@ protected QueuesToPagedIterable(MarconiApi api) {
}

@Override
protected Function<Object, IterableWithMarker<Queue>> markerToNextForArg0(Optional<Object> arg0) {
String zone = String.class.cast(arg0.get());
protected Function<Object, IterableWithMarker<Queue>> markerToNextForArgs(List<Object> args) {
String zone = String.class.cast(args.get(0));
UUID clientId = UUID.class.cast(args.get(1));

return new ListQueuesAtMarker(api.getQueueApiForZone(zone));
return new ListQueuesAtMarker(api.getQueueApiForZone(zone, clientId));
}

private static class ListQueuesAtMarker implements Function<Object, IterableWithMarker<Queue>> {
@@ -42,7 +42,7 @@ public class ClaimApiLiveTest extends BaseMarconiApiLiveTest {

public void createQueues() throws Exception {
for (String zoneId : zones) {
QueueApi queueApi = api.getQueueApiForZone(zoneId);
QueueApi queueApi = api.getQueueApiForZone(zoneId, CLIENT_ID);
boolean success = queueApi.create("jclouds-test");

assertTrue(success);
@@ -52,9 +52,8 @@ public void createQueues() throws Exception {
@Test(dependsOnMethods = { "createQueues" })
public void createMessages() throws Exception {
for (String zoneId : zones) {
MessageApi messageApi = api.getMessageApiForZoneAndQueue(zoneId, "jclouds-test");
MessageApi messageApi = api.getMessageApiForZoneAndQueue(zoneId, CLIENT_ID, "jclouds-test");

UUID clientId = UUID.fromString("3381af92-2b9e-11e3-b191-71861300734c");
String json1 = "{\"event\":{\"name\":\"Austin Java User Group\",\"attendees\":[\"bob\",\"jim\",\"sally\"]}}";
CreateMessage message1 = CreateMessage.builder().ttl(86400).body(json1).build();
String json2 = "{\"event\":{\"name\":\"SF Java User Group\",\"attendees\":[\"bob\",\"jim\",\"sally\"]}}";
@@ -63,7 +62,7 @@ public void createMessages() throws Exception {
CreateMessage message3 = CreateMessage.builder().ttl(86400).body(json3).build();
List<CreateMessage> messages = ImmutableList.of(message1, message2, message3);

MessagesCreated messagesCreated = messageApi.create(clientId, messages);
MessagesCreated messagesCreated = messageApi.create(messages);

assertNotNull(messagesCreated);
assertEquals(messagesCreated.getMessageIds().size(), 3);
@@ -123,7 +122,7 @@ public void releaseClaim() throws Exception {
@Test(dependsOnMethods = { "getClaim" })
public void delete() throws Exception {
for (String zoneId : zones) {
QueueApi queueApi = api.getQueueApiForZone(zoneId);
QueueApi queueApi = api.getQueueApiForZone(zoneId, CLIENT_ID);
boolean success = queueApi.delete("jclouds-test");

assertTrue(success);

0 comments on commit e219faf

Please sign in to comment.