簡體   English   中英

什么是對AWS用戶處理java.net.UnknownHostException的最佳方法?

[英]What can be the best approach to handle java.net.UnknownHostException for AWS users?

我的應用程序將消息發送到Amazon Simple Notification Service(SNS)主題,但有時(6/10)我收到java.net.UnknownHostException:sqs.ap-southeast-1.amazonaws.com。 亞馬遜Web服務討論論壇中描述了異常原因,請查看: https : //forums.aws.amazon.com/thread.jspa?messageID=499290&#499290

我的問題類似於在亞馬遜論壇中描述的問題,但是我將消息發布到主題的速度非常動態。 它可以是1條消息/秒或1條消息/分鍾,或者一小時內沒有消息。 我正在尋找一種更清潔,更好和安全的方法,以確保將消息發送到SNS主題。

問題的詳細描述:

Topic_Arn =有關應用程序要在其中發布消息的SNS主題的信息

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塊,所以當我們捕獲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:服務器的無效主機名:本地,但是我擔心的是依賴於應用程序代碼級別的解決方案,而較少依賴於對計算機的更改。 由於我的服務器應用程序將在許多設備中運行(開發人員設備,測試設備或生產設備)。 如果僅保證機器主機文件或其他內容的更改是解決方案,那么我寧願將其包含在代碼級更改中。

每個AWS開發工具包均實現自動重試邏輯。 適用於Java的AWS開發工具包會自動重試請求,您可以使用ClientConfiguration類配置重試設置。

下面是創建SNS客戶端的示例示例。 如果遇到UnKnownHostException,它將重試25次。 它使用默認的BackOff和重試策略。 如果要擁有自己的接口,則需要實現這兩個接口: 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM