44package com .example .eventbridgeschedule .scenario ;
55
66// snippet-start:[scheduler.javav2.actions.main]
7+
78import org .slf4j .Logger ;
89import org .slf4j .LoggerFactory ;
910import software .amazon .awssdk .core .client .config .ClientOverrideConfiguration ;
1819import software .amazon .awssdk .services .scheduler .model .CreateScheduleRequest ;
1920import software .amazon .awssdk .services .scheduler .model .DeleteScheduleGroupRequest ;
2021import software .amazon .awssdk .services .scheduler .model .DeleteScheduleRequest ;
22+ import software .amazon .awssdk .services .scheduler .model .DeleteScheduleResponse ;
2123import software .amazon .awssdk .services .scheduler .model .FlexibleTimeWindow ;
2224import software .amazon .awssdk .services .scheduler .model .FlexibleTimeWindowMode ;
2325import software .amazon .awssdk .services .scheduler .model .ResourceNotFoundException ;
2426import software .amazon .awssdk .services .scheduler .model .Target ;
2527import java .time .Instant ;
2628import java .util .concurrent .CompletableFuture ;
2729import java .time .Duration ;
30+ import java .util .concurrent .CompletionException ;
2831
2932public class EventbridgeSchedulerActions {
3033
@@ -63,6 +66,7 @@ public static SchedulerAsyncClient getAsyncClient() {
6366 }
6467
6568 // snippet-start:[scheduler.javav2.create.schedule.group.main]
69+
6670 /**
6771 * Creates a new schedule group.
6872 *
@@ -78,8 +82,7 @@ public CompletableFuture<CreateScheduleGroupResponse> createScheduleGroup(String
7882 CompletableFuture <CreateScheduleGroupResponse > futureResponse = getAsyncClient ().createScheduleGroup (request );
7983 futureResponse .whenComplete ((response , ex ) -> {
8084 if (ex != null ) {
81- // Throw an exception if something went wrong
82- throw new RuntimeException ("Failed to create schedule group: " + name , ex );
85+ throw new CompletionException ("Failed to create schedule group: " + name , ex );
8386 } else if (response == null ) {
8487 throw new RuntimeException ("Failed to create schedule group: response was null" );
8588 } else {
@@ -114,11 +117,9 @@ public CompletableFuture<Boolean> createScheduleAsync(
114117 boolean deleteAfterCompletion ,
115118 boolean useFlexibleTimeWindow ) {
116119
117- // Define the time windows.
118120 int hoursToRun = 1 ;
119121 int flexibleTimeWindowMinutes = 10 ;
120122
121- // Build the Target for the Schedule.
122123 Target target = Target .builder ()
123124 .arn (targetArn )
124125 .roleArn (roleArn )
@@ -152,12 +153,12 @@ public CompletableFuture<Boolean> createScheduleAsync(
152153
153154 return getAsyncClient ().createSchedule (request )
154155 .thenApply (response -> {
155- logger .info ("Successfully created schedule '" + name + "' in schedule group '" + scheduleGroupName + "': " + response .scheduleArn ());
156+ logger .info ("Successfully created schedule {} in schedule group {}, The ARN is {} " , name , scheduleGroupName , response .scheduleArn ());
156157 return true ;
157158 })
158159 .whenComplete ((result , ex ) -> {
159160 if (ex != null ) {
160- throw new RuntimeException ("Error creating schedule: " + ex .getMessage (), ex );
161+ throw new CompletionException ("Error creating schedule: " + ex .getMessage (), ex );
161162 }
162163 });
163164 }
@@ -169,54 +170,51 @@ public CompletableFuture<Boolean> createScheduleAsync(
169170 *
170171 * @param name the name of the schedule group to delete
171172 * @return a {@link CompletableFuture} that completes when the schedule group has been deleted
172- * @throws RuntimeException if an error occurs while deleting the schedule group
173+ * @throws CompletionException if an error occurs while deleting the schedule group
173174 */
174175 public CompletableFuture <Void > deleteScheduleGroupAsync (String name ) {
175- try {
176- DeleteScheduleGroupRequest request = DeleteScheduleGroupRequest .builder ()
177- .name (name )
178- .build ();
176+ DeleteScheduleGroupRequest request = DeleteScheduleGroupRequest .builder ()
177+ .name (name )
178+ .build ();
179179
180- return getAsyncClient ().deleteScheduleGroup (request )
181- .thenRun (() -> {
182- logger .info ("Successfully deleted schedule group {}" , name );
183- })
184- .whenComplete ((result , ex ) -> {
185- if (ex != null ) {
186- throw new RuntimeException ("Error deleting schedule group: " + ex .getMessage (), ex );
187- }
188- });
189-
190- } catch (Exception ex ) {
191- throw new RuntimeException ("Unexpected error occurred: " + ex .getMessage (), ex );
192- }
180+ return getAsyncClient ().deleteScheduleGroup (request )
181+ .thenRun (() -> {
182+ logger .info ("Successfully deleted schedule group {}" , name );
183+ })
184+ .whenComplete ((result , ex ) -> {
185+ if (ex != null ) {
186+ throw new CompletionException ("Error deleting schedule group: " + ex .getMessage (), ex );
187+ }
188+ });
193189 }
194190 // snippet-end:[scheduler.javav2.delete.schedule.group.main]
195191
196192 // snippet-start:[scheduler.javav2.delete.schedule.main]
193+ /**
194+ * Deletes a schedule with the specified name and group name.
195+ *
196+ * @param name the name of the schedule to be deleted
197+ * @param groupName the group name of the schedule to be deleted
198+ * @return a {@link CompletableFuture} that, when completed, indicates whether the schedule was successfully deleted
199+ * @throws CompletionException if an error occurs while deleting the schedule, except for the case where the schedule is not found
200+ */
197201 public CompletableFuture <Boolean > deleteScheduleAsync (String name , String groupName ) {
198- return CompletableFuture .supplyAsync (() -> {
199- try {
200- DeleteScheduleRequest request = DeleteScheduleRequest .builder ()
201- .name (name )
202- .groupName (groupName )
203- .build ();
204-
205- getAsyncClient ().deleteSchedule (request ).get ();
206- logger .info (String .format ("Successfully deleted schedule with name '%s'." , name ));
207- return true ;
208- } catch (ResourceNotFoundException ex ) {
209- logger .info (String .format ("Failed to delete schedule with ID '%s' because the resource was not found: %s" , name , ex .getMessage ()));
210- return true ;
211- } catch (Exception ex ) {
212- logger .info (String .format ("An error occurred while deleting schedule with ID '%s': %s" , name , ex .getMessage ()));
213- return false ;
214- }
215- }).whenComplete ((result , throwable ) -> {
216- if (throwable != null ) {
217- // Handle any exceptions that occurred during the operation
218- logger .info ("Error deleting schedule: " + throwable .getMessage ());
202+ DeleteScheduleRequest request = DeleteScheduleRequest .builder ()
203+ .name (name )
204+ .groupName (groupName )
205+ .build ();
206+
207+ CompletableFuture <DeleteScheduleResponse > response = getAsyncClient ().deleteSchedule (request );
208+ return response .handle ((result , ex ) -> {
209+ if (ex != null ) {
210+ if (ex instanceof ResourceNotFoundException ) {
211+ throw new CompletionException ("Resource not found while deleting schedule with ID: " + name , ex );
212+ } else {
213+ throw new CompletionException ("Failed to delete schedule." , ex );
214+ }
219215 }
216+ logger .info ("Successfully deleted schedule with name {}." , name );
217+ return true ;
220218 });
221219 }
222220 // snippet-end:[scheduler.javav2.delete.schedule.main]
0 commit comments