Skip to content

Commit 5ecf199

Browse files
committed
rolled in review comments
1 parent 4cd1542 commit 5ecf199

File tree

4 files changed

+53
-64
lines changed

4 files changed

+53
-64
lines changed

javav2/example_code/scheduler/src/main/java/com/example/eventbrideschedule/scenario/CloudFormationHelper.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,6 @@ public static void deployCloudFormationStack(String stackName, String email) {
106106
}).join();
107107
}
108108

109-
/*
110-
111-
CloudFormationWaiter waiter = CloudFormationWaiter.builder()
112-
.client(client)
113-
.build();
114-
115-
try {
116-
// Create the stack
117-
client.createStack(stackRequest);
118-
119-
// Use the waiter to wait for the stack creation to complete
120-
WaiterResponse<DescribeStacksResponse> waiterResponse = waiter.waitUntilStackCreateComplete(
121-
DescribeStacksRequest.builder().stackName(stackName).build()
122-
);
123-
124-
// Check the result of the waiter
125-
if (waiterResponse.matched().exception().isPresent()) {
126-
throw waiterResponse.matched().exception().get(); // Rethrow the exception if the waiter fails
127-
}
128-
129-
logger.info("Stack {} created successfully.", stackName);
130-
131-
} catch (Throwable e) {
132-
logger.error("Failed to create CloudFormation stack: {}", e.getMessage(), e);
133-
134-
// Fetch stack events to find the reason for rollback
135-
fetchStackFailureDetails(client, stackName);
136-
throw new RuntimeException("Stack deployment failed", e);
137-
}
138-
}
139-
*/
140-
141109
/**
142110
* Fetches and logs the details of the stack failure by describing stack events.
143111
*/
@@ -228,18 +196,12 @@ public static void destroyCloudFormationStack(String stackName) {
228196
}
229197

230198
public static Map<String, String> getStackOutputs(String stackName) {
231-
CloudFormationClient cloudFormationClient = CloudFormationClient.builder()
232-
.region(Region.US_EAST_1)
233-
.build();
234-
235-
DescribeStacksRequest describeStacksRequest = DescribeStacksRequest.builder()
199+
DescribeStacksRequest describeStacksRequest = DescribeStacksRequest.builder()
236200
.stackName(stackName)
237201
.build();
238202

239203
try {
240-
DescribeStacksResponse describeStacksResponse = cloudFormationClient.describeStacks(describeStacksRequest);
241-
242-
// Process the result
204+
DescribeStacksResponse describeStacksResponse = getCloudFormationClient().describeStacks(describeStacksRequest).join();
243205
if (describeStacksResponse.stacks().isEmpty()) {
244206
throw new RuntimeException("Stack not found: " + stackName);
245207
}

javav2/example_code/scheduler/src/main/java/com/example/eventbrideschedule/scenario/EventbridgeSchedulerActions.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import software.amazon.awssdk.regions.Region;
1515
import software.amazon.awssdk.services.scheduler.SchedulerAsyncClient;
1616
import software.amazon.awssdk.services.scheduler.model.ActionAfterCompletion;
17+
import software.amazon.awssdk.services.scheduler.model.ConflictException;
1718
import software.amazon.awssdk.services.scheduler.model.CreateScheduleGroupRequest;
1819
import software.amazon.awssdk.services.scheduler.model.CreateScheduleGroupResponse;
1920
import software.amazon.awssdk.services.scheduler.model.CreateScheduleRequest;
@@ -24,6 +25,7 @@
2425
import software.amazon.awssdk.services.scheduler.model.FlexibleTimeWindowMode;
2526
import software.amazon.awssdk.services.scheduler.model.ResourceNotFoundException;
2627
import software.amazon.awssdk.services.scheduler.model.Target;
28+
2729
import java.time.Instant;
2830
import java.util.concurrent.CompletableFuture;
2931
import java.time.Duration;
@@ -66,6 +68,7 @@ public static SchedulerAsyncClient getAsyncClient() {
6668
}
6769

6870
// snippet-start:[scheduler.javav2.create.schedule.group.main]
71+
6972
/**
7073
* Creates a new schedule group.
7174
*
@@ -81,7 +84,12 @@ public CompletableFuture<CreateScheduleGroupResponse> createScheduleGroup(String
8184
CompletableFuture<CreateScheduleGroupResponse> futureResponse = getAsyncClient().createScheduleGroup(request);
8285
futureResponse.whenComplete((response, ex) -> {
8386
if (ex != null) {
84-
throw new CompletionException("Failed to create schedule group: " + name, ex);
87+
if (ex instanceof CompletionException && ex.getCause() instanceof ConflictException) {
88+
// Rethrow the ConflictException
89+
throw (ConflictException) ex.getCause();
90+
} else {
91+
throw new CompletionException("Failed to create schedule group: " + name, ex);
92+
}
8593
} else if (response == null) {
8694
throw new RuntimeException("Failed to create schedule group: response was null");
8795
} else {
@@ -94,10 +102,12 @@ public CompletableFuture<CreateScheduleGroupResponse> createScheduleGroup(String
94102
// snippet-end:[scheduler.javav2.create.schedule.group.main]
95103

96104
// snippet-start:[scheduler.javav2.create.schedule.main]
105+
97106
/**
98107
* Creates a new schedule for a target task.
99108
*
100109
* @param name the name of the schedule
110+
* @param scheduleExpression The schedule expression that defines when the schedule should run.
101111
* @param scheduleGroupName the name of the schedule group to which the schedule belongs
102112
* @param targetArn the Amazon Resource Name (ARN) of the target task
103113
* @param roleArn the ARN of the IAM role to be used for the schedule
@@ -157,13 +167,20 @@ public CompletableFuture<Boolean> createScheduleAsync(
157167
})
158168
.whenComplete((result, ex) -> {
159169
if (ex != null) {
160-
throw new CompletionException("Error creating schedule: " + ex.getMessage(), ex);
170+
if (ex instanceof ConflictException) {
171+
// Handle ConflictException
172+
logger.error("A conflict exception occurred while creating the schedule: {}", ex.getMessage());
173+
throw new CompletionException("A conflict exception occurred while creating the schedule: " + ex.getMessage(), ex);
174+
} else {
175+
throw new CompletionException("Error creating schedule: " + ex.getMessage(), ex);
176+
}
161177
}
162178
});
163179
}
164180
// snippet-end:[scheduler.javav2.create.schedule.main]
165181

166182
// snippet-start:[scheduler.javav2.delete.schedule.group.main]
183+
167184
/**
168185
* Deletes the specified schedule group.
169186
*
@@ -182,13 +199,18 @@ public CompletableFuture<Void> deleteScheduleGroupAsync(String name) {
182199
})
183200
.whenComplete((result, ex) -> {
184201
if (ex != null) {
185-
throw new CompletionException("Error deleting schedule group: " + ex.getMessage(), ex);
202+
if (ex instanceof ResourceNotFoundException) {
203+
throw new CompletionException("The resource was not found: " + ex.getMessage(), ex);
204+
} else {
205+
throw new CompletionException("Error deleting schedule group: " + ex.getMessage(), ex);
206+
}
186207
}
187208
});
188209
}
189210
// snippet-end:[scheduler.javav2.delete.schedule.group.main]
190211

191212
// snippet-start:[scheduler.javav2.delete.schedule.main]
213+
192214
/**
193215
* Deletes a schedule with the specified name and group name.
194216
*

javav2/example_code/scheduler/src/main/java/com/example/eventbrideschedule/scenario/EventbridgeSchedulerScenario.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class EventbridgeSchedulerScenario {
4646

4747
private static final Logger logger = LoggerFactory.getLogger(EventbridgeSchedulerScenario.class);
4848
private static final Scanner scanner = new Scanner(System.in);
49-
private static final String STACK_NAME = "workflow-stack-name";
49+
private static String STACK_NAME = "workflow-stack-name";
5050
private static final String scheduleGroupName = "schedules-group";
5151

5252
private static String recurringScheduleName = "";
@@ -100,18 +100,28 @@ public static void main(String[] args) {
100100
""");
101101
waitForInputToContinue();
102102
createOneTimeSchedule();
103+
logger.info("Do you want to delete the schedule {} (y/n) ?", oneTimeScheduleName);
104+
String ans = scanner.nextLine().trim();
105+
if (ans.equalsIgnoreCase("y")) {
106+
eventbridgeActions.deleteScheduleAsync(oneTimeScheduleName,scheduleGroupName);
107+
}
103108
logger.info(DASHES);
104109

105-
logger.info("3. Create a reoccurring schedule.");
110+
logger.info("3. Create a recurring schedule.");
106111
logger.info("""
107112
A recurring schedule is a feature that allows you to schedule and manage the execution
108113
of your serverless applications or workloads on a recurring basis. For example,
109114
with EventBridge Scheduler, you can create custom schedules for your AWS Lambda functions,
110115
AWS Step Functions, and other supported event sources, enabling you to automate tasks and
111-
workflows without the need for complex infrastructure management. T
116+
workflows without the need for complex infrastructure management.
112117
""");
113118
waitForInputToContinue();
114119
createRecurringSchedule();
120+
logger.info("Do you want to delete the schedule {} (y/n) ?", oneTimeScheduleName);
121+
String ans2 = scanner.nextLine().trim();
122+
if (ans2.equalsIgnoreCase("y")) {
123+
eventbridgeActions.deleteScheduleAsync(recurringScheduleName,scheduleGroupName);
124+
}
115125
logger.info(DASHES);
116126
}
117127
} catch (Exception ex) {
@@ -138,26 +148,11 @@ public static void main(String[] args) {
138148
*/
139149
public static void cleanUp() {
140150
logger.info("First, delete the schedule group.");
151+
logger.info("When the schedule group is deleted, schedules that are part of that group are deleted.");
141152
waitForInputToContinue();
142153
try {
143154
eventbridgeActions.deleteScheduleGroupAsync(scheduleGroupName).join();
144155

145-
} catch (CompletionException ce) {
146-
Throwable cause = ce.getCause();
147-
if (cause instanceof SchedulerException schedulerException) {
148-
logger.error("Scheduler error occurred: Error message: {}, Error code {}",
149-
schedulerException.getMessage(), schedulerException.awsErrorDetails().errorCode(), schedulerException);
150-
} else {
151-
logger.error("An unexpected error occurred: {}", cause.getMessage());
152-
}
153-
return;
154-
}
155-
logger.info("Next, delete the schedules");
156-
waitForInputToContinue();
157-
try {
158-
eventbridgeActions.deleteScheduleAsync(recurringScheduleName, scheduleGroupName).join();
159-
eventbridgeActions.deleteScheduleAsync(oneTimeScheduleName, scheduleGroupName).join();
160-
161156
} catch (CompletionException ce) {
162157
Throwable cause = ce.getCause();
163158
if (cause instanceof SchedulerException schedulerException) {
@@ -169,7 +164,7 @@ public static void cleanUp() {
169164
return;
170165
}
171166

172-
logger.info("Finally, destroy the CloudFormation stack");
167+
logger.info("Destroy the CloudFormation stack");
173168
waitForInputToContinue();
174169
CloudFormationHelper.destroyCloudFormationStack(STACK_NAME);
175170
}
@@ -191,6 +186,15 @@ public static boolean prepareApplication() {
191186
String emailAddress = promptUserForEmail();
192187
logger.info("You entered {}", emailAddress);
193188

189+
logger.info("Do you want to use a custom Stack name (y/n) ?");
190+
String ans = scanner.nextLine().trim();
191+
if (ans.equalsIgnoreCase("y")) {
192+
String newStackName = scanner.nextLine();
193+
logger.info("You entered {} for the new stack name", newStackName);
194+
waitForInputToContinue();
195+
STACK_NAME = newStackName;
196+
}
197+
194198
logger.info("Get the roleArn and snsTopicArn values using a Cloudformation template.");
195199
waitForInputToContinue();
196200
CloudFormationHelper.deployCloudFormationStack(STACK_NAME, emailAddress);
@@ -204,6 +208,7 @@ public static boolean prepareApplication() {
204208
try {
205209
eventbridgeActions.createScheduleGroup(scheduleGroupName).join();
206210
logger.info("createScheduleGroupAsync completed successfully.");
211+
207212
} catch (RuntimeException e) {
208213
logger.error("Error occurred: {} ", e.getMessage());
209214
return false;

javav2/example_code/scheduler/src/main/java/com/example/eventbrideschedule/scenario/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This example shows how to use AWS SDKs to work with Amazon EventBridge Scheduler
55

66
The target SNS topic and the AWS Identity and Access Management (IAM) role used with the schedules are created as part of an AWS CloudFormation stack that is deployed at the start of the workflow, and deleted when the workflow is complete.
77

8-
![Object Lock Features](../../../../../../../../../../workflows/eventbridge_scheduler/resources/scheduler-workflow.png)
8+
![Scheduler application structure](../../../../../../../../../../workflows/eventbridge_scheduler/resources/scheduler-workflow.png)
99

1010
This workflow demonstrates the following steps and tasks:
1111

0 commit comments

Comments
 (0)