/
PutEvents.java
70 lines (57 loc) · 2.47 KB
/
PutEvents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.example.cloudwatch;
// snippet-start:[cloudwatch.java2.put_events.main]
// snippet-start:[cloudwatch.java2.put_events.import]
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;
// snippet-end:[cloudwatch.java2.put_events.import]
/**
* 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);
}
}
}
// snippet-end:[cloudwatch.java2.put_events.main]