Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SDK for Java 2.x를 사용한 EventBridge 스케줄러 예제
다음 코드 예제에서는 EventBridge 스케줄러와 AWS SDK for Java 2.x 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 관련 시나리오의 컨텍스트에 따라 표시되며, 개별 서비스 함수를 직접적으로 호출하는 방법을 보여줍니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.
시작
다음 코드 예제에서는 EventBridge 스케줄러 사용을 시작하는 방법을 보여줍니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import software.amazon.awssdk.services.scheduler.SchedulerAsyncClient; import software.amazon.awssdk.services.scheduler.model.ListSchedulesRequest; import software.amazon.awssdk.services.scheduler.model.ScheduleSummary; import software.amazon.awssdk.services.scheduler.paginators.ListSchedulesPublisher; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; public class HelloScheduler { public static void main(String [] args) { listSchedulesAsync(); } /** * Lists all the schedules available. * <p> * This method uses the {@link SchedulerAsyncClient} to make an asynchronous request to * list all the schedules available. The method uses the {@link ListSchedulesPublisher} * to fetch the schedules in a paginated manner, and then processes the responses * asynchronously. */ public static void listSchedulesAsync() { SchedulerAsyncClient schedulerAsyncClient = SchedulerAsyncClient.create(); // Build the request to list schedules ListSchedulesRequest listSchedulesRequest = ListSchedulesRequest.builder().build(); // Use the paginator to fetch all schedules asynchronously. ListSchedulesPublisher paginator = schedulerAsyncClient.listSchedulesPaginator(listSchedulesRequest); List<ScheduleSummary> results = new ArrayList<>(); // Subscribe to the paginator to process the response asynchronously CompletableFuture<Void> future = paginator.subscribe(response -> { response.schedules().forEach(schedule -> { results.add(schedule); System.out.printf("Schedule: %s%n", schedule.name()); }); }); // Wait for the asynchronous operation to complete. future.join(); // After all schedules are fetched, print the total count. System.out.printf("Total of %d schedule(s) available.%n", results.size()); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 ListSchedules를 참조하세요.
-
작업
다음 코드 예시에서는 CreateSchedule
을 사용하는 방법을 보여 줍니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. /** * Creates a new schedule for a target task. * * @param name the name of the schedule * @param scheduleExpression The schedule expression that defines when the schedule should run. * @param scheduleGroupName the name of the schedule group to which the schedule belongs * @param targetArn the Amazon Resource Name (ARN) of the target task * @param roleArn the ARN of the IAM role to be used for the schedule * @param input the input data for the target task * @param deleteAfterCompletion whether to delete the schedule after it's executed * @param useFlexibleTimeWindow whether to use a flexible time window for the schedule execution * @return true if the schedule was successfully created, false otherwise */ public CompletableFuture<Boolean> createScheduleAsync( String name, String scheduleExpression, String scheduleGroupName, String targetArn, String roleArn, String input, boolean deleteAfterCompletion, boolean useFlexibleTimeWindow) { int hoursToRun = 1; int flexibleTimeWindowMinutes = 10; Target target = Target.builder() .arn(targetArn) .roleArn(roleArn) .input(input) .build(); FlexibleTimeWindow flexibleTimeWindow = FlexibleTimeWindow.builder() .mode(useFlexibleTimeWindow ? FlexibleTimeWindowMode.FLEXIBLE : FlexibleTimeWindowMode.OFF) .maximumWindowInMinutes(useFlexibleTimeWindow ? flexibleTimeWindowMinutes : null) .build(); Instant startDate = Instant.now(); Instant endDate = startDate.plus(Duration.ofHours(hoursToRun)); CreateScheduleRequest request = CreateScheduleRequest.builder() .name(name) .scheduleExpression(scheduleExpression) .groupName(scheduleGroupName) .target(target) .actionAfterCompletion(deleteAfterCompletion ? ActionAfterCompletion.DELETE : ActionAfterCompletion.NONE) .startDate(startDate) .endDate(endDate) .flexibleTimeWindow(flexibleTimeWindow) .build(); return getAsyncClient().createSchedule(request) .thenApply(response -> { logger.info("Successfully created schedule {} in schedule group {}, The ARN is {} ", name, scheduleGroupName, response.scheduleArn()); return true; }) .whenComplete((result, ex) -> { if (ex != null) { if (ex instanceof ConflictException) { // Handle ConflictException logger.error("A conflict exception occurred while creating the schedule: {}", ex.getMessage()); throw new CompletionException("A conflict exception occurred while creating the schedule: " + ex.getMessage(), ex); } else { throw new CompletionException("Error creating schedule: " + ex.getMessage(), ex); } } }); }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateSchedule을 참조하세요.
-
다음 코드 예시에서는 CreateScheduleGroup
을 사용하는 방법을 보여 줍니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. /** * Creates a new schedule group. * * @param name the name of the schedule group to be created * @return a {@link CompletableFuture} representing the asynchronous operation of creating the schedule group */ public CompletableFuture<CreateScheduleGroupResponse> createScheduleGroup(String name) { CreateScheduleGroupRequest request = CreateScheduleGroupRequest.builder() .name(name) .build(); logger.info("Initiating createScheduleGroup call for group: {}", name); CompletableFuture<CreateScheduleGroupResponse> futureResponse = getAsyncClient().createScheduleGroup(request); futureResponse.whenComplete((response, ex) -> { if (ex != null) { if (ex instanceof CompletionException && ex.getCause() instanceof ConflictException) { // Rethrow the ConflictException throw (ConflictException) ex.getCause(); } else { throw new CompletionException("Failed to create schedule group: " + name, ex); } } else if (response == null) { throw new RuntimeException("Failed to create schedule group: response was null"); } else { logger.info("Successfully created schedule group '{}': {}", name, response.scheduleGroupArn()); } }); return futureResponse; }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateScheduleGroup을 참조하세요.
-
다음 코드 예시에서는 DeleteSchedule
을 사용하는 방법을 보여 줍니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. /** * Deletes a schedule with the specified name and group name. * * @param name the name of the schedule to be deleted * @param groupName the group name of the schedule to be deleted * @return a {@link CompletableFuture} that, when completed, indicates whether the schedule was successfully deleted * @throws CompletionException if an error occurs while deleting the schedule, except for the case where the schedule is not found */ public CompletableFuture<Boolean> deleteScheduleAsync(String name, String groupName) { DeleteScheduleRequest request = DeleteScheduleRequest.builder() .name(name) .groupName(groupName) .build(); CompletableFuture<DeleteScheduleResponse> response = getAsyncClient().deleteSchedule(request); return response.handle((result, ex) -> { if (ex != null) { if (ex instanceof ResourceNotFoundException) { throw new CompletionException("Resource not found while deleting schedule with ID: " + name, ex); } else { throw new CompletionException("Failed to delete schedule.", ex); } } logger.info("Successfully deleted schedule with name {}.", name); return true; }); }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DeleteSchedule을 참조하세요.
-
다음 코드 예시에서는 DeleteScheduleGroup
을 사용하는 방법을 보여 줍니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. /** * Deletes the specified schedule group. * * @param name the name of the schedule group to delete * @return a {@link CompletableFuture} that completes when the schedule group has been deleted * @throws CompletionException if an error occurs while deleting the schedule group */ public CompletableFuture<Void> deleteScheduleGroupAsync(String name) { DeleteScheduleGroupRequest request = DeleteScheduleGroupRequest.builder() .name(name) .build(); return getAsyncClient().deleteScheduleGroup(request) .thenRun(() -> { logger.info("Successfully deleted schedule group {}", name); }) .whenComplete((result, ex) -> { if (ex != null) { if (ex instanceof ResourceNotFoundException) { throw new CompletionException("The resource was not found: " + ex.getMessage(), ex); } else { throw new CompletionException("Error deleting schedule group: " + ex.getMessage(), ex); } } }); }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DeleteScheduleGroup을 참조하세요.
-
시나리오
다음 코드 예제는 다음과 같은 작업을 수행하는 방법을 보여줍니다.
필요한 리소스가 있는 AWS CloudFormation 스택을 배포합니다.
EventBridge 스케줄러 일정 그룹을 생성합니다.
유연한 기간으로 일회성 EventBridge 스케줄러 일정을 생성합니다.
지정된 속도로 반복 EventBridge 스케줄러 일정을 생성합니다.
EventBridge 스케줄러 일정 및 일정 그룹을 삭제합니다.
리소스를 정리하고 스택을 삭제합니다.
- SDK for Java 2.x
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. 시나리오를 실행합니다.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.scheduler.model.SchedulerException; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Map; import java.util.Scanner; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; /** * This Java code example performs the following tasks for the Amazon EventBridge Scheduler workflow: * <p> * 1. Prepare the Application: * - Prompt the user for an email address to use for the subscription for the SNS topic subscription. * - Deploy the Cloud Formation template in resources/cfn_template.yaml for resource creation. * - Store the outputs of the stack into variables for use in the workflow. * - Create a schedule group for all workflow schedules. * <p> * 2. Create one-time Schedule: * - Create a one-time schedule to send an initial event. * - Use a Flexible Time Window and set the schedule to delete after completion. * - Wait for the user to receive the event email from SNS. * <p> * 3. Create a time-based schedule: * - Prompt the user for how many X times per Y hours a recurring event should be scheduled. * - Create the scheduled event for X times per hour for Y hours. * - Wait for the user to receive the event email from SNS. * - Delete the schedule when the user is finished. * <p> * 4. Clean up: * - Prompt the user for y/n answer if they want to destroy the stack and clean up all resources. * - Delete the schedule group. * - Destroy the Cloud Formation stack and wait until the stack has been removed. */ public class EventbridgeSchedulerScenario { private static final Logger logger = LoggerFactory.getLogger(EventbridgeSchedulerScenario.class); private static final Scanner scanner = new Scanner(System.in); private static String STACK_NAME = "workflow-stack-name"; private static final String scheduleGroupName = "schedules-group"; private static String recurringScheduleName = ""; private static String oneTimeScheduleName = ""; private static final EventbridgeSchedulerActions eventbridgeActions = new EventbridgeSchedulerActions(); public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static String roleArn = ""; public static String snsTopicArn = ""; public static void main(String[] args) { logger.info(DASHES); logger.info("Welcome to the Amazon EventBridge Scheduler Workflow."); logger.info(""" Amazon EventBridge Scheduler is a fully managed service that helps you schedule and execute a wide range of tasks and events in the cloud. It's designed to simplify the process of scheduling and managing recurring or one-time events, making it easier for developers and businesses to automate various workflows and processes. One of the key features of Amazon EventBridge Scheduler is its ability to schedule events based on a variety of triggers, including time-based schedules, custom event patterns, or even integration with other AWS services. For example, you can use EventBridge Scheduler to schedule a report generation task to run every weekday at 9 AM, or to trigger a Lambda function when a specific Amazon S3 object is created. This flexibility allows you to build complex and dynamic event-driven architectures that adapt to your business needs. Lets get started... """); waitForInputToContinue(); logger.info(DASHES); logger.info(DASHES); logger.info("1. Prepare the application."); waitForInputToContinue(); try { boolean prepareSuccess = prepareApplication(); logger.info(DASHES); if (prepareSuccess) { logger.info("2. Create one-time schedule."); logger.info(""" A one-time schedule in Amazon EventBridge Scheduler is an event trigger that allows you to schedule a one-time event to run at a specific date and time. This is useful for executing a specific task or workflow at a predetermined time, without the need for recurring or complex scheduling. """); waitForInputToContinue(); createOneTimeSchedule(); logger.info("Do you want to delete the schedule {} (y/n) ?", oneTimeScheduleName); String ans = scanner.nextLine().trim(); if (ans.equalsIgnoreCase("y")) { eventbridgeActions.deleteScheduleAsync(oneTimeScheduleName,scheduleGroupName); } logger.info(DASHES); logger.info("3. Create a recurring schedule."); logger.info(""" A recurring schedule is a feature that allows you to schedule and manage the execution of your serverless applications or workloads on a recurring basis. For example, with EventBridge Scheduler, you can create custom schedules for your AWS Lambda functions, AWS Step Functions, and other supported event sources, enabling you to automate tasks and workflows without the need for complex infrastructure management. """); waitForInputToContinue(); createRecurringSchedule(); logger.info("Do you want to delete the schedule {} (y/n) ?", oneTimeScheduleName); String ans2 = scanner.nextLine().trim(); if (ans2.equalsIgnoreCase("y")) { eventbridgeActions.deleteScheduleAsync(recurringScheduleName,scheduleGroupName); } logger.info(DASHES); } } catch (Exception ex) { logger.info("There was a problem with the workflow {}, initiating cleanup...", ex.getMessage()); cleanUp(); } logger.info(DASHES); logger.info("4. Clean up the resources."); logger.info("Do you want to delete these AWS resources (y/n) ?"); String delAns = scanner.nextLine().trim(); if (delAns.equalsIgnoreCase("y")) { cleanUp(); } else { logger.info("The AWS resources will not be deleted."); } logger.info("Amazon EventBridge Scheduler workflow completed."); logger.info(DASHES); } /** * Cleans up the resources associated with the EventBridge scheduler. * If any errors occur during the cleanup process, the corresponding error messages are logged. */ public static void cleanUp() { logger.info("First, delete the schedule group."); logger.info("When the schedule group is deleted, schedules that are part of that group are deleted."); waitForInputToContinue(); try { eventbridgeActions.deleteScheduleGroupAsync(scheduleGroupName).join(); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof SchedulerException schedulerException) { logger.error("Scheduler error occurred: Error message: {}, Error code {}", schedulerException.getMessage(), schedulerException.awsErrorDetails().errorCode(), schedulerException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } return; } logger.info("Destroy the CloudFormation stack"); waitForInputToContinue(); CloudFormationHelper.destroyCloudFormationStack(STACK_NAME); } /** * Prepares the application by creating resources in a CloudFormation stack, including an SNS topic * that will be subscribed to the EventBridge Scheduler events. The user will need to confirm the subscription * in order to receive event emails. * * @return true if the application preparation was successful, false otherwise */ public static boolean prepareApplication() { logger.info(""" This example creates resources in a CloudFormation stack, including an SNS topic that will be subscribed to the EventBridge Scheduler events. You will need to confirm the subscription in order to receive event emails. """); String emailAddress = promptUserForEmail(); logger.info("You entered {}", emailAddress); logger.info("Do you want to use a custom Stack name (y/n) ?"); String ans = scanner.nextLine().trim(); if (ans.equalsIgnoreCase("y")) { String newStackName = scanner.nextLine(); logger.info("You entered {} for the new stack name", newStackName); waitForInputToContinue(); STACK_NAME = newStackName; } logger.info("Get the roleArn and snsTopicArn values using a Cloudformation template."); waitForInputToContinue(); CloudFormationHelper.deployCloudFormationStack(STACK_NAME, emailAddress); Map<String, String> stackOutputs = CloudFormationHelper.getStackOutputs(STACK_NAME); roleArn = stackOutputs.get("RoleARN"); snsTopicArn = stackOutputs.get("SNStopicARN"); logger.info("The roleARN is {}", roleArn); logger.info("The snsTopicArn is {}", snsTopicArn); try { eventbridgeActions.createScheduleGroup(scheduleGroupName).join(); logger.info("createScheduleGroupAsync completed successfully."); } catch (RuntimeException e) { logger.error("Error occurred: {} ", e.getMessage()); return false; } logger.info("Application preparation complete."); return true; } /** * Waits for the user to enter 'c' followed by <ENTER> to continue the program. * This method is used to pause the program execution and wait for user input before * proceeding. */ private static void waitForInputToContinue() { while (true) { logger.info(""); logger.info("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { logger.info("Continuing with the program..."); logger.info(""); break; } else { // Handle invalid input. logger.info("Invalid input. Please try again."); } } } /** * Prompts the user to enter an email address and validates the input. * If the provided email address is invalid, the method will prompt the user to try again. * * @return the valid email address entered by the user */ private static String promptUserForEmail() { logger.info("Enter an email address to use for event subscriptions: "); String email = scanner.nextLine(); if (!isValidEmail(email)) { logger.info("Invalid email address. Please try again."); return promptUserForEmail(); } return email; } /** * Checks if the given email address is valid. * * @param email the email address to be validated * @return {@code true} if the email address is valid, {@code false} otherwise */ private static boolean isValidEmail(String email) { try { InternetAddress emailAddress = new InternetAddress(email); emailAddress.validate(); return true; } catch (AddressException e) { return false; } } /** * Creates a one-time schedule to send an initial event in 1 minute with a flexible time window. * * @return {@code true} if the schedule was created successfully, {@code false} otherwise */ public static Boolean createOneTimeSchedule() { oneTimeScheduleName = promptUserForResourceName("Enter a name for the one-time schedule:"); logger.info("Creating a one-time schedule named {} to send an initial event in 1 minute with a flexible time window...", oneTimeScheduleName); LocalDateTime scheduledTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); String scheduleExpression = "at(" + scheduledTime.format(formatter) + ")"; return eventbridgeActions.createScheduleAsync( oneTimeScheduleName, scheduleExpression, scheduleGroupName, snsTopicArn, roleArn, "One time scheduled event test from schedule", true, true).join(); } /** * Creates a recurring schedule to send events based on a specific time. * * @return A {@link CompletableFuture} that completes with a boolean value indicating the success or failure of the operation. */ public static Boolean createRecurringSchedule() { logger.info("Creating a recurring schedule to send events for one hour..."); recurringScheduleName = promptUserForResourceName("Enter a name for the recurring schedule:"); // Prompt the user for the schedule rate (in minutes). int scheduleRateInMinutes = promptUserForInteger("Enter the desired schedule rate (in minutes): "); String scheduleExpression = "rate(" + scheduleRateInMinutes + " minutes)"; return eventbridgeActions.createScheduleAsync( recurringScheduleName, scheduleExpression, scheduleGroupName, snsTopicArn, roleArn, "Recurrent event test from schedule " + recurringScheduleName, true, true).join(); } /** * Prompts the user for a resource name and validates the input. * * @param prompt the message to display to the user when prompting for the resource name * @return the valid resource name entered by the user */ private static String promptUserForResourceName(String prompt) { logger.info(prompt); String resourceName = scanner.nextLine(); String regex = "[0-9a-zA-Z-_.]+"; if (!resourceName.matches(regex)) { logger.info("Invalid resource name. Please use a name that matches the pattern " + regex + "."); return promptUserForResourceName(prompt); } return resourceName; } /** * Prompts the user for an integer input and returns the integer value. * * @param prompt the message to be displayed to the user when prompting for input * @return the integer value entered by the user */ private static int promptUserForInteger(String prompt) { logger.info(prompt); String stringResponse = scanner.nextLine(); if (stringResponse == null || stringResponse.trim().isEmpty() || !isInteger(stringResponse)) { logger.info("Invalid integer."); return promptUserForInteger(prompt); } return Integer.parseInt(stringResponse); } /** * Checks if the given string represents a valid integer. * * @param str the string to be checked * @return {@code true} if the string represents a valid integer, {@code false} otherwise */ private static boolean isInteger(String str) { try { Integer.parseInt(str); return true; } catch (NumberFormatException e) { return false; } } }
서비스 작업용 래퍼입니다.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.retry.RetryMode; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.scheduler.SchedulerAsyncClient; import software.amazon.awssdk.services.scheduler.model.ActionAfterCompletion; import software.amazon.awssdk.services.scheduler.model.ConflictException; import software.amazon.awssdk.services.scheduler.model.CreateScheduleGroupRequest; import software.amazon.awssdk.services.scheduler.model.CreateScheduleGroupResponse; import software.amazon.awssdk.services.scheduler.model.CreateScheduleRequest; import software.amazon.awssdk.services.scheduler.model.DeleteScheduleGroupRequest; import software.amazon.awssdk.services.scheduler.model.DeleteScheduleRequest; import software.amazon.awssdk.services.scheduler.model.DeleteScheduleResponse; import software.amazon.awssdk.services.scheduler.model.FlexibleTimeWindow; import software.amazon.awssdk.services.scheduler.model.FlexibleTimeWindowMode; import software.amazon.awssdk.services.scheduler.model.ResourceNotFoundException; import software.amazon.awssdk.services.scheduler.model.Target; import java.time.Instant; import java.util.concurrent.CompletableFuture; import java.time.Duration; import java.util.concurrent.CompletionException; public class EventbridgeSchedulerActions { private static SchedulerAsyncClient schedulerClient; private static final Logger logger = LoggerFactory.getLogger(EventbridgeSchedulerActions.class); public static SchedulerAsyncClient getAsyncClient() { if (schedulerClient == null) { /* The `NettyNioAsyncHttpClient` class is part of the AWS SDK for Java, version 2, and it is designed to provide a high-performance, asynchronous HTTP client for interacting with AWS services. It uses the Netty framework to handle the underlying network communication and the Java NIO API to provide a non-blocking, event-driven approach to HTTP requests and responses. */ SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(50) // Adjust as needed. .connectionTimeout(Duration.ofSeconds(60)) // Set the connection timeout. .readTimeout(Duration.ofSeconds(60)) // Set the read timeout. .writeTimeout(Duration.ofSeconds(60)) // Set the write timeout. .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) // Set the overall API call timeout. .apiCallAttemptTimeout(Duration.ofSeconds(90)) // Set the individual call attempt timeout. .retryStrategy(RetryMode.STANDARD) .build(); schedulerClient = SchedulerAsyncClient.builder() .region(Region.US_EAST_1) .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return schedulerClient; } /** * Creates a new schedule group. * * @param name the name of the schedule group to be created * @return a {@link CompletableFuture} representing the asynchronous operation of creating the schedule group */ public CompletableFuture<CreateScheduleGroupResponse> createScheduleGroup(String name) { CreateScheduleGroupRequest request = CreateScheduleGroupRequest.builder() .name(name) .build(); logger.info("Initiating createScheduleGroup call for group: {}", name); CompletableFuture<CreateScheduleGroupResponse> futureResponse = getAsyncClient().createScheduleGroup(request); futureResponse.whenComplete((response, ex) -> { if (ex != null) { if (ex instanceof CompletionException && ex.getCause() instanceof ConflictException) { // Rethrow the ConflictException throw (ConflictException) ex.getCause(); } else { throw new CompletionException("Failed to create schedule group: " + name, ex); } } else if (response == null) { throw new RuntimeException("Failed to create schedule group: response was null"); } else { logger.info("Successfully created schedule group '{}': {}", name, response.scheduleGroupArn()); } }); return futureResponse; } /** * Creates a new schedule for a target task. * * @param name the name of the schedule * @param scheduleExpression The schedule expression that defines when the schedule should run. * @param scheduleGroupName the name of the schedule group to which the schedule belongs * @param targetArn the Amazon Resource Name (ARN) of the target task * @param roleArn the ARN of the IAM role to be used for the schedule * @param input the input data for the target task * @param deleteAfterCompletion whether to delete the schedule after it's executed * @param useFlexibleTimeWindow whether to use a flexible time window for the schedule execution * @return true if the schedule was successfully created, false otherwise */ public CompletableFuture<Boolean> createScheduleAsync( String name, String scheduleExpression, String scheduleGroupName, String targetArn, String roleArn, String input, boolean deleteAfterCompletion, boolean useFlexibleTimeWindow) { int hoursToRun = 1; int flexibleTimeWindowMinutes = 10; Target target = Target.builder() .arn(targetArn) .roleArn(roleArn) .input(input) .build(); FlexibleTimeWindow flexibleTimeWindow = FlexibleTimeWindow.builder() .mode(useFlexibleTimeWindow ? FlexibleTimeWindowMode.FLEXIBLE : FlexibleTimeWindowMode.OFF) .maximumWindowInMinutes(useFlexibleTimeWindow ? flexibleTimeWindowMinutes : null) .build(); Instant startDate = Instant.now(); Instant endDate = startDate.plus(Duration.ofHours(hoursToRun)); CreateScheduleRequest request = CreateScheduleRequest.builder() .name(name) .scheduleExpression(scheduleExpression) .groupName(scheduleGroupName) .target(target) .actionAfterCompletion(deleteAfterCompletion ? ActionAfterCompletion.DELETE : ActionAfterCompletion.NONE) .startDate(startDate) .endDate(endDate) .flexibleTimeWindow(flexibleTimeWindow) .build(); return getAsyncClient().createSchedule(request) .thenApply(response -> { logger.info("Successfully created schedule {} in schedule group {}, The ARN is {} ", name, scheduleGroupName, response.scheduleArn()); return true; }) .whenComplete((result, ex) -> { if (ex != null) { if (ex instanceof ConflictException) { // Handle ConflictException logger.error("A conflict exception occurred while creating the schedule: {}", ex.getMessage()); throw new CompletionException("A conflict exception occurred while creating the schedule: " + ex.getMessage(), ex); } else { throw new CompletionException("Error creating schedule: " + ex.getMessage(), ex); } } }); } /** * Deletes the specified schedule group. * * @param name the name of the schedule group to delete * @return a {@link CompletableFuture} that completes when the schedule group has been deleted * @throws CompletionException if an error occurs while deleting the schedule group */ public CompletableFuture<Void> deleteScheduleGroupAsync(String name) { DeleteScheduleGroupRequest request = DeleteScheduleGroupRequest.builder() .name(name) .build(); return getAsyncClient().deleteScheduleGroup(request) .thenRun(() -> { logger.info("Successfully deleted schedule group {}", name); }) .whenComplete((result, ex) -> { if (ex != null) { if (ex instanceof ResourceNotFoundException) { throw new CompletionException("The resource was not found: " + ex.getMessage(), ex); } else { throw new CompletionException("Error deleting schedule group: " + ex.getMessage(), ex); } } }); } /** * Deletes a schedule with the specified name and group name. * * @param name the name of the schedule to be deleted * @param groupName the group name of the schedule to be deleted * @return a {@link CompletableFuture} that, when completed, indicates whether the schedule was successfully deleted * @throws CompletionException if an error occurs while deleting the schedule, except for the case where the schedule is not found */ public CompletableFuture<Boolean> deleteScheduleAsync(String name, String groupName) { DeleteScheduleRequest request = DeleteScheduleRequest.builder() .name(name) .groupName(groupName) .build(); CompletableFuture<DeleteScheduleResponse> response = getAsyncClient().deleteSchedule(request); return response.handle((result, ex) -> { if (ex != null) { if (ex instanceof ResourceNotFoundException) { throw new CompletionException("Resource not found while deleting schedule with ID: " + name, ex); } else { throw new CompletionException("Failed to delete schedule.", ex); } } logger.info("Successfully deleted schedule with name {}.", name); return true; }); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 다음 주제를 참조하십시오.
-