Java網絡編程教程之設置請求超時的方法
一、引言
隨著企業(yè)系統(tǒng)的發(fā)展,應用多采用分布式結構,嚴重依賴于網絡的穩(wěn)定性。但由于網絡天生的不穩(wěn)定性,系統(tǒng)開發(fā)過程中需要考慮網絡不穩(wěn)定情況下如何保證應用的魯棒性。 設置網絡超時是其中一種保證應用健壯性的手段。 設置網絡超時設置后,請求在設定時間能未完成將被強制終止,保證程序不出現無限制的線程阻塞情況,有效的提高了應用的可用性。
下面話不多說了,來一起看看詳細的介紹吧。
二、未設置超時與設置超時情況對比
1. 網絡請求圖例:

網絡請求超時案例
2. 設置超時時間后,請求圖例:

網絡請求超時案例-設置超時
三、常見的網絡超時設置
1. httpclient超時設置(Spring bean)
配置
<bean id="multiThreadedHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property name="params">
<bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<property name="maxTotalConnections" value="${maxTotalConnections:300}" />
<property name="defaultMaxConnectionsPerHost" value="${defaultMaxConnectionsPerHost:300}" />
<!-- 連接超時,毫秒。 -->
<property name="connectionTimeout" value="${connectTimeout:10000}" />
<!-- socket超時,毫秒。 -->
<property name="soTimeout" value="${readTimeout:600000}" />
<property name="staleCheckingEnabled" value="${staleCheckingEnabled:true}" />
</bean>
</property>
</bean>
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg>
<ref bean="multiThreadedHttpConnectionManager" />
</constructor-arg>
</bean>
httpinvoker使用場景
配置HttpInvokerRequestExecutor,覆蓋HttpInvokerProxyFactoryBean中默認使用的的SimpleHttpInvokerRequestExecutor,并配置網絡超時。見《配置》。
<bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<constructor-arg>
<ref bean="httpClient" />
</constructor-arg>
</bean>
<bean id="xxxxService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="${xxxxServiceUrl}" />
<property name="serviceInterface" value="com.xxxxService" />
<property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" />
</bean>
2. HttpClient超時設置(硬編碼)
樣例
RequestConfig config = RequestConfig.custom() .setSocketTimeout(1*1000) // socket套接字超時,毫秒。 .setConnectionRequestTimeout(1*1000) //使用連接池來管理連接時,從連接池獲取連接的超時時間,毫秒。 .setConnectTimeout(5*1000) // 連接建立超時,毫秒。 .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(config) // .build(); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); // 執(zhí)行請求
3. 郵件超時設置
基于Spring框架開發(fā)的項目可以很方便的使用
org.springframework.mail.javamail.JavaMailSenderImpl實現郵件提醒等功能。
配置
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
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說明
- mail.smtp.timeout : smtp郵件服務器讀取超時。
- mail.smtp.connectiontimeout : smtp郵件服務器連接超時。
- mail.smtp.auth : 是否認證用戶。
注: property參數名列表可查詢JavaMail API documentation。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
參考
相關文章
SpringBoot利用@Retryable注解實現接口重試
本文主要介紹了springboot如何利用@Retryable注解實現接口重試功能,文中示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
jvm細節(jié)探索之synchronized及實現問題分析
這篇文章主要介紹了jvm細節(jié)探索之synchronized及實現問題分析,涉及synchronized的字節(jié)碼表示,JVM中鎖的優(yōu)化,對象頭的介紹等相關內容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11
SpringBoot實現在webapp下直接訪問html,jsp
這篇文章主要介紹了SpringBoot實現在webapp下直接訪問html,jsp問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Spring DATA JPA 中findAll 進行OrderBy方式
這篇文章主要介紹了Spring DATA JPA 中findAll 進行OrderBy方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

