In software development, configuring a timeout prevents a program from being blocked for an extended period while waiting for a response. Failure to configure a timeout can lead to wasted resources or even a system crash. A timeout that is too short can interrupt a task before it is complete, which affects normal functionality. Setting an appropriate timeout improves system stability and user experience. This topic describes how to configure timeouts for the V2.0 Java SDK.
Timeout configuration
Timeout settings are applied in the following order of priority, from highest to lowest: settings in RuntimeOptions, settings in Config, and default configurations.
You can use the default configuration. The default connection timeout is 5,000 milliseconds, and the read timeout is 10,000 milliseconds.
You can configure the timeout for the current request using the com.aliyun.teautil.models.RuntimeOptions class.
com.aliyun.teautil.models.RuntimeOptions runtimeOptions = new com.aliyun.teautil.models.RuntimeOptions(); runtimeOptions.connectTimeout = 5000; runtimeOptions.readTimeout = 10000;You can configure a global timeout using the com.aliyun.teaopenapi.models.Config class.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config(); // The default connection timeout is 5 × 1000 milliseconds. config.setConnectTimeout(5000); // The default read timeout is 10 × 1000 milliseconds. config.setReadTimeout(10000);