HTTP client implementation using Java's URLConnection for the AWS SDK for Java 2.0
This document covers how the UrlConnectionHttpClient integrates with the AWS SDK through the Java ServiceLoader mechanism and how to use it with AWS services.
The URL Connection HTTP Client is automatically discoverable by the AWS SDK through Java's ServiceLoader mechanism.
public class UrlConnectionSdkHttpService implements SdkHttpService {
public SdkHttpClient.Builder createHttpClientBuilder();
}The AWS SDK automatically discovers available HTTP client implementations at runtime:
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.regions.Region;
// The AWS SDK will automatically find and can use UrlConnectionHttpClient
S3Client s3 = S3Client.builder()
.region(Region.US_EAST_1)
.build();
// If no other HTTP client is on the classpath, UrlConnectionHttpClient will be usedThe service is registered in META-INF/services/software.amazon.awssdk.http.SdkHttpService:
software.amazon.awssdk.http.urlconnection.UrlConnectionSdkHttpServiceimport software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.regions.Region;
import java.time.Duration;
// Configure S3 client with UrlConnectionHttpClient
S3Client s3Client = S3Client.builder()
.httpClient(UrlConnectionHttpClient.builder()
.connectionTimeout(Duration.ofSeconds(10))
.socketTimeout(Duration.ofSeconds(30))
.build())
.region(Region.US_WEST_2)
.build();
// Configure DynamoDB client
DynamoDbClient dynamoClient = DynamoDbClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.region(Region.US_EAST_1)
.build();import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.urlconnection.ProxyConfiguration;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.ec2.EC2Client;
import software.amazon.awssdk.regions.Region;
import java.time.Duration;
// Configure HTTP client to be shared across multiple AWS service clients
SdkHttpClient sharedHttpClient = UrlConnectionHttpClient.builder()
.connectionTimeout(Duration.ofSeconds(15))
.socketTimeout(Duration.ofSeconds(60))
.proxyConfiguration(ProxyConfiguration.builder()
.useSystemPropertyValues(true)
.build())
.build();
// Use the shared client across multiple services
S3Client s3 = S3Client.builder()
.httpClient(sharedHttpClient)
.region(Region.US_WEST_2)
.build();
EC2Client ec2 = EC2Client.builder()
.httpClient(sharedHttpClient)
.region(Region.US_WEST_2)
.build();
// Remember to close the shared client when done
// sharedHttpClient.close(); // Note: This is actually a no-op for UrlConnectionHttpClient// Optimized for high-throughput operations
SdkHttpClient highThroughputClient = UrlConnectionHttpClient.builder()
.socketTimeout(Duration.ofMinutes(5)) // Longer timeout for large uploads
.connectionTimeout(Duration.ofSeconds(10))
.build();
S3Client s3 = S3Client.builder()
.httpClient(highThroughputClient)
.region(Region.US_EAST_1)
.build();// Optimized for low-latency operations
SdkHttpClient lowLatencyClient = UrlConnectionHttpClient.builder()
.connectionTimeout(Duration.ofSeconds(3))
.socketTimeout(Duration.ofSeconds(10))
.build();
DynamoDbClient dynamodb = DynamoDbClient.builder()
.httpClient(lowLatencyClient)
.region(Region.US_EAST_1)
.build();// Enterprise configuration with proxy and custom TLS
SdkHttpClient enterpriseClient = UrlConnectionHttpClient.builder()
.connectionTimeout(Duration.ofSeconds(30))
.socketTimeout(Duration.ofMinutes(2))
.proxyConfiguration(ProxyConfiguration.builder()
.endpoint(URI.create("http://corporate-proxy.company.com:8080"))
.username("domain\\serviceaccount")
.password("password")
.nonProxyHosts(Set.of("*.internal.company.com", "localhost"))
.build())
.tlsTrustManagersProvider(TlsTrustManagersProvider.fileStore(
Paths.get("/etc/ssl/corporate-ca.jks"), "password"))
.build();
// Use with multiple AWS services
S3Client s3 = S3Client.builder()
.httpClient(enterpriseClient)
.region(Region.US_EAST_1)
.build();
SecretsManagerClient secrets = SecretsManagerClient.builder()
.httpClient(enterpriseClient)
.region(Region.US_EAST_1)
.build();Best for:
Example Use Case:
// Simple Lambda function with minimal cold start time
public class SimpleS3Handler implements RequestHandler<S3Event, String> {
private static final S3Client s3Client = S3Client.builder()
.httpClient(UrlConnectionHttpClient.create()) // Minimal overhead
.build();
@Override
public String handleRequest(S3Event event, Context context) {
// Handle S3 events with minimal dependencies
return "Success";
}
}// Apache HTTP Client (more features, better performance for high volume)
SdkHttpClient apacheClient = ApacheHttpClient.builder()
.maxConnections(100)
.build();
// Netty HTTP Client (async support, better for reactive applications)
SdkHttpClient nettyClient = NettyNioAsyncHttpClient.builder()
.maxConcurrency(50)
.build();// Main service interface
public interface SdkHttpService {
SdkHttpClient.Builder createHttpClientBuilder();
}
// URL Connection implementation
public class UrlConnectionSdkHttpService implements SdkHttpService {
@Override
public SdkHttpClient.Builder createHttpClientBuilder() {
return UrlConnectionHttpClient.builder();
}
}import org.junit.jupiter.api.Test;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
@Test
void testS3Integration() {
// Use UrlConnectionHttpClient in tests for simplicity
S3Client s3Client = S3Client.builder()
.httpClient(UrlConnectionHttpClient.builder()
.connectionTimeout(Duration.ofSeconds(5))
.socketTimeout(Duration.ofSeconds(10))
.build())
.region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create("test", "test")))
.endpointOverride(URI.create("http://localhost:9000")) // LocalStack or mock
.build();
// Test S3 operations
ListBucketsResponse response = s3Client.listBuckets();
assertThat(response.buckets()).isNotNull();
s3Client.close();
}// Service provider interface
public interface SdkHttpService {
SdkHttpClient.Builder createHttpClientBuilder();
}
// URL Connection service implementation
public class UrlConnectionSdkHttpService implements SdkHttpService {
public SdkHttpClient.Builder createHttpClientBuilder();
}
// Core HTTP client interface (from AWS SDK)
public interface SdkHttpClient extends Closeable {
ExecutableHttpRequest prepareRequest(HttpExecuteRequest request);
String clientName();
void close();
interface Builder<B extends Builder<B>> {
SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults);
}
}Install with Tessl CLI
npx tessl i tessl/maven-software-amazon-awssdk--url-connection-client