[ad_1]
We’re excited to announce the overall availability of the waiters function within the AWS SDK for Java 2.x (model 2.15.0 or later). Waiters are an abstraction used to ballot a useful resource till a desired state is reached, or till it’s decided that the useful resource won’t ever enter into the specified state.
When interacting with AWS APIs which are asynchronous, prospects usually want to attend for a selected useful resource to change into accessible with a purpose to carry out additional actions on it. Let’s take an instance of AWS DynamoDB CreateTable API. After you invoke the API, the response returns instantly with a TableStatus of CREATING
, and you’ll’t invoke learn or write operations till the desk standing has been transitioned to ACTIVE
.
Writing logic to constantly ballot the desk standing may be cumbersome and error-prone. That is very true when you’re coping with a number of AWS assets, as totally different assets usually have totally different polling APIs and success states that point out the supply of that individual useful resource.
The waiter utility helps take the complexity out of it by offering a easy API that handles the polling process for you. It additionally supplies polling configurations, corresponding to the utmost variety of makes an attempt and the back-off technique between every try, which you’ll be able to customise primarily based in your use-cases. With the waiters utility, you may deal with working with the useful resource with out worrying learn how to make it possible for this useful resource is prepared.
Polling with out waiters
Let’s think about we’ve got a easy utility that creates a DynamoDB desk and must carry out a write operation after it’s created. With out waiters, the polling code utilizing the synchronous DynamoDB consumer may appear to be this:
// Polling 5 instances for a desk to change into lively
int makes an attempt = 0;
whereas (makes an attempt < 5) {
attempt {
DescribeTableResponse describeTableResponse = dynamo.describeTable(b -> b.tableName("myTable"));
TableStatus standing = describeTableResponse.desk().tableStatus();
if (standing.equals(TableStatus.ACTIVE)) {
break;
}
Thread.sleep(5000);
makes an attempt++;
} catch (ResourceNotFoundException e) {
// proceed to ballot
}
}
If you’re utilizing an asynchronous SDK consumer, polling code may very well be extra advanced. Relatively than the while-loop utilized in sync code, you would wish to write down the polling logic in CompletableFuture#whenComplete
block and use ScheduledExecutorService
to schedule the delays between every try. The code may appear to be this:
dynamoDbAsyncClient.describeTable(b -> b.tableName("myTable"))
.whenComplete((r, t) -> {
if (t != null) {
if (t instanceof ResourceNotFoundException) {
// examine if it exceeds max makes an attempt and if not, schedule subsequent try
// ...
} else {
future.completeExceptionally(new RuntimeException("fail to be lively"));
}
} else if (r.desk().tableStatus().equals(TableStatus.ACTIVE)) {
future.full(r);
} else {
// examine if it exceeds max makes an attempt and if not, schedule subsequent try
// ...
}
});
Polling with waiters
With the brand new addition of the waiters function, you solely want to write down a couple of strains of code. To instantiate a waiter occasion, the really helpful approach is to create one from an present SDK consumer by way of waiter()
technique. Beneath is the pattern code displaying you learn how to wait till a desk turns into lively utilizing synchronous waiters and learn how to wait till a desk is deleted utilizing asynchronous waiters.
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);
}
}).be part of();
Customizing waiters
The waiters created from an present SDK consumer are configured with optimum values outlined by every service for various APIs. You possibly can present override configurations on all the waiters or per request by way of WaiterOverrideConfiguration
.
Waiter-level override configuration
To override the default configurations on all the waiter, you’ll want to use the waiter builder to instantiate the waiter.
Beneath is an instance illustrating learn how to configure DynamoDbWaiter and DynamoDbAsyncWaiter:
// sync
DynamoDbWaiter waiter =
DynamoDbWaiter.builder()
.overrideConfiguration(b -> b.maxAttempts(10))
.consumer(dynamoDbClient)
.construct();
// async
DynamoDbAsyncWaiter asyncWaiter =
DynamoDbAsyncWaiter.builder()
.consumer(dynamoDbAsyncClient)
.overrideConfiguration(o -> o.backoffStrategy(
FixedDelayBackoffStrategy.create(Period.ofSeconds(2))))
.scheduledExecutorService(Executors.newScheduledThreadPool(3))
.construct();
Request-level override configuration
You may as well apply configurations to sure operations. Beneath is an instance of learn how to configure WaiterOverrideConfiguration
per request
waiter.waitUntilTableNotExists(b -> b.tableName("myTable"),
o -> o.maxAttempts(10));
asyncWaiter.waitUntilTableExists(b -> b.tableName("myTable"),
o -> o.waitTimeout(Period.ofMinutes(1)));
Conclusion
On this weblog publish, we confirmed you ways simple it’s to make the most of waiters to attend for a useful resource to change into accessible, corresponding to DynamoDB desk being lively or an S3 bucket being created. To study extra, go to our Developer Information and our API References . If in case you have any suggestions tell us by opening a GitHub subject.
[ad_2]