国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - Java網(wǎng)絡(luò)編程教程之設(shè)置請求超時的方法

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

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

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

 

一、引言

隨著企業(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ò)超時是其中一種保證應(yīng)用健壯性的手段。 設(shè)置網(wǎng)絡(luò)超時設(shè)置后,請求在設(shè)定時間能未完成將被強(qiáng)制終止,保證程序不出現(xiàn)無限制的線程阻塞情況,有效的提高了應(yīng)用的可用性。

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

二、未設(shè)置超時與設(shè)置超時情況對比

1. 網(wǎng)絡(luò)請求圖例:

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

網(wǎng)絡(luò)請求超時案例

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

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

網(wǎng)絡(luò)請求超時案例-設(shè)置超時

三、常見的網(wǎng)絡(luò)超時設(shè)置

1. httpclient超時設(shè)置(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中默認(rèn)使用的的simplehttpinvokerrequestexecutor,并配置網(wǎng)絡(luò)超時。見《配置》。

?
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超時設(shè)置(硬編碼)

樣例

?
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); // 執(zhí)行請求

3. 郵件超時設(shè)置

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

配置

?
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郵件服務(wù)器讀取超時。
  • mail.smtp.connectiontimeout : smtp郵件服務(wù)器連接超時。
  • mail.smtp.auth : 是否認(rèn)證用戶。

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

總結(jié)

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

參考

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

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一区视频在线 | 精品成人在线视频 | 国产亚洲精品美女久久久久久久久久 | 福利社午夜影院 | 一区二区福利 | av在线电影网站 | 欧美日韩国产高清 | 欧美在线a | 色网站在线观看 | 免费一级片在线观看 | 精品无码久久久久久国产 | 在线免费观看毛片 | 国产成人av在线播放 | 国产黄 | 中文字幕视频在线 | 在线观看的av| av午夜电影 | 亚洲精品一区二区三区不 | 欧美三区| 欧美日韩视频一区二区 | 亚洲精品视频区 | 欧美日韩国产一级片 | 国产精品久久久久久久久久免费看 | 交视频在线观看国产 | 日韩在线播放一区二区三区 | 久久精彩视频 | 亚洲成人久久久 | 国产97色在线 | 亚洲 | 国产日韩精品在线 | 中文字幕精品一区 | 一区二区三区在线 | 欧美欧美欧美 | 久久久久久香蕉 | 四虎免费视频 | 亚洲精品福利 | 日韩一区二区不卡 | 亚洲午夜一区 | 午夜精品久久 | 97理论片 | 一级黄色大片 | 国产一区二区三区午夜 |