激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java網絡編程教程之設置請求超時的方法

Java網絡編程教程之設置請求超時的方法

2021-03-06 13:42iamgeektao Java教程

這篇文章主要給大家介紹了關于Java網絡編程教程之設置請求超時的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

 

一、引言

隨著企業系統的發展,應用多采用分布式結構,嚴重依賴于網絡的穩定性。但由于網絡天生的不穩定性,系統開發過程中需要考慮網絡不穩定情況下如何保證應用的魯棒性。 設置網絡超時是其中一種保證應用健壯性的手段。 設置網絡超時設置后,請求在設定時間能未完成將被強制終止,保證程序不出現無限制的線程阻塞情況,有效的提高了應用的可用性。

下面話不多說了,來一起看看詳細的介紹吧。

二、未設置超時與設置超時情況對比

1. 網絡請求圖例:

Java網絡編程教程之設置請求超時的方法

網絡請求超時案例

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

Java網絡編程教程之設置請求超時的方法

網絡請求超時案例-設置超時

三、常見的網絡超時設置

1. httpclient超時設置(spring bean)

配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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,并配置網絡超時。見《配置》。

?
1
2
3
4
5
6
7
8
9
10
<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超時設置(硬編碼)

樣例

?
1
2
3
4
5
6
7
8
9
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); // 執行請求

3. 郵件超時設置

基于spring框架開發的項目可以很方便的使用
org.springframework.mail.javamail.javamailsenderimpl實現郵件提醒等功能。

配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

參考

原文鏈接:http://www.jianshu.com/p/b9365dfa66f4

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产免费最爽的乱淫视频a 午夜精品久久久久久久99热浪潮 | 精国品产一区二区三区有限公司 | 狼伊千合综网中文 | 特一级黄色毛片 | 九色中文字幕 | 免费网站看毛片 | 国产精品久久久久久久四虎电影 | 羞羞视频免费网站 | 在线免费观看精品 | 中文字幕国产亚洲 | 高清成人在线 | 久草在线手机视频 | 一级大片视频 | 日本一级黄色大片 | 国产一级在线免费观看 | 欧美成人午夜影院 | 国产精品久久久久久影院8一贰佰 | 777zyz色资源站在线观看 | 91资源在线观看 | 成人免费乱码大片a毛片视频网站 | 精品久久久久久成人av | 超级av在线 | 激情夜色| 欧美日韩精品一区二区三区不卡 | 99久久久精品 | 成人福利电影在线观看 | 大西瓜永久免费av在线 | 青青久在线视频 | 成人激情综合网 | 激情小说激情图片激情电影 | 法国极品成人h版 | 国产69精品久久久久久久久久 | 91视频久久 | 国产一区二区三区撒尿在线 | 热re91久久精品国产99热 | 97久久精品一区二区三区观看 | 毛片大全免费 | 国产高潮好爽受不了了夜色 | 亚洲91精品 | 毛片在线免费视频 | 免费毛片电影 |