如何快速解决Java网络编程设置请求超时

https://blog.csdn.net/longdan3105/article/details/78929967?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242

一 、引言

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

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

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

三、常见的网络超时设置

3.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>

3.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.3、webservice超时设置

https://blog.csdn.net/samyang1/article/details/79309223
在 Spring+CXF(axis) 的 WebService 环境下,客户端有两个时间属性是可配置的,分别是 ConnectionTimeout 和ReceiveTimeout。      
ConnectionTimeout 连接超时 — WebService 以 TCP 连接为基础,这个属性可以理解为 tcp 的握手时的时间设置,超过设置的时间长则认为是连接超时,以毫秒为单位,默认是 30,000 毫秒,即 30 秒。        
ReceiveTimeout 读取超时 — 这个属性是发送 WebService 的请求后等待响应的时间,超过设置的时长就认为是响应超时,以毫秒为单位,默认是 60,000 毫秒,即60秒。

<!-- 单个服务 -->
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd  
           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
           http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">  
    <http-conf:conduit name="{http://impl.service.izhangheng.com/}WebService.http-conduit">   
        <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="30000"/>  
    </http-conf:conduit>
</beans> 

<!-- 所有服务 -->   
<http-conf:conduit name="*.http-conduit">  
    <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="30000"/>   
</http-conf:conduit>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值