傳送事件到 CloudWatch - AWS SDK for Java 1.x

截至 2024 年 7 月 31 日, AWS SDK for Java 1.x 已進入維護模式,並將end-of-support在 2025 年 12 月 31 日送達。我們建議您遷移至 AWS SDK for Java 2.x,以繼續接收新功能、可用性改進和安全性更新。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

傳送事件到 CloudWatch

CloudWatch事件可傳送近乎即時的系統事件串流,描述AWS資源Amazon EC2實例,Lambda函數,Kinesis串流,Amazon ECS任務,Step Functions狀態機器,Amazon SNS主題,Amazon SQS隊列或內置目標。您可以使用簡單的規則,來比對事件,並將這些事件轉傳到一或多個目標函數或串流。

新增事件

新增自訂CloudWatch事件,請致電卓越亞馬遜客户端的putEvents方法搭配PutEventsRequest對象包含一或多個PutEventsRequestEntry對象,提供有關每個事件的詳細信息。您可以指定項目的多個參數,例如事件的來源和類型、與事件相關聯的資源等等。

注意

putEvents 的每個呼叫最多可以指定 10 個事件。

匯入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutEventsRequest; import com.amazonaws.services.cloudwatchevents.model.PutEventsRequestEntry; import com.amazonaws.services.cloudwatchevents.model.PutEventsResult;

Code

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry request_entry = new PutEventsRequestEntry() .withDetail(EVENT_DETAILS) .withDetailType("sampleSubmitted") .withResources(resource_arn) .withSource("aws-sdk-java-cloudwatch-example"); PutEventsRequest request = new PutEventsRequest() .withEntries(request_entry); PutEventsResult response = cwe.putEvents(request);

新增規則

若要建立或更新規則,請呼叫卓越管理程序客户端的putRule方法搭配完全 PutRuleRequest與規則的名稱和可選參數(例如事件模式、IAM角色與規則關聯,以及排程表達式,描述了規則的運行頻率。

匯入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutRuleRequest; import com.amazonaws.services.cloudwatchevents.model.PutRuleResult; import com.amazonaws.services.cloudwatchevents.model.RuleState;

Code

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); PutRuleRequest request = new PutRuleRequest() .withName(rule_name) .withRoleArn(role_arn) .withScheduleExpression("rate(5 minutes)") .withState(RuleState.ENABLED); PutRuleResult response = cwe.putRule(request);

新增目標

目標是觸發規則時叫用的資源。目標的例子包括 Amazon EC2 執行個體、Lambda 函數、Kinesis 串流、Amazon ECS 任務、Step Functions 狀態機器和內建目標。

若要新增目標到規則,請呼叫卓越管理程序客户端的putTargets方法搭配PutTargetsRequest中包含要更新的規則和要新增至規則的目標清單。

匯入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutTargetsRequest; import com.amazonaws.services.cloudwatchevents.model.PutTargetsResult; import com.amazonaws.services.cloudwatchevents.model.Target;

Code

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); Target target = new Target() .withArn(function_arn) .withId(target_id); PutTargetsRequest request = new PutTargetsRequest() .withTargets(target) .withRule(rule_name); PutTargetsResult response = cwe.putTargets(request);

詳細資訊