Skip to content

Commit 5139aed

Browse files
committed
refactored packages to organzie code better and added an example
1 parent 4c4b157 commit 5139aed

File tree

15 files changed

+976
-766
lines changed

15 files changed

+976
-766
lines changed

.doc_gen/metadata/cloudwatch-logs_metadata.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ cloudwatch-logs_DescribeExportTasks:
9999
- CloudWatchLogs.dotnetv3.DescribeExportTasksExammple
100100
services:
101101
cloudwatch-logs: {DescribeExportTasks}
102+
cloudwatch-logs_DescribeLogStreams:
103+
languages:
104+
Java:
105+
versions:
106+
- sdk_version: 2
107+
github: javav2/example_code/cloudwatch
108+
sdkguide:
109+
excerpts:
110+
- description:
111+
snippet_tags:
112+
- cloudwatch.javav2.describe.log group.main
113+
services:
114+
cloudwatch-logs: {DescribeLogStreams}
102115
cloudwatch-logs_DescribeLogGroups:
103116
languages:
104117
.NET:

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DisableAlarmActions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public static void main(String[] args) {
4545
cw.close();
4646
}
4747

48+
/**
49+
* Disables the actions associated with a specified CloudWatch alarm.
50+
*
51+
* @param cw the {@link CloudWatchClient} used to interact with the CloudWatch service
52+
* @param alarmName the name of the CloudWatch alarm whose actions are to be disabled
53+
*
54+
*/
4855
public static void disableActions(CloudWatchClient cw, String alarmName) {
4956
try {
5057
DisableAlarmActionsRequest request = DisableAlarmActionsRequest.builder()

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/EnableAlarmActions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public static void main(String[] args) {
4545
cw.close();
4646
}
4747

48+
/**
49+
* Enables actions on the specified Amazon CloudWatch alarm.
50+
*
51+
* <p>This method sends a request to Amazon CloudWatch to enable actions on the given alarm name.
52+
* Alarm actions can include notifications, auto scaling, or other automated responses triggered by alarm state changes.</p>
53+
*
54+
* @param cw The {@link CloudWatchClient} used to send the request.
55+
* @param alarm The name of the alarm to enable actions on.
56+
*
57+
* @throws CloudWatchException if the request fails due to client-side issues or service errors.
58+
*/
4859
public static void enableActions(CloudWatchClient cw, String alarm) {
4960
try {
5061
EnableAlarmActionsRequest request = EnableAlarmActionsRequest.builder()

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/HelloService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public static void main(String[] args) {
4444
cw.close();
4545
}
4646

47+
/**
48+
* Lists all metrics within the specified namespace using AWS CloudWatch.
49+
*
50+
* @param cw the CloudWatchClient used to interact with AWS CloudWatch
51+
* @param namespace the namespace from which to list the metrics
52+
*/
4753
public static void listMets(CloudWatchClient cw, String namespace) {
4854
try {
4955
ListMetricsRequest request = ListMetricsRequest.builder()
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,70 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
// SPDX-License-Identifier: Apache-2.0
3-
4-
package com.example.cloudwatch;
5-
6-
// snippet-start:[cloudwatch.javav2.describe_rule.main]
7-
// snippet-start:[cloudwatch.javav2.describe_rule.import]
8-
import software.amazon.awssdk.regions.Region;
9-
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
10-
import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient;
11-
import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleRequest;
12-
import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleResponse;
13-
// snippet-end:[cloudwatch.javav2.describe_rule.import]
14-
15-
/**
16-
* Before running this Java V2 code example, set up your development
17-
* environment, including your credentials.
18-
*
19-
* For more information, see the following documentation topic:
20-
*
21-
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
22-
*/
23-
public class DescribeRule {
24-
public static void main(String[] args) {
25-
final String usage = """
26-
27-
Usage:
28-
<ruleName>
29-
30-
Where:
31-
ruleName - The name of the rule to describe.
32-
""";
33-
34-
if (args.length != 1) {
35-
System.out.println(usage);
36-
System.exit(1);
37-
}
38-
39-
String ruleName = args[0];
40-
CloudWatchEventsClient cwe = CloudWatchEventsClient.builder()
41-
.region(Region.US_WEST_2)
42-
.build();
43-
44-
describeSpecificRule(cwe, ruleName);
45-
cwe.close();
46-
}
47-
48-
public static void describeSpecificRule(CloudWatchEventsClient cwe, String ruleName) {
49-
try {
50-
DescribeRuleRequest ruleRequest = DescribeRuleRequest.builder()
51-
.name(ruleName)
52-
.build();
53-
54-
DescribeRuleResponse ruleResp = cwe.describeRule(ruleRequest);
55-
String schedule = ruleResp.scheduleExpression();
56-
System.out.println("The schedule for this rule is " + schedule);
57-
58-
} catch (CloudWatchException e) {
59-
System.err.println(e.awsErrorDetails().errorMessage());
60-
System.exit(1);
61-
}
62-
}
63-
}
64-
// snippet-end:[cloudwatch.javav2.describe_rule.main]
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.cloudwatch.events;
5+
6+
// snippet-start:[cloudwatch.javav2.describe_rule.main]
7+
// snippet-start:[cloudwatch.javav2.describe_rule.import]
8+
import software.amazon.awssdk.regions.Region;
9+
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
10+
import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient;
11+
import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleRequest;
12+
import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleResponse;
13+
// snippet-end:[cloudwatch.javav2.describe_rule.import]
14+
15+
/**
16+
* Before running this Java V2 code example, set up your development
17+
* environment, including your credentials.
18+
*
19+
* For more information, see the following documentation topic:
20+
*
21+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
22+
*/
23+
public class DescribeRule {
24+
public static void main(String[] args) {
25+
final String usage = """
26+
27+
Usage:
28+
<ruleName>
29+
30+
Where:
31+
ruleName - The name of the rule to describe.
32+
""";
33+
34+
if (args.length != 1) {
35+
System.out.println(usage);
36+
System.exit(1);
37+
}
38+
39+
String ruleName = args[0];
40+
CloudWatchEventsClient cwe = CloudWatchEventsClient.builder()
41+
.region(Region.US_WEST_2)
42+
.build();
43+
44+
describeSpecificRule(cwe, ruleName);
45+
cwe.close();
46+
}
47+
48+
/**
49+
* Describes a specific CloudWatch Events rule and prints its schedule expression.
50+
*
51+
* @param cwe the CloudWatchEventsClient used to interact with AWS CloudWatch Events
52+
* @param ruleName the name of the rule to describe
53+
*/
54+
public static void describeSpecificRule(CloudWatchEventsClient cwe, String ruleName) {
55+
try {
56+
DescribeRuleRequest ruleRequest = DescribeRuleRequest.builder()
57+
.name(ruleName)
58+
.build();
59+
60+
DescribeRuleResponse ruleResp = cwe.describeRule(ruleRequest);
61+
String schedule = ruleResp.scheduleExpression();
62+
System.out.println("The schedule for this rule is " + schedule);
63+
64+
} catch (CloudWatchException e) {
65+
System.err.println(e.awsErrorDetails().errorMessage());
66+
System.exit(1);
67+
}
68+
}
69+
}
70+
// snippet-end:[cloudwatch.javav2.describe_rule.main]
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,79 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
// SPDX-License-Identifier: Apache-2.0
3-
4-
package com.example.cloudwatch;
5-
6-
// snippet-start:[cloudwatch.java2.put_events.main]
7-
// snippet-start:[cloudwatch.java2.put_events.import]
8-
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
9-
import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient;
10-
import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest;
11-
import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry;
12-
// snippet-end:[cloudwatch.java2.put_events.import]
13-
14-
/**
15-
* Before running this Java V2 code example, set up your development
16-
* environment, including your credentials.
17-
*
18-
* For more information, see the following documentation topic:
19-
*
20-
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
21-
*/
22-
public class PutEvents {
23-
public static void main(String[] args) {
24-
final String usage = """
25-
26-
Usage:
27-
<resourceArn>
28-
29-
Where:
30-
resourceArn - An Amazon Resource Name (ARN) related to the events.
31-
""";
32-
33-
if (args.length != 1) {
34-
System.out.println(usage);
35-
System.exit(1);
36-
}
37-
38-
String resourceArn = args[0];
39-
CloudWatchEventsClient cwe = CloudWatchEventsClient.builder()
40-
.build();
41-
42-
putCWEvents(cwe, resourceArn);
43-
cwe.close();
44-
}
45-
46-
public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) {
47-
try {
48-
final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }";
49-
50-
PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder()
51-
.detail(EVENT_DETAILS)
52-
.detailType("sampleSubmitted")
53-
.resources(resourceArn)
54-
.source("aws-sdk-java-cloudwatch-example")
55-
.build();
56-
57-
PutEventsRequest request = PutEventsRequest.builder()
58-
.entries(requestEntry)
59-
.build();
60-
61-
cwe.putEvents(request);
62-
System.out.println("Successfully put CloudWatch event");
63-
64-
} catch (CloudWatchException e) {
65-
System.err.println(e.awsErrorDetails().errorMessage());
66-
System.exit(1);
67-
}
68-
}
69-
}
70-
// snippet-end:[cloudwatch.java2.put_events.main]
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.cloudwatch.events;
5+
6+
// snippet-start:[cloudwatch.java2.put_events.main]
7+
// snippet-start:[cloudwatch.java2.put_events.import]
8+
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
9+
import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient;
10+
import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest;
11+
import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry;
12+
// snippet-end:[cloudwatch.java2.put_events.import]
13+
14+
/**
15+
* Before running this Java V2 code example, set up your development
16+
* environment, including your credentials.
17+
*
18+
* For more information, see the following documentation topic:
19+
*
20+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
21+
*/
22+
public class PutEvents {
23+
public static void main(String[] args) {
24+
final String usage = """
25+
26+
Usage:
27+
<resourceArn>
28+
29+
Where:
30+
resourceArn - An Amazon Resource Name (ARN) related to the events.
31+
""";
32+
33+
if (args.length != 1) {
34+
System.out.println(usage);
35+
System.exit(1);
36+
}
37+
38+
String resourceArn = args[0];
39+
CloudWatchEventsClient cwe = CloudWatchEventsClient.builder()
40+
.build();
41+
42+
putCWEvents(cwe, resourceArn);
43+
cwe.close();
44+
}
45+
46+
/**
47+
* Sends a custom event to Amazon CloudWatch Events.
48+
*
49+
* @param cwe The {@link CloudWatchEventsClient} used to interact with
50+
* Amazon CloudWatch Events.
51+
* @param resourceArn The ARN (Amazon Resource Name) of the resource associated
52+
* with the event.
53+
*
54+
*/
55+
public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) {
56+
try {
57+
final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }";
58+
59+
PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder()
60+
.detail(EVENT_DETAILS)
61+
.detailType("sampleSubmitted")
62+
.resources(resourceArn)
63+
.source("aws-sdk-java-cloudwatch-example")
64+
.build();
65+
66+
PutEventsRequest request = PutEventsRequest.builder()
67+
.entries(requestEntry)
68+
.build();
69+
70+
cwe.putEvents(request);
71+
System.out.println("Successfully put CloudWatch event");
72+
73+
} catch (CloudWatchException e) {
74+
System.err.println(e.awsErrorDetails().errorMessage());
75+
System.exit(1);
76+
}
77+
}
78+
}
79+
// snippet-end:[cloudwatch.java2.put_events.main]

0 commit comments

Comments
 (0)