服务器之家:专注于服务器技术及软件下载分享
分类导航

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

 

延伸 · 阅读

精彩推荐