diff --git a/labs/sqs/src/main/java/org/jclouds/sqs/SQSApi.java b/labs/sqs/src/main/java/org/jclouds/sqs/SQSApi.java index 0be40a4fe6..bc39a283d5 100644 --- a/labs/sqs/src/main/java/org/jclouds/sqs/SQSApi.java +++ b/labs/sqs/src/main/java/org/jclouds/sqs/SQSApi.java @@ -166,6 +166,48 @@ public interface SQSApi { */ void deleteMessage(URI queue, String receiptHandle); + /** + * The ChangeMessageVisibility action changes the visibility timeout of a + * specified message in a queue to a new value. The maximum allowed timeout + * value you can set the value to is 12 hours. This means you can't extend + * the timeout of a message in an existing queue to more than a total + * visibility timeout of 12 hours. (For more information visibility timeout, + * see Visibility Timeout in the Amazon SQS Developer Guide.) + * + * For example, let's say the timeout for the queue is 30 seconds, and you + * receive a message. Once you're 20 seconds into the timeout for that + * message (i.e., you have 10 seconds left), you extend it by 60 seconds by + * calling ChangeMessageVisibility with VisibilityTimeoutset to 60 seconds. + * You have then changed the remaining visibility timeout from 10 seconds to + * 60 seconds. + * + *

Important

+ * + * If you attempt to set the VisibilityTimeout to an amount more than the + * maximum time left, Amazon SQS returns an error. It will not automatically + * recalculate and increase the timeout to the maximum time remaining. + * + *

Important

+ * + * Unlike with a queue, when you change the visibility timeout for a specific + * message, that timeout value is applied immediately but is not saved in + * memory for that message. If you don't delete a message after it is + * received, the visibility timeout for the message the next time it is + * received reverts to the original timeout value, not the value you set with + * the ChangeMessageVisibility action. + * + * @param queue + * the queue the message is in + * @param receiptHandle + * The receipt handle associated with the message whose visibility + * timeout you want to change. This parameter is returned by the + * ReceiveMessage action. + * @param visibilityTimeout + * The new value for the message's visibility timeout (in seconds) + * from 0 to 43200 (maximum 12 hours) + */ + void changeMessageVisibility(URI queue, String receiptHandle, int visibilityTimeout); + /** * The SendMessage action delivers a message to the specified queue. The * maximum allowed message size is 64 KB. diff --git a/labs/sqs/src/main/java/org/jclouds/sqs/SQSAsyncApi.java b/labs/sqs/src/main/java/org/jclouds/sqs/SQSAsyncApi.java index 8099acd174..e3fb939286 100644 --- a/labs/sqs/src/main/java/org/jclouds/sqs/SQSAsyncApi.java +++ b/labs/sqs/src/main/java/org/jclouds/sqs/SQSAsyncApi.java @@ -126,6 +126,15 @@ public interface SQSAsyncApi { @FormParams(keys = ACTION, values = "DeleteMessage") ListenableFuture deleteMessage(@EndpointParam URI queue, @FormParam("ReceiptHandle") String receiptHandle); + /** + * @see SQSApi#changeMessageVisibility + */ + @POST + @Path("/") + @FormParams(keys = ACTION, values = "ChangeMessageVisibility") + ListenableFuture changeMessageVisibility(@EndpointParam URI queue, + @FormParam("ReceiptHandle") String receiptHandle, @FormParam("VisibilityTimeout") int visibilityTimeout); + /** * @see SQSApi#sendMessage */ diff --git a/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiExpectTest.java b/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiExpectTest.java index 2ccbd1f259..96f01a442c 100644 --- a/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiExpectTest.java +++ b/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiExpectTest.java @@ -164,6 +164,36 @@ public class SQSApiExpectTest extends BaseSQSApiExpectTest { apiWhenExist.deleteMessage(URI.create("https://sqs.us-east-1.amazonaws.com/993194456877/adrian-sqs11/"), "eXJYhj5rDr9cAe"); } + + + public HttpRequest changeMessageVisibility = HttpRequest.builder() + .method("POST") + .endpoint("https://sqs.us-east-1.amazonaws.com/993194456877/adrian-sqs11/") + .addHeader("Host", "sqs.us-east-1.amazonaws.com") + .addFormParam("Action", "ChangeMessageVisibility") + .addFormParam("ReceiptHandle", "eXJYhj5rDr9cAe") + .addFormParam("Signature", "gvmSHleGLkmszYU6aURCBImuec2k0O3pg3tAYhDvkNs%3D") + .addFormParam("SignatureMethod", "HmacSHA256") + .addFormParam("SignatureVersion", "2") + .addFormParam("Timestamp", "2009-11-08T15%3A54%3A08.897Z") + .addFormParam("Version", "2011-10-01") + .addFormParam("VisibilityTimeout", "10") + .addFormParam("AWSAccessKeyId", "identity").build(); + + public void testChangeMessageVisibilityWhenResponseIs2xx() throws Exception { + + HttpResponse changeMessageVisibilityResponse = HttpResponse.builder() + .statusCode(200) + .payload( + payloadFromStringWithContentType( + "b5293cb5-d306-4a17-9048-b263635abe42", + "text/xml")).build(); + + SQSApi apiWhenExist = requestSendsResponse(changeMessageVisibility, changeMessageVisibilityResponse); + + apiWhenExist.changeMessageVisibility(URI.create("https://sqs.us-east-1.amazonaws.com/993194456877/adrian-sqs11/"), + "eXJYhj5rDr9cAe", 10); + } public HttpRequest getQueueAttributes = HttpRequest.builder() .method("POST") diff --git a/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiLiveTest.java b/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiLiveTest.java index eb6a8db36d..806d7648b6 100644 --- a/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiLiveTest.java +++ b/labs/sqs/src/test/java/org/jclouds/sqs/SQSApiLiveTest.java @@ -22,6 +22,7 @@ import static org.jclouds.sqs.options.ListQueuesOptions.Builder.queuePrefix; import static org.jclouds.sqs.options.ReceiveMessageOptions.Builder.attribute; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; import java.net.URI; import java.util.Map; @@ -111,11 +112,26 @@ public class SQSApiLiveTest extends BaseSQSApiLiveTest { assertEquals(api().receiveMessage(queue, attribute("All").visibilityTimeout(0)).getMD5(), md5); } } + + String receiptHandle; @Test(dependsOnMethods = "testReceiveMessageWithoutHidingMessage") + protected void testChangeMessageVisibility() { + for (URI queue : queues) { + // start hiding it at 5 seconds + receiptHandle = api().receiveMessage(queue, attribute("None").visibilityTimeout(5)).getReceiptHandle(); + // hidden message, so we can't see it + assertNull(api().receiveMessage(queue)); + // this should unhide it + api().changeMessageVisibility(queue, receiptHandle, 0); + // so we can see it again + assertEquals(api().receiveMessage(queue, attribute("All").visibilityTimeout(0)).getMD5(), md5); + } + } + + @Test(dependsOnMethods = "testChangeMessageVisibility") protected void testDeleteMessage() throws InterruptedException { for (URI queue : queues) { - String receiptHandle = api().receiveMessage(queue, attribute("None").visibilityTimeout(0)).getReceiptHandle(); api().deleteMessage(queue, receiptHandle); assertNoMessages(queue); }