国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Java網(wǎng)絡(luò)編程教程之設(shè)置請(qǐng)求超時(shí)的方法

2024-07-13 10:14:50
字體:
供稿:網(wǎng)友

一、引言

隨著企業(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)求圖例:

java請(qǐng)求超時(shí)時(shí)間設(shè)置,java,設(shè)置請(qǐng)求超時(shí),請(qǐng)求超時(shí)處理

網(wǎng)絡(luò)請(qǐng)求超時(shí)案例

2. 設(shè)置超時(shí)時(shí)間后,請(qǐng)求圖例:

java請(qǐng)求超時(shí)時(shí)間設(shè)置,java,設(shè)置請(qǐng)求超時(shí),請(qǐng)求超時(shí)處理

網(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說明

  • mail.smtp.timeout : smtp郵件服務(wù)器讀取超時(shí)。
  • mail.smtp.connectiontimeout : smtp郵件服務(wù)器連接超時(shí)。
  • mail.smtp.auth : 是否認(rèn)證用戶。

注: property參數(shù)名列表可查詢JavaMail API documentation。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VeVb武林網(wǎng)的支持。

參考

  • JavaMail API documentation
  • JavaMail Reference Implementation

注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巢湖市| 客服| 新田县| 建水县| 蒲城县| 高州市| 阿城市| 崇明县| 榕江县| 冀州市| 嫩江县| 铜梁县| 宁陕县| 黔江区| 绥中县| 白沙| 景泰县| 巨鹿县| 九台市| 合作市| 汕尾市| 运城市| 香格里拉县| 南城县| 深州市| 泌阳县| 花莲市| 绍兴县| 莱西市| 武隆县| 阿鲁科尔沁旗| 郁南县| 长春市| 紫云| 得荣县| 土默特左旗| 嘉善县| 永川市| 张掖市| 永善县| 土默特右旗|