質問

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: https://forums.aws.amazon.com/thread.jspa?messageID=499290&#499290.

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.

Description of problem in detail:

Topic_Arn= arn of SNS topic where application wants to publish message

msg = Message to send in topic

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

class which calls SimpleNotificationService

class MessagingManager {

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

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.

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.

So MessagingManager class will look something like this:

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
           }
     }
}

I am open for answers like this: java.net.UnknownHostException: Invalid hostname for server: local 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.

役に立ちましたか?

解決

Each AWS SDK implements automatic retry logic. The AWS SDK for Java automatically retries requests, and you can configure the retry settings using the ClientConfiguration class.

Below is the sample example to create SNS client. It retries for 25 times if encounters UnKnownHostException. It uses default BackOff and retry strategy. If you want to have your own then you need to implement these two interfaces: 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);
}

他のヒント

Have you considering looking into the JVM TTL for the DNS Cache?

http://docs.aws.amazon.com/AWSSdkDocsJava/latest//DeveloperGuide/java-dg-jvm-ttl.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top