AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
你好 AWS Batch
以下代码示例展示了如何开始使用 AWS Batch。
- Java
-
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 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; } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 listJobsPaginator” 中的。
-
基础知识
操作