CloudWatch SDK for Java 2.x を使用したイベントの例 - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

CloudWatch SDK for Java 2.x を使用したイベントの例

次のコード例は、 AWS SDK for Java 2.xイベントで を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています CloudWatch 。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。このリンクには GitHub、コンテキスト内でコードをセットアップして実行する方法の手順が記載されています。

トピック

アクション

次のコード例は、Amazon CloudWatch Events イベントにターゲットを追加する方法を示しています。

SDK for Java 2.x
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.Target; /** * To run this Java V2 code example, ensure that you have setup your development * environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutTargets { public static void main(String[] args) { final String usage = """ Usage: <ruleName> <functionArn> <targetId>\s Where: ruleName - A rule name (for example, myrule). functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:xxxxxx047983:function:lamda1). targetId - A target id value. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; String functionArn = args[1]; String targetId = args[2]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWTargets(cwe, ruleName, functionArn, targetId); cwe.close(); } public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) { try { Target target = Target.builder() .arn(functionArn) .id(targetId) .build(); PutTargetsRequest request = PutTargetsRequest.builder() .targets(target) .rule(ruleName) .build(); cwe.putTargets(request); System.out.printf( "Successfully created CloudWatch events target for rule %s", ruleName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • API の詳細については、「 API リファレンスPutTargets」の「」を参照してください。 AWS SDK for Java 2.x

次のコード例は、Amazon CloudWatch Events のスケジュールされたルールを作成する方法を示しています。

SDK for Java 2.x
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleResponse; import software.amazon.awssdk.services.cloudwatchevents.model.RuleState; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutRule { public static void main(String[] args) { final String usage = """ Usage: <ruleName> roleArn>\s Where: ruleName - A rule name (for example, myrule). roleArn - A role ARN value (for example, arn:aws:iam::xxxxxx047983:user/MyUser). """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; String roleArn = args[1]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWRule(cwe, ruleName, roleArn); cwe.close(); } public static void putCWRule(CloudWatchEventsClient cwe, String ruleName, String roleArn) { try { PutRuleRequest request = PutRuleRequest.builder() .name(ruleName) .roleArn(roleArn) .scheduleExpression("rate(5 minutes)") .state(RuleState.ENABLED) .build(); PutRuleResponse response = cwe.putRule(request); System.out.printf( "Successfully created CloudWatch events rule %s with arn %s", roleArn, response.ruleArn()); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • API の詳細については、「 API リファレンスPutRule」の「」を参照してください。 AWS SDK for Java 2.x

次のコード例は、Amazon CloudWatch Events イベントを送信する方法を示しています。

SDK for Java 2.x
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutEvents { public static void main(String[] args) { final String usage = """ Usage: <resourceArn> Where: resourceArn - An Amazon Resource Name (ARN) related to the events. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String resourceArn = args[0]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWEvents(cwe, resourceArn); cwe.close(); } public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) { try { final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder() .detail(EVENT_DETAILS) .detailType("sampleSubmitted") .resources(resourceArn) .source("aws-sdk-java-cloudwatch-example") .build(); PutEventsRequest request = PutEventsRequest.builder() .entries(requestEntry) .build(); cwe.putEvents(request); System.out.println("Successfully put CloudWatch event"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • API の詳細については、「 API リファレンスPutEvents」の「」を参照してください。 AWS SDK for Java 2.x