2

我的應用程序發送消息給亞馬遜簡單通知服務(SNS)主題,但有時(6/10)我得到java.net.UnknownHostException:sqs.ap-southeast-1.amazonaws.com。亞馬遜網絡服務討論論壇中描述了異常的原因,請看:https://forums.aws.amazon.com/thread.jspa?messageID=499290&#499290爲AWS用戶處理java.net.UnknownHostException的最佳方法是什麼?

我的問題與亞馬遜論壇中所描述的類似,但是我向主題發佈消息的速度非常動態。它可以是1條消息/秒或1條消息/分鐘或在一小時內沒有消息。我正在尋找一種更清潔,更好,更安全的方法,保證向SNS主題發送消息。詳細的問題

描述:

Topic_Arn =其中應用程序想要發佈消息

MSG SNS主題的ARN =消息到在主題發送

// 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); 
    } 
} 

請注意,這是一個山姆ple代碼,而不是我的實際代碼。這可能是課堂設計問題,但如果它們與問題無關,請忽略它們。

我的思考過程說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: Invalid hostname for server: local但我關心的是依賴於解決方案在應用程序代碼級和更少地依賴於改變機器。由於我的服務器應用程序將運行在許多盒子(開發盒,測試盒或生產盒)中。如果機器主機文件等中的更改只是保證解決方案,那麼我更喜歡包含代碼級別更改。

+0

你嘗試過什麼,喬爾@ AWS在論壇上建議? – CBredlow

+0

@CBredlow我沒有在我的代碼中添加重試邏輯,因爲joel @ AWS推薦,但是Amazon SDK爲我使用的客戶端提供了重試邏輯,並且沒有遇到錯誤。 –

回答

4

每個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); 
} 
相關問題