This topic describes how to configure timeout periods in Alibaba Cloud SDK V2.0.
Alibaba Cloud SDK V2.0 allows you to separately specify business parameters and runtime parameters. The timeout periods configured in SDK V2.0 are effective in the following descending order: the timeout periods that you configure by using RuntimeOptions, the timeout periods that you configure for an SDK client, and the default timeout periods. The default timeout period for connection requests is 5 seconds, and the default timeout period for read requests is 10 seconds.
import com.aliyun.ecs20140526.models.DescribeRegionsRequest;
import com.aliyun.teautil.models.RuntimeOptions;
public class Sample {
public static void main(String[] args) throws Exception {
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// Use the SDK Credentials package to configure a credential.
config.setCredential(credentialClient);
config.setRegionId("<regionId>");
// Configure timeout periods when you initialize the SDK client.
// The default timeout period for connection requests is 5,000 milliseconds. The value is calculated by using the formula: 5 × 1,000 = 5,000.
config.setConnectTimeout(5000);
// The default timeout period for read requests is 10,000 milliseconds. The value is calculated by using the formula: 10 × 1,000 = 10,000.
config.setReadTimeout(10000);
com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
// Configure timeout periods by using runtime parameters. The timeout periods take effect only on the requests that use the RuntimeOptions instance.
RuntimeOptions runtimeOptions = new RuntimeOptions();
runtimeOptions.connectTimeout = 5000;
runtimeOptions.readTimeout = 10000;
DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions);
}
}