文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS Batch 使用 AWS SDKs程式碼範例
下列程式碼範例示範如何 AWS Batch 搭配 AWS 軟體開發套件 (SDK) 使用 。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
案例是向您展示如何呼叫服務中的多個函數或與其他 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;
}
}