1. Introduction
With the development of enterprise systems, applications mostly adopt distributed structures, which rely heavily on the stability of the network. However, due to the inherent instability of the network, it is necessary to consider how to ensure the robustness of the application in the case of network instability during system development. Setting network timeout is one of the means to ensure application robustness. After setting the network timeout setting, the request will be forced to terminate if it fails to complete the set time, ensuring that the program does not have unlimited thread blockage, effectively improving the availability of the application.
I won’t say much below, let’s take a look at the detailed introduction together.
2. Comparison between the timeout not set and the timeout setting
1. Network request legend:
Network request timeout case
2. After setting the timeout time, request the legend:
Network request timeout case - Set timeout
3. Common network timeout settings
1. httpclient timeout setting (Spring bean)
Configuration
<bean id="multiThreadedHttpConnectionManager"> <property name="params"> <bean > <property name="maxTotalConnections" value="${maxTotalConnections:300}" /> <property name="defaultMaxConnectionsPerHost" value="${defaultMaxConnectionsPerHost:300}" /> <!-- Connection timed out, milliseconds. --> <property name="connectionTimeout" value="${connectTimeout:10000}" /> <!-- socket timeout, milliseconds. --> <property name="soTimeout" value="${readTimeout:600000}" /> <property name="staleCheckingEnabled" value="${staleCheckingEnabled:true}" /> </bean> </property> </bean> <bean id="httpClient"> <constructor-arg> <ref bean="multiThreadedHttpConnectionManager" /> </constructor-arg> </bean>httpinvoker usage scenario
Configure HttpInvokerRequestExecutor, override the SimpleHttpInvokerRequestExecutor used by default in HttpInvokerProxyFactoryBean, and configure network timeout. See "Configuration".
<bean id="httpInvokerRequestExecutor" > <constructor-arg> <ref bean="httpClient" /> </constructor-arg> </bean> <bean id="xxxxService" > <property name="serviceUrl" value="${xxxxServiceUrl}" /> <property name="serviceInterface" value="com.xxxxService" /> <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" /> </bean>2. HttpClient timeout setting (hard coded)
Sample
RequestConfig config = RequestConfig.custom() .setSocketTimeout(1*1000) // socket socket timeout, milliseconds. .setConnectionRequestTimeout(1*1000) // When using the connection pool to manage the connection, get the connection timeout time, milliseconds from the connection pool. .setConnectTimeout(5*1000) // Connection establishment timeout, milliseconds. .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(config) // .build(); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); // Execute request
3. Mail timeout settings
Projects developed based on Spring framework can be used very easily
org.springframework.mail.javamail.JavaMailSenderImpl implements email reminders and other functions.
Configuration
<bean id="mailSender" p:host="${mailSender.host}" p:username="${mailSender.username}" p:password="${mailSender.password}"> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mailSender.smtp.auth:true} </prop> <prop key="mail.smtp.timeout">${mailSender.smtp.timeout:10000} </prop> <prop key="mail.smtp.connectiontimeout">${mailSender.smtp.connectiontimeout:10000} </prop> </props> </property> </bean>javaMailProperties Description
Note: The property parameter name list can query the JavaMail API documentation.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.
refer to