Aws-sdk-java-v2: Waiters

Created on 3 Jul 2017  Â·  6Comments  Â·  Source: aws/aws-sdk-java-v2

Review the inherited state of V1 waiters and determine which changes are necessary for V2.

(Feel free to comment on this issue with desired changes).

1.11.x Parity feature-request

Most helpful comment

This is what I'm doing in Scala using the async clients. I imagine you could do something similar in java.

def whileInProgress[Request <: AmazonWebServiceRequest, Response](
    method: Request => java.util.concurrent.CompletableFuture[Response],
    request: Request,
    isInProgress: Response ⇒ Boolean,
    duration: Duration = 30 seconds): Future[Response] = {
    def loop(): Future[Response] = {
      val fut = method(request).toScala
      fut.flatMap { response =>
        if (isInProgress(response)) {
          Thread.sleep(duration.toMillis)
          loop
        }
        fut
      }
    }

    loop
  }

All 6 comments

This is what I'm doing in Scala using the async clients. I imagine you could do something similar in java.

def whileInProgress[Request <: AmazonWebServiceRequest, Response](
    method: Request => java.util.concurrent.CompletableFuture[Response],
    request: Request,
    isInProgress: Response ⇒ Boolean,
    duration: Duration = 30 seconds): Future[Response] = {
    def loop(): Future[Response] = {
      val fut = method(request).toScala
      fut.flatMap { response =>
        if (isInProgress(response)) {
          Thread.sleep(duration.toMillis)
          loop
        }
        fut
      }
    }

    loop
  }

Is this ever going to be implemented?

Yes. Unfortunately, we cannot provide a date.

Hey all, we have released waiters in 2.15.0.

Below is the sample code of how to use it:

  • sync waiter:
DynamoDbClient dynamo = DynamoDbClient.create();
DynamoDbWaiter waiter = dynamo.waiter();

WaiterResponse<DescribeTableResponse> waiterResponse =
   waiter.waitUntilTableExists(r -> r.tableName("myTable"));

// print out the matched response with a tableStatus of ACTIVE
waiterResponse.matched().response().ifPresent(System.out::println);
  • async waiter
DynamoDbAsyncClient asyncDynamo = DynamoDbAsyncClient.create();
DynamoDbAsyncWaiter asyncWaiter = asyncDynamo.waiter();

CompletableFuture<WaiterResponse<DescribeTableResponse>> waiterResponse =
                    asyncWaiter.waitUntilTableNotExists(r -> r.tableName("myTable"));

waiterResponse.whenComplete((r, t) -> {
   if (t == null) {
      // print out the matched ResourceNotFoundException
      r.matched().exception().ifPresent(System.out::println);
   }
}).join();

To learn more, check out our blog post and developer guide:

https://aws.amazon.com/blogs/developer/using-waiters-in-the-aws-sdk-for-java-2-x/
https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/waiters.html

Let us know if you have any feedback! 😃

all works well, many thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhovell picture jhovell  Â·  4Comments

tigertoes picture tigertoes  Â·  6Comments

Jorgey7 picture Jorgey7  Â·  3Comments

ceven picture ceven  Â·  5Comments

mscharp picture mscharp  Â·  5Comments