All Products
Search
Document Center

Alibaba Cloud SDK:Configure an HTTP connection pool

最終更新日:Aug 12, 2024

In scenarios such as web services that support high-concurrency requests, frequent operation calls, and communication within a distributed system, you can use an HTTP connection pool to reduce latency, optimize resource management, accelerate responses, and improve system stability. This topic describes how to configure an HTTP connection pool in Alibaba Cloud SDK for Java V2.0.

Alibaba Cloud SDK for Java V2.0 allows you to configure an HTTP connection pool in the Config object when you initialize a client. You can specify only the maximum number of idle connections for the connection pool to optimize performance and resource management. The timeout period of idle connections in the connection pool is fixed to 10 seconds, which is calculated based on best practices.

import com.aliyun.ecs20140526.Client;
import com.aliyun.teaopenapi.models.Config;

public class Sample {
    public static void main(String[] args) throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.setRegionId("<regionId>");
        // Specify the maximum number of idle connections.
        config.setMaxIdleConns(10);
        Client client = new Client(config);
    }
}