java - 为 AWS 用户处理 java.net.UnknownHostException 的最佳方法是什么?

标签 java amazon-web-services exception amazon-sqs unknown-host

我的应用程序向 Amazon Simple Notification Service (SNS) 主题发送消息,但有时 (6/10) 我收到 java.net.UnknownHostException:sqs.ap-southeast-1.amazonaws.com。异常原因在amazon web services 论坛中有描述,请看:https://forums.aws.amazon.com/thread.jspa?messageID=499290&#499290 .

我的问题类似于亚马逊论坛中描述的问题,但我向主题发布消息的速度非常动态。它可以是 1 条消息/秒或 1 条消息/分钟或一小时内没有消息。我正在寻找一种更清洁、更好和安全的方法,它保证将消息发送到 SNS 主题。

问题详细描述:

Topic_Arn= 应用程序要发布消息的 SNS 主题的 arn

msg = 要在主题中发送的消息

// Just a sample example which publish message to Amazon SNS topic
class SimpleNotificationService {
    AmazonSNSClient mSnsClient = null;

    static {
        createSnsClient()
    }

    private void static createSnsClient() {
        Region region = Region.getRegion(Regions.AP_SOUTHEAST_1);
        AWSCredentials credentials = new 
                BasicAWSCredentials(AwsPropertyLoader.getInstance().getAccessKey(),        
                        AwsPropertyLoader.getInstance().getSecretKey());
        mSqsClient = new AmazonSQSClient(credentials);
        mSqsClient.setRegion(region);
    }

    public void static publishMessage(String Topic_Arn, String msg) {
        PublishRequest req = new PublishRequest(Topic_Arn, msg);
        mSnsClient.publish(req);
    }
}

调用 SimpleNotificationService 的类

class MessagingManager {

     public void sendMessage(String message) {
           String topic_arn = "arn:of:amazon:sns:topic";
           SimpleNotificationService.publishMessage(topic_arn, message);
     }
}

请注意,这是示例代码,不是我的实际代码。这里可能是类设计问题,但如果它们与问题无关,请忽略。

我的想法是在 sendMessage 中有 try-catch block ,所以当我们捕获 UnknownHostException 然后再次重试,但我不确定如何以更安全、更清晰和更好的方式编写它。

所以 MessagingManager 类看起来像这样:

class MessagingManager {

     public void sendMessage(String message) {
           String topic_arn = "arn:of:amazon:sns:topic";
           try {
               SimpleNotificationService.publishMessage(topic_arn, message);
           } catch (UnknownHostException uhe) { 
               // I need to catch AmazonClientException as aws throws
               //AmazonClientException when sees UnknownHostException. 
               // I am mentioning UnknownHostException for non-aws user to understand
               // my problem in better way.
               sendMessage(message); // Isn't unsafe? - may falls into infinite loop
           }
     }
}

我愿意接受这样的答案:java.net.UnknownHostException: Invalid hostname for server: local但我担心的是依赖于应用程序代码级别的解决方案,而不是依赖于对机器的更改。因为我的服务器应用程序将在许多盒子(开发盒子、测试盒子或生产盒子)中运行。如果机器主机文件等的更改只是保证解决方案,那么我更喜欢将其包含在代码级别更改中。

最佳答案

每个 AWS 开发工具包都实现自动重试逻辑。适用于 Java 的 AWS 开发工具包会自动重试请求,您可以使用 ClientConfiguration 类配置重试设置。

以下是创建 SNS 客户端的示例。如果遇到 UnKnownHostException,它会重试 25 次。它使用默认的 BackOff 和重试策略。如果你想拥有自己的,那么你需要实现这两个接口(interface):http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/retry/RetryPolicy.html

private void static createSnsClient() {
    Region region = Region.getRegion(Regions.AP_SOUTHEAST_1);
    AWSCredentials credentials = new 
            BasicAWSCredentials(AwsPropertyLoader.getInstance().getAccessKey(),        
                    AwsPropertyLoader.getInstance().getSecretKey());
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxErrorRetry(25);
        clientConfiguration.setRetryPolicy(new RetryPolicy(null, null, 25, true));
        mSnsClient = new AmazonSNSClient(credentials, clientConfiguration);
        mSnsClient.setRegion(region);
}

关于java - 为 AWS 用户处理 java.net.UnknownHostException 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330613/

相关文章:

java - 使用 Activiti 工作流程跳过或完成用户任务

java - 使用按钮显示/隐藏 JLabel?

amazon-web-services - Terraform ELB S3 权限问题

amazon-web-services - 如何在Ubuntu 12.04上启动Postgresql服务器

c# - 为什么 Directory::EnumerateFiles 和 DirectoryInfo::EnumerateFiles 抛出不同的异常?

java - Shogun - 缺少 Java 包

java - Tomcat 8 log4j wicket 7 配置

java - 如何从aws s3将视频上传到azure媒体服务

c# - LINQ to SQL 中超时插入数百万条记录

exception - PowerShell不断抛出异常