Search in sources :

Example 1 with WaiterResponse

use of software.amazon.awssdk.core.waiters.WaiterResponse in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncCreateTable method createTable.

// snippet-start:[dynamodb.java2.dbasync.table.main]
public static void createTable(DynamoDbAsyncClient client, String tableName, String key) {
    // Create a DynamoDbAsyncWaiter object
    DynamoDbAsyncWaiter asyncWaiter = client.waiter();
    // Create the CreateTableRequest object
    CreateTableRequest request = CreateTableRequest.builder().attributeDefinitions(AttributeDefinition.builder().attributeName(key).attributeType(ScalarAttributeType.S).build()).keySchema(KeySchemaElement.builder().attributeName(key).keyType(KeyType.HASH).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).tableName(tableName).build();
    // Create the table by using the DynamoDbAsyncClient object
    CompletableFuture<CreateTableResponse> response = client.createTable(request);
    // When future is complete (either successfully or in error) handle the response
    response.whenComplete((table, err) -> {
        try {
            if (table != null) {
                // Create a DescribeTableRequest object required for waiter functionality
                DescribeTableRequest tableRequest = DescribeTableRequest.builder().tableName(table.tableDescription().tableName()).build();
                CompletableFuture<WaiterResponse<DescribeTableResponse>> waiterResponse = asyncWaiter.waitUntilTableExists(tableRequest);
                // Fires when the table is ready
                waiterResponse.whenComplete((r, t) -> {
                    // print out the new table's ARN when its ready
                    String tableARN = r.matched().response().get().table().tableArn();
                    System.out.println("The table " + tableARN + " is ready");
                });
                waiterResponse.join();
            } else {
                // Handle error
                err.printStackTrace();
            }
        } finally {
            // Lets the application shut down. Only close the client when you are completely done with it.
            client.close();
        }
    });
    response.join();
}
Also used : WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) DynamoDbAsyncWaiter(software.amazon.awssdk.services.dynamodb.waiters.DynamoDbAsyncWaiter) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Aggregations

WaiterResponse (software.amazon.awssdk.core.waiters.WaiterResponse)1 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)1 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)1 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)1 DynamoDbAsyncWaiter (software.amazon.awssdk.services.dynamodb.waiters.DynamoDbAsyncWaiter)1