Timeout configuration

Updated at:
Copy as MD

Appropriate timeout periods prevent requests from hanging on unstable networks or unavailable services, improving system stability and response efficiency. This topic describes how to configure timeout periods for the Java (asynchronous) SDK.

Configuration methods

Note

Timeout configurations are prioritized in the following order, from highest to lowest: RequestConfiguration, ClientOverrideConfiguration, HttpClient, and default configurations.

Method 1: Use the default configurations

The default connection timeout is 10000 ms, and the default response timeout is 20000 ms.

Method 2: Configure using RequestConfiguration

darabonba.core.RequestConfiguration requestConfiguration = darabonba.core.RequestConfiguration.create()
        .setConnectTimeout(java.time.Duration.ofMillis(10000))
        .setResponseTimeout(java.time.Duration.ofMillis(15000));

After the configuration, pass the RequestConfiguration object to the OpenAPI request. The following example sets the connection timeout and response timeout for SendSms:

darabonba.core.RequestConfiguration requestConfiguration = darabonba.core.RequestConfiguration.create()
        .setConnectTimeout(java.time.Duration.ofMillis(10000))
        .setResponseTimeout(java.time.Duration.ofMillis(15000));

// SendSmsRequest is the request object for SendSms. The timeout configuration applies only to the current request.
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
        .requestConfiguration(requestConfiguration)
        .build();

Method 3: Configure using ClientOverrideConfiguration

darabonba.core.client.ClientOverrideConfiguration clientOverrideConfiguration = darabonba.core.client.ClientOverrideConfiguration.create()
        .setConnectTimeout(Duration.ofMillis(10000))
        .setResponseTimeout(Duration.ofMillis(15000));

After the configuration, pass the ClientOverrideConfiguration object to AsyncClient.

ClientOverrideConfiguration clientOverrideConfiguration = ClientOverrideConfiguration.create()
        .setConnectTimeout(Duration.ofMillis(10000))
        .setResponseTimeout(Duration.ofMillis(15000));

// The timeout configuration applies to all requests invoked through this AsyncClient.
AsyncClient client = AsyncClient.builder()
        .credentialsProvider(credentialProvider) // This example omits the credentialProvider implementation procedure...
        .overrideConfiguration(clientOverrideConfiguration)
        .build();

Method 4: Configure using HttpClient

// HttpClient Configuration
HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
        .connectionTimeout(Duration.ofMillis(10000))
        .responseTimeout(Duration.ofMillis(20000))
        .build();

After the configuration, pass the HttpClient object to AsyncClient.

// HttpClient Configuration
HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
        .connectionTimeout(Duration.ofMillis(10000))
        .responseTimeout(Duration.ofMillis(20000))
        .build();

// The timeout configuration applies to all requests invoked through this AsyncClient.
AsyncClient client = AsyncClient.builder()
        .httpClient(httpClient)
        .credentialsProvider(credentialProvider) // This example omits the credentialProvider implementation procedure...
        .build();