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

網(wǎng)絡(luò)請(qǐng)求超時(shí)案例
2. 設(shè)置超時(shí)時(shí)間后,請(qǐng)求圖例:

網(wǎng)絡(luò)請(qǐng)求超時(shí)案例-設(shè)置超時(shí)
三、常見的網(wǎng)絡(luò)超時(shí)設(shè)置
1. httpclient超時(shí)設(shè)置(Spring bean)
配置
java;"> <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}" /> <!-- 連接超時(shí),毫秒。 --> <property name="connectionTimeout" value="${connectTimeout:10000}" /> <!-- socket超時(shí),毫秒。 --> <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使用場(chǎng)景
配置HttpInvokerRequestExecutor,覆蓋HttpInvokerProxyFactoryBean中默認(rèn)使用的的SimpleHttpInvokerRequestExecutor,并配置網(wǎng)絡(luò)超時(shí)。見《配置》。
<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超時(shí)設(shè)置(硬編碼)
樣例
RequestConfig config = RequestConfig.custom() .setSocketTimeout(1*1000) // socket套接字超時(shí),毫秒。 .setConnectionRequestTimeout(1*1000) //使用連接池來管理連接時(shí),從連接池獲取連接的超時(shí)時(shí)間,毫秒。 .setConnectTimeout(5*1000) // 連接建立超時(shí),毫秒。 .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(config) // .build(); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); // 執(zhí)行請(qǐng)求
3. 郵件超時(shí)設(shè)置
基于Spring框架開發(fā)的項(xiàng)目可以很方便的使用
org.springframework.mail.javamail.JavaMailSenderImpl實(shí)現(xiàn)郵件提醒等功能。
配置
<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說明
注: property參數(shù)名列表可查詢JavaMail API documentation。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VeVb武林網(wǎng)的支持。
參考
新聞熱點(diǎn)
疑難解答
圖片精選