Skip to content

Commit

Permalink
Merge pull request #77 from pyeremenko/v3
Browse files Browse the repository at this point in the history
Resolve V3 inconsistency
  • Loading branch information
featalion committed Mar 25, 2015
2 parents 2a8f140 + a7ed059 commit 3c9df42
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 181 deletions.
5 changes: 1 addition & 4 deletions src/main/java/io/iron/ironmq/Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public class Cloud {
int port;
String pathPrefix = "";

public static final Cloud ironAWSUSEast = new Cloud("https", "mq-aws-us-east-1.iron.io", 443);
public static final Cloud ironAWSEUWest = new Cloud("https", "mq-aws-eu-west-1.iron.io", 443);
public static final Cloud ironRackspaceORD = new Cloud("https", "mq-rackspace-ord.iron.io", 443);
public static final Cloud ironRackspaceLON = new Cloud("https", "mq-rackspace-lon.iron.io", 443);
public static final Cloud ironAWSUSEast = new Cloud("https", "mq-v3-aws-us-east-1.iron.io", 443);

public Cloud(String url) throws MalformedURLException {
URL u = new URL(url);
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/io/iron/ironmq/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
public class Message extends MessageOptions implements Serializable {
private String body;
private long timeout;
// Long, not long, so that it's nullable. Gson doesn't serialize null,
// so we can use the default on the server and not have to know about
// it.
Expand All @@ -30,18 +29,6 @@ public Message() {}
*/
public void setBody(String body) { this.body = body; }

/**
* Returns the Message's timeout.
*/
public long getTimeout() { return timeout; }

/**
* Sets the Message's timeout.
*
* @param timeout The new timeout.
*/
public void setTimeout(long timeout) { this.timeout = timeout; }

/**
* Returns the number of times the message has been reserved.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/iron/ironmq/MessageOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class MessageOptions implements Serializable {
protected String id;
protected Long delay;
private Long timeout;

@SerializedName("reservation_id")
protected String reservationId;
Expand All @@ -31,6 +32,12 @@ public MessageOptions(String id, String reservationId) {
this.reservationId = reservationId;
}

public MessageOptions(String id, String reservationId, Long timeout) {
this.id = id;
this.reservationId = reservationId;
this.timeout = timeout;
}

/**
* Returns Id of the Message.
*/
Expand All @@ -50,6 +57,11 @@ public String getReservationId() {
*/
public long getDelay() { return delay; }

/**
* Returns the Message's timeout.
*/
public long getTimeout() { return timeout; }

/**
* Sets Id to the Message.
*
Expand All @@ -74,4 +86,11 @@ public void setReservationId(String reservationId) {
* @param delay The new delay.
*/
public void setDelay(long delay) { this.delay = delay; }

/**
* Sets the Message's timeout.
*
* @param timeout The new timeout.
*/
public void setTimeout(long timeout) { this.timeout = timeout; }
}
150 changes: 89 additions & 61 deletions src/main/java/io/iron/ironmq/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,39 @@ public Messages peek(int numberOfMessages) throws IOException {
/**
* Touching a reserved message extends its timeout to the duration specified when the message was created.
*
* @param id The ID of the message to delete.
* @param message The message to delete.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
* @deprecated It's not possible to touch a message without reservation id since v3 of IronMQ
*/
@Deprecated
public void touchMessage(String id) throws IOException {
touchMessage(id, null);
public MessageOptions touchMessage(Message message) throws IOException {
return touchMessage(message, null);
}

/**
* Touching a reserved message extends its timeout to the specified duration.
*
* @param message The message to delete.
* @param timeout After timeout (in seconds), item will be placed back onto queue.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public MessageOptions touchMessage(Message message, int timeout) throws IOException {
return touchMessage(message, (long) timeout);
}

/**
* Touching a reserved message extends its timeout to the duration specified when the message was created.
*
* @param message The message to delete.
* @param timeout After timeout (in seconds), item will be placed back onto queue.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public MessageOptions touchMessage(Message message) throws IOException {
MessageOptions messageOptions = touchMessage(message.getId(), message.getReservationId());
public MessageOptions touchMessage(Message message, Long timeout) throws IOException {
MessageOptions messageOptions = touchMessage(message.getId(), message.getReservationId(), timeout);
message.setReservationId(messageOptions.getReservationId());
return messageOptions;
}
Expand All @@ -201,12 +213,41 @@ public MessageOptions touchMessage(Message message) throws IOException {
* Touching a reserved message extends its timeout to the duration specified when the message was created.
*
* @param id The ID of the message to delete.
* @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public MessageOptions touchMessage(String id, String reservationId) throws IOException {
String payload = new Gson().toJson(new MessageOptions(reservationId));
return touchMessage(id, reservationId, null);
}

/**
* Touching a reserved message extends its timeout to the specified duration.
*
* @param id The ID of the message to delete.
* @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved.
* @param timeout After timeout (in seconds), item will be placed back onto queue.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public MessageOptions touchMessage(String id, String reservationId, int timeout) throws IOException {
return touchMessage(id, reservationId, (long) timeout);
}

/**
* Touching a reserved message extends its timeout to the duration specified when the message was created.
*
* @param id The ID of the message to delete.
* @param reservationId This id is returned when you reserve a message and must be provided to delete a message that is reserved.
* @param timeout After timeout (in seconds), item will be placed back onto queue.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public MessageOptions touchMessage(String id, String reservationId, Long timeout) throws IOException {
String payload = new Gson().toJson(new MessageOptions(null, reservationId, timeout));
IronReader reader = client.post("queues/" + name + "/messages/" + id + "/touch", payload);
try {
return new Gson().fromJson(reader.reader, MessageOptions.class);
Expand Down Expand Up @@ -237,7 +278,20 @@ public void deleteMessage(String id) throws IOException {
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void deleteMessage(String id, String reservationId) throws IOException {
String payload = new Gson().toJson(new MessageOptions(reservationId));
deleteMessage(id, reservationId, null);
}

/**
* Deletes a Message from the queue.
*
* @param id The ID of the message to delete.
* @param reservationId Reservation Id of the message. Reserved message could not be deleted without reservation Id.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void deleteMessage(String id, String reservationId, String subscriberName) throws IOException {
String payload = new Gson().toJson(new SubscribedMessageOptions(reservationId, subscriberName));
IronReader reader = client.delete("queues/" + name + "/messages/" + id, payload);
reader.close();
}
Expand Down Expand Up @@ -460,22 +514,6 @@ public Delay(int delay) {
}
}

/**
* Release reserved message after specified time. If there is no message with such id on the queue, an
* EmptyQueueException is thrown.
*
* @param id The ID of the message to release.
* @param delay The time after which the message will be released.
*
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
* @deprecated Reservation Id is required for message releasing since v3
*/
@Deprecated
public void releaseMessage(String id, int delay) throws IOException {
releaseMessage(id, null, new Long(delay));
}

/**
* Release reserved message after specified time. If there is no message with such id on the queue, an
* EmptyQueueException is thrown.
Expand Down Expand Up @@ -520,18 +558,6 @@ public void releaseMessage(String id, String reservationId, Long delay) throws I
reader.close();
}

/**
* Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribersList The array list of subscribers.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
* @deprecated Use updateSubscribers instead
*/
@Deprecated
public void addSubscribersToQueue(ArrayList<Subscriber> subscribersList) throws IOException {
this.updateSubscribers(subscribersList);
}

/**
* Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribersList The array list of subscribers.
Expand Down Expand Up @@ -565,14 +591,33 @@ public void addSubscribers(Subscribers subscribers) throws IOException {
}

/**
* Add subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribersList The array list of subscribers.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public QueueModel updateSubscribers(ArrayList<Subscriber> subscribersList) throws IOException {
QueueModel payload = new QueueModel(new QueuePushModel(subscribersList));
return this.update(payload);
public void updateSubscribers(ArrayList<Subscriber> subscribersList) throws IOException {
addSubscribers(subscribersList);
}

/**
* Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribers The array of subscribers.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void updateSubscribers(Subscriber[] subscribers) throws IOException {
addSubscribers(subscribers);
}

/**
* Update old or add new subscribers to Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribers The array of subscribers.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void updateSubscribers(Subscribers subscribers) throws IOException {
addSubscribers(subscribers);
}

/**
Expand Down Expand Up @@ -610,22 +655,6 @@ public void replaceSubscribers(Subscribers subscribers) throws IOException {
reader.close();
}


/**
* Remove subscribers from Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribersList The array list of subscribers.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void removeSubscribersFromQueue(ArrayList<Subscriber> subscribersList) throws IOException {
String url = "queues/" + name + "/subscribers";
Subscribers subscribers = new Subscribers(subscribersList);
Gson gson = new Gson();
String jsonMessages = gson.toJson(subscribers);
IronReader reader = client.delete(url, jsonMessages);
reader.close();
}

/**
* Remove subscribers from Queue. If there is no queue, an EmptyQueueException is thrown.
* @param subscribersList The array list of subscribers.
Expand Down Expand Up @@ -677,14 +706,13 @@ public SubscribersInfo getPushStatusForMessage(String messageId) throws IOExcept
/**
* Delete push message for subscriber by subscriber ID and message ID. If there is no message or subscriber,
* an EmptyQueueException is thrown.
* @param subscriberId The Subscriber ID.
* @param subscriberName The name of Subscriber.
* @param messageId The Message ID.
* @throws io.iron.ironmq.HTTPException If the IronMQ service returns a status other than 200 OK.
* @throws java.io.IOException If there is an error accessing the IronMQ server.
*/
public void deletePushMessageForSubscriber(String messageId, String subscriberId) throws IOException {
IronReader reader = client.delete("queues/" + name + "/messages/" + messageId + "/subscribers/" + subscriberId);
reader.close();
public void deletePushMessageForSubscriber(String messageId, String reservationId, String subscriberName) throws IOException {
deleteMessage(messageId, reservationId, subscriberName);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/io/iron/ironmq/SubscribedMessageOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.iron.ironmq;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;

/**
* The Message class represents a message retrieved from an IronMQ queue.
*/
public class SubscribedMessageOptions extends MessageOptions {
@SerializedName("subscriber_name")
protected String subscriberName;

public SubscribedMessageOptions() {
}

public SubscribedMessageOptions(String reservationId, String subscriberName) {
super(reservationId);
this.subscriberName = subscriberName;
}

public SubscribedMessageOptions(String id, String reservationId, String subscriberName) {
super(id, reservationId);
this.subscriberName = subscriberName;
}

public SubscribedMessageOptions(String id, String reservationId, Long timeout, String subscriberName) {
super(id, reservationId, timeout);
this.subscriberName = subscriberName;
}

/**
* Returns the name of Message's Subscriber.
*/
public String getSubscriberName() {
return subscriberName;
}

/**
* Sets the name of Message's Subscriber.
*/
public void setSubscriberName(String subscriberName) {
this.subscriberName = subscriberName;
}
}
Loading

0 comments on commit 3c9df42

Please sign in to comment.