title: What can be the best approach to handle java.net.UnknownHostException for AWS users?
body: <p>My application sends message to Amazon Simple Notification Service (SNS) topic but sometime (6/10) I get java.net.UnknownHostException:sqs.ap-southeast-1.amazonaws.com. The reason of exception is described in the amazon web services discussion forums, please look: <a href="https://forums.aws.amazon.com/thread.jspa?messageID=499290&amp;#499290" rel="nofollow noreferrer">https://forums.aws.amazon.com/thread.jspa?messageID=499290&amp;#499290</a>. </p>
  
  <p>My problem is similar to what described in forums of amazon but my rate of publishing messages to topic is very dynamic. It can be 1 message/second or 1 message/minute or no message in an hour. I am looking for a cleaner, better and safe approach, which guaranties sending of message to SNS topic. </p>
  
  <p>Description of problem in detail:</p>
  
  <p>Topic_Arn=  arn of SNS topic where application wants to publish message</p>
  
  <p>msg = Message to send in topic</p>
  
  <pre><code>// 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);
      }
  }
  </code></pre>
  
  <p>class which calls SimpleNotificationService</p>
  
  <pre><code>class MessagingManager {
  
       public void sendMessage(String message) {
             String topic_arn = "arn:of:amazon:sns:topic";
             SimpleNotificationService.publishMessage(topic_arn, message);
       }
  }
  </code></pre>
  
  <p>Please note that this is a sample code, not my actual code. Here can be class design issue but please ignore those if they are not related to problem.</p>
  
  <p>My thought process says to have try-catch block inside sendMessage, so when we catch UnknownHostException then again retry but I am not sure how to write this in safer, cleaner and better way.</p>
  
  <p>So MessagingManager class will look something like this:</p>
  
  <pre><code>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
             }
       }
  }
  </code></pre>
  
  <p>I am open for answers like this: <a href="https://stackoverflow.com/questions/6484275/what-causes-the-error-java-net-unknownhostexception">java.net.UnknownHostException: Invalid hostname for server: local</a> but my concern is to dependent on solution at application code-level and less dependent on changes to machine. As my server application is going to run in many boxes (developer boxes, testing boxes or production boxes). If changes in machine host-files or etc is only guaranted solution then I prefer that to include with code level changes.</p>