There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.
Code examples for AWS Batch using AWS SDKs
The following code examples show you how to use AWS Batch with an AWS software development kit (SDK).
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.
Get started
The following code example shows how to get started using 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;
}
}