webService超时时间的JAVA中设置

本文介绍了在JAVA中调用SOAP和REST两种类型的WebService时如何设置超时时间。对于SOAP服务,通过Spring+CXF环境,可以配置ConnectionTimeout和ReceiveTimeout参数。而对于REST服务,可以通过HttpClient设置超时。内容详细解释了两个超时属性的作用,并提供了相关配置方法的参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        目前比较常用的 WebService 有两种方式:SOAP和REST,本文就以调用这两种方式的服务,介绍编程中常用的超时设置,包括 JAVA调用远程 SOAP 服务,以及利用 HttpClient(JAVA调用 REST 服务(简单的 HTTP 服务)的超时问题。

一、webservice概念

        Web 服务是一个软件接口,它描述了一组可以在网络上通过标准化的 XML 消息传递访问的操作。Web Service 最基本的组成部分为服务的提供者(Service Provider)和服务的请求者(Service Requester)。Web Service 两端的应用是通过基于标准的 XML 格式的协议进行通信的,这种最常用的协议就是 SOAP(Simple Object Access Protocol)。

具体可参考:http://www.ruanyifeng.com/blog/2009/08/what_is_web_service.html

                       https://www.ibm.com/developerworks/cn/webservices/ws-wsar/part2/

二、SOAP服务

1、CXF

        在使用 WebService 的时候,我们可能需要一个备份的 WebService 服务器。一旦主服务器 down 了,我们可以使用备份的服务器。那么这里就需要对客服端连接服务器的时间做一个修改。

在 Spring+CXF 的 WebService 环境下,客户端有两个时间属性是可配置的,分别是 ConnectionTimeout 和ReceiveTimeout。

        ConnectionTimeout — WebService 以 TCP 连接为基础,这个属性可以理解为 tcp 的握手时的时间设置,超过设置的时间长则认为是连接超时,以毫秒为单位,默认是 30,000 毫秒,即 30 秒。

        ReceiveTimeout — 这个属性是发送 WebService 的请求后等待响应的时间,超过设置的时长就认为是响应超时,以毫秒为单位,默认是 60,000 毫秒,即60秒。

在调用 CXF 服务的 xml 文件中加入下面的配置:

<!-- 单个服务 -->
<?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>
这里需要注意的有几个地方: 
1、需要指定 http-conf 名称空间:xmlns:http-conf=http://cxf.apache.org/transports/http/configuration
2、指定模式位置:http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd

3、http-conf:conduit中的 name 属性,指定设置生效的服务。

详细参考:http://blog.youkuaiyun.com/shb_derek1/article/details/8018287

三、Rest

         代表性状态传输(Representational State Transfer,REST)在 Web 领域已经得到了广泛的接受,是基于 SOAP 和 Web 服务描述语言(Web Services Description Language,WSDL)的 Web 服务的更为简单的替代方法。接口设计方面这一转变的关键证据是主流 Web 2.0 服务提供者(包括 Yahoo、Google 和 Facebook)对 REST 的采用,这些提供者弃用或放弃了基于 SOAP 和 WSDL 的接口,而采用了更易于使用、面向资源的模型来公开其服务。如果考虑使用它的 Web 服务的数量,REST 近年来已经成为最主要的 Web 服务设计模型。

详细参考:https://www.ibm.com/developerworks/cn/webservices/ws-restful/

1、HttpClient

Java 利用 HttpClient 调用 REST 服务时,关键是给 HttpParams 设置超时,如下:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5*1000); //设置连接超时
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30*1000); //等待数据的最大时
HttpClient client = new DefaultHttpClient(params);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值