AWSSDK中的Retry只能在元数据服务中使用吗?
创始人
2024-09-27 05:30:07
0

在 AWS SDK 中,Retry 并非仅适用于元数据服务。Retry 可以用于所有 AWS 服务 API 请求中。以下是一个示例,显示如何使用 RetryHandler 来执行 Amazon DynamoDB 请求以及如何在请求失败时进行自定义 Retry:

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.handlers.RequestHandler2;
import com.amazonaws.retry.RetryUtils;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.*;

import java.util.HashMap;
import java.util.Map;

public class DynamoDBRetry {

    public static void main(String[] args) {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                .withRegion("us-west-2")
                .build();

        client.getRequestHandlers().add(new RequestHandler2() {
            @Override
            public AmazonWebServiceRequest beforeMarshalling(AmazonWebServiceRequest request) {
                // Adjust request before marshalling
                return request;
            }

            @Override
            public void afterResponse(AmazonWebServiceRequest request, Object response, TimingInfo timingInfo, AmazonWebServiceResponse amazonWebServiceResponse) {
                // Adjust response after unmarshalling
            }

            @Override
            public void afterError(AmazonWebServiceRequest request, AmazonClientException e) {
                if (RetryUtils.isThrottlingException(e)) {
                    // Retry on throttling exception
                    return;
                }

                if (e instanceof AmazonServiceException) {
                    AmazonServiceException ase = (AmazonServiceException) e;
                    if (RetryUtils.isRetryableServiceException(ase)) {
                        // Throw back exception with delay for custom retry logic
                        throw ase;
                    }
                }

                // Do nothing for other exceptions (e.g. non-AWS or networking issues)
            }
        });

        Map item = new HashMap<>();
        item.put("id", new AttributeValue().withN("1"));
        item.put("name", new AttributeValue().withS("John Doe"));

        PutItemRequest putItemRequest = new PutItemRequest()
                .withTableName("my-table")
                .withItem(item);

        try {
            client.putItem(putItemRequest);
        } catch (AmazonServiceException e) {
            // Handle service exceptions
        } catch (AmazonClientException e) {
            // Handle client exceptions
        }
    }
}

在此示例中,我们通过向 AmazonDynamoDBClient 添加 RequestHandler2 的实例来实现自定义 Retry 逻辑。此处理程序

相关内容

热门资讯

透视模拟器!wpk辅助器下载,... 透视模拟器!wpk辅助器下载,途游四川小程序脚本辅助,攻略教程(有挂技巧);科技安装教程;13670...
透视软件!wepoker辅助透... 1、透视软件!wepoker辅助透视软件,随意玩透视辅助,微扑克教程(有挂辅助)。2、随意玩透视辅助...
透视ai代打!红龙poker作... 透视ai代打!红龙poker作弊指令,老k麻将有挂吗,攻略方法(有挂技巧)1、点击下载安装,微扑克w...
辅助透视!德普之星透视辅助软件... 1、辅助透视!德普之星透视辅助软件下载,中至赣牌圈祈福有用吗,玩家教程(有挂教程)。2、中至赣牌圈祈...
透视app!wpk辅助购买,欢... 透视app!wpk辅助购买,欢乐对决辅助,黑科技教程(有挂软件);小薇(透视辅助)致您一封信;亲爱欢...
透视好牌!智星菠萝辅助怎么买,... 这是一款非常优秀的丽水都来大菠萝脚本辅助 ia辅助检测软件,能够让你了解到丽水都来大菠萝脚本辅助中牌...
透视实锤!hhpoker辅助器... 透视实锤!hhpoker辅助器,闲来山水广西辅助,2025新版技巧(有挂软件);实战中需综合运用上述...
透视好牌!智星菠萝辅助,心悦填... 透视好牌!智星菠萝辅助,心悦填大坑辅助器,可靠技巧(有挂神器);最新版2024是一款经典耐玩的益智游...
透视最新!智星德州菠萝安装,越... 透视最新!智星德州菠萝安装,越乡游义乌辅助,新2025教程(有挂技巧);科技安装教程;1367043...
透视中牌率!wpk辅助软件,雀... 透视中牌率!wpk辅助软件,雀友会广东潮汕辅助透视,爆料教程(有挂方法);小薇(透视辅助)致您一封信...