等待者從第 1 版變更為第 2 版 - AWS SDK for Java 2.x

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

等待者從第 1 版變更為第 2 版

本主題詳細描述等待者功能從第 1 版 (v1) 變更為第 2 版 (v2)。

下表特別示範 DynamoDB 服務生的差異。其他服務的等待者遵循相同的模式。

高階變更

等待者類別與 服務屬於相同的 Maven 成品。

變更 v1 v2

Maven 相依性

<dependencyManagement> <dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-bom</artifactId> <version>1.12.6801</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>dynamodb</artifactId> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.27.102</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>dynamodb</artifactId> </dependency> </dependencies>
套件名稱 com.amazonaws.services.dynamodbv2.waiters software.amazon.awssdk.services.dynamodb.waiters
類別名稱

AmazonDynamoDBWaiters

1 最新版本 。 2 最新版本

API 變更

變更 v1 v2
建立服務生
AmazonDynamoDB client = AmazonDynamoDBClientBuilder .standard().build(); AmazonDynamoDBWaiters waiter = client.waiters();
同步:
DynamoDbClient client = DynamoDbClient.create(); DynamoDbWaiter waiter = client.waiter();

非同步:

DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create(); DynamoDbAsyncWaiter waiter = asyncClient.waiter();
等待資料表存在 同步:
waiter.tableExists() .run(new WaiterParameters<>( new DescribeTableRequest(tableName)));

非同步:

waiter.tableExists() .runAsync(new WaiterParameters() .withRequest(new DescribeTableRequest(tableName)), new WaiterHandler() { @Override public void onWaitSuccess( AmazonWebServiceRequest amazonWebServiceRequest) { System.out.println("Table creation succeeded"); } @Override public void onWaitFailure(Exception e) { e.printStackTrace(); } }).get();

同步:

WaiterResponse<DescribeTableResponse> waiterResponse = waiter.waitUntilTableExists( r -> r.tableName("myTable")); waiterResponse.matched().response() .ifPresent(System.out::println);

非同步:

waiter.waitUntilTableExists(r -> r.tableName(tableName)) .whenComplete((r, t) -> { if (t != null) { t.printStackTrace(); } else { System.out.println( "Table creation succeeded"); } }).join();

組態變更

變更 v1 v2
輪詢策略 (最大嘗試次數和固定延遲)
MaxAttemptsRetryStrategy maxAttemptsRetryStrategy = new MaxAttemptsRetryStrategy(10); FixedDelayStrategy fixedDelayStrategy = new FixedDelayStrategy(3); PollingStrategy pollingStrategy = new PollingStrategy(maxAttemptsRetryStrategy, fixedDelayStrategy); waiter.tableExists().run( new WaiterParameters<>( new DescribeTableRequest(tableName)), pollingStrategy);
FixedDelayBackoffStrategy fixedDelayBackoffStrategy = FixedDelayBackoffStrategy .create(Duration.ofSeconds(3)); waiter.waitUntilTableExists(r -> r.tableName(tableName), c -> c.maxAttempts(10) .backoffStrategy(fixedDelayBackoffStrategy));