Java网络编程设置请求超时

发表于:2017-12-18 10:30

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:iamgeektao    来源:简书

#
java
分享:
  一、引言
  随着企业系统的发展,应用多采用分布式结构,严重依赖于网络的稳定性。但由于网络天生的不稳定性,系统开发过程中需要考虑网络不稳定情况下如何保证应用的鲁棒性。 设置网络超时是其中一种保证应用健壮性的手段。 设置网络超时设置后,请求在设定时间能未完成将被强制终止,保证程序不出现无限制的线程阻塞情况,有效的提高了应用的可用性。
  二、未设置超时与设置超时情况对比
  1. 网络请求图例:
  
网络请求超时案例
  2. 设置超时时间后,请求图例:
  
网络请求超时案例-设置超时
  三、常见的网络超时设置
  1. httpclient超时设置(Spring bean)
  配置
<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,并配置网络超时。见《配置》。
<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超时设置(硬编码)
  样例
  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实现邮件提醒等功能。
  配置
<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。

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号