私のアプリケーションは Amazon Simple Notification Service (SNS) トピックにメッセージを送信しますが、いつか (6/10) java.net.UnknownHostException:sqs.ap-southeast-1.amazonaws.com を受け取ります。例外の理由は、アマゾン ウェブ サービスのディスカッション フォーラムで説明されています。
私の問題は、Amazon のフォーラムで説明されているものと似ていますが、トピックへのメッセージの発行率は非常に動的です。1 メッセージ/秒、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 ブロックを含めるように指示されているため、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: サーバーのホスト名が無効です: ローカルですが、私の懸念は、アプリケーション コード レベルでのソリューションに依存し、マシンへの変更への依存度が低いことです。私のサーバー アプリケーションは多くのボックス (開発ボックス、テスト ボックス、または実稼働ボックス) で実行される予定です。マシンのホスト ファイルなどの変更が唯一の保証された解決策である場合は、コード レベルの変更を含めることをお勧めします。