文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您好 AWS Batch
下列程式碼範例示範如何開始使用 AWS Batch。
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 listJobsPaginator AWS SDK for Java 2.x 參考中的 API。
-
基本概念
動作