Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
AWS SDKs AWS Batch 사용에 대한 코드 예제
다음 코드 예제에서는 AWS 소프트웨어 개발 키트(SDK)와 AWS Batch 함께를 사용하는 방법을 보여줍니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 관련 시나리오의 컨텍스트에 따라 표시되며, 개별 서비스 함수를 직접적으로 호출하는 방법을 보여줍니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
시작
다음 코드 예제에서는 AWS Batch를 사용하여 시작하는 방법을 보여 줍니다.
- Java
-
- SDK for Java 2.x
-
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.retry.RetryPolicy;
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.batch.BatchAsyncClient;
import software.amazon.awssdk.services.batch.model.JobStatus;
import software.amazon.awssdk.services.batch.model.JobSummary;
import software.amazon.awssdk.services.batch.model.ListJobsRequest;
import software.amazon.awssdk.services.batch.paginators.ListJobsPublisher;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class HelloBatch {
private static BatchAsyncClient batchClient;
public static void main(String[] args) {
List<JobSummary> jobs = listJobs("my-job-queue");
jobs.forEach(job ->
System.out.printf("Job ID: %s, Job Name: %s, Job Status: %s%n",
job.jobId(), job.jobName(), job.status())
);
}
public static List<JobSummary> listJobs(String jobQueue) {
if (jobQueue == null || jobQueue.isEmpty()) {
throw new IllegalArgumentException("Job queue cannot be null or empty");
}
ListJobsRequest listJobsRequest = ListJobsRequest.builder()
.jobQueue(jobQueue)
.jobStatus(JobStatus.SUCCEEDED)
.build();
List<JobSummary> jobSummaries = new ArrayList<>();
ListJobsPublisher listJobsPaginator = getAsyncClient().listJobsPaginator(listJobsRequest);
CompletableFuture<Void> future = listJobsPaginator.subscribe(response -> {
jobSummaries.addAll(response.jobSummaryList());
});
future.join();
return jobSummaries;
}
private static BatchAsyncClient getAsyncClient() {
SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder()
.maxConcurrency(100) // Increase max concurrency to handle more simultaneous connections.
.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.
.retryPolicy(RetryPolicy.builder() // Add a retry policy to handle transient errors.
.numRetries(3) // Number of retry attempts.
.build())
.build();
if (batchClient == null) {
batchClient = BatchAsyncClient.builder()
.region(Region.US_EAST_1)
.httpClient(httpClient)
.overrideConfiguration(overrideConfig)
.build();
}
return batchClient;
}
}