如何快速解决Java网络编程设置请求超时,还不快加入收藏!

一 引言

随着企业系统的发展,应用多采用分布式结构,严重依赖于网络的稳定性。但由于网络天生的不稳定性,系统开发过程中需要考虑网络不稳定情况下如何保证应用的鲁棒性。 设置网络超时是其中一种保证应用健壮性的手段。 设置网络超时设置后,请求在设定时间能未完成将被强制终止,保证程序不出现无限制的线程阻塞情况,有效的提高了应用的可用性。

二、未设置超时与设置超时情况对比

  1. 网络请求图例:

如何快速解决Java网络编程设置请求超时,还不快加入收藏!

Java

网络请求超时案例

 2. 设置超时时间后,请求图例:

网络请求超时案例-设置超时

 

如何快速解决Java网络编程设置请求超时,还不快加入收藏!

Java

三、常见的网络超时设置

  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>

如何快速解决Java网络编程设置请求超时,还不快加入收藏!

Java

 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); // 执行请求

 

如何快速解决Java网络编程设置请求超时,还不快加入收藏!

Java

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。

Java相关资料+QQ:1018925780

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值