繁体   English   中英

如何为 JAX-WS Web 服务客户端设置超时?

[英]How do I set the timeout for a JAX-WS webservice client?

我已经使用 JAXWS-RI 2.1 为我的 Web 服务创建了一个基于 WSDL 的接口。 我可以毫无问题地与 Web 服务交互,但无法指定向 Web 服务发送请求的超时时间。 如果由于某种原因它没有响应,客户端似乎永远在旋转它的轮子。

四处寻找表明我可能应该尝试做这样的事情:

((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.ws.request.timeout", 10000);
((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.ws.connect.timeout", 10000);

我还发现,根据您拥有的 JAXWS-RI 版本,您可能需要设置这些属性:

((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 10000);
((BindingProvider)myInterface).getRequestContext().put("com.sun.xml.internal.ws.connect.timeout", 10000);

我遇到的问题是,无论以上哪一项是正确的,我都不知道在哪里可以做到这一点。 我所拥有的只是一个Service子类,它实现了 webservice 的自动生成的接口,并且在它被实例化的时候,如果 WSDL 没有响应,那么设置属性已经太晚了:

MyWebServiceSoap soap;
MyWebService service = new MyWebService("http://www.google.com");
soap = service.getMyWebServiceSoap();
soap.sendRequestToMyWebService();

谁能指出我正确的方向?!

我知道这是旧的并在其他地方回答,但希望这可以解决这个问题。 我不确定为什么要动态下载 WSDL 但系统属性:

sun.net.client.defaultConnectTimeout (default: -1 (forever))
sun.net.client.defaultReadTimeout (default: -1 (forever))

应该适用于使用 JAX-WS 使用的 HttpURLConnection 的所有读取和连接。 如果您从远程位置获取 WSDL,这应该可以解决您的问题 - 但本地磁盘上的文件可能更好!

接下来,如果您想为特定服务设置超时,一旦您创建了代理,您需要将其转换为 BindingProvider(您已经知道),获取请求上下文并设置您的属性。 在线 JAX-WS 文档是错误的,这些是正确的属性名称(好吧,它们对我有用)。

MyInterface myInterface = new MyInterfaceService().getMyInterfaceSOAP();
Map<String, Object> requestContext = ((BindingProvider)myInterface).getRequestContext();
requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 3000); // Timeout in millis
requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 1000); // Timeout in millis
myInterface.callMyRemoteMethodWith(myParameter);

当然,这是一种可怕的做事方式,我会创建一个很好的工厂来生产这些绑定提供程序,这些提供程序可以注入您想要的超时。

接受的答案中的属性对我不起作用,可能是因为我使用的是 JAX-WS 的 JBoss 实现?

使用一组不同的属性(在JBoss JAX-WS 用户指南中找到)使其工作:

//Set timeout until a connection is established
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", "6000");

//Set timeout until the response is received
((BindingProvider) port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", "1000");

这是我的工作解决方案:

// --------------------------
// SOAP Message creation
// --------------------------
SOAPMessage sm = MessageFactory.newInstance().createMessage();
sm.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
sm.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

SOAPPart sp = sm.getSOAPPart();
SOAPEnvelope se = sp.getEnvelope();
se.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
se.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

SOAPBody sb = sm.getSOAPBody();
// 
// Add all input fields here ...
// 

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
// -----------------------------------
// URL creation with TimeOut connexion
// -----------------------------------
URL endpoint = new URL(null,
                      "http://myDomain/myWebService.php",
                    new URLStreamHandler() { // Anonymous (inline) class
                    @Override
                    protected URLConnection openConnection(URL url) throws IOException {
                    URL clone_url = new URL(url.toString());
                    HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();
                    // TimeOut settings
                    clone_urlconnection.setConnectTimeout(10000);
                    clone_urlconnection.setReadTimeout(10000);
                    return(clone_urlconnection);
                    }
                });


try {
    // -----------------
    // Send SOAP message
    // -----------------
    SOAPMessage retour = connection.call(sm, endpoint);
}
catch(Exception e) {
    if ((e instanceof com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl) && (e.getCause()!=null) && (e.getCause().getCause()!=null) && (e.getCause().getCause().getCause()!=null)) {
        System.err.println("[" + e + "] Error sending SOAP message. Initial error cause = " + e.getCause().getCause().getCause());
    }
    else {
        System.err.println("[" + e + "] Error sending SOAP message.");

    }
}
ProxyWs proxy = (ProxyWs) factory.create();
Client client = ClientProxy.getClient(proxy);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(0);
httpClientPolicy.setReceiveTimeout(0);
http.setClient(httpClientPolicy);

这对我有用。

如果您在 JDK6 上使用 JAX-WS,请使用以下属性:

com.sun.xml.internal.ws.connect.timeout  
com.sun.xml.internal.ws.request.timeout

如果您的应用服务器是 WebLogic(对我来说是 10.3.6),那么负责超时的属性是:

com.sun.xml.ws.connect.timeout 
com.sun.xml.ws.request.timeout

不确定这是否对您的上下文有帮助...

可以将soap 对象转换为BindingProvider 吗?

MyWebServiceSoap soap;
MyWebService service = new MyWebService("http://www.google.com");
soap = service.getMyWebServiceSoap();
// set timeouts here
((BindingProvider)soap).getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 10000);
    soap.sendRequestToMyWebService();

另一方面,如果您想在 MyWebService 对象的初始化中设置超时,那么这将无济于事。

当想要超时单个 WebService 调用时,这对我有用。

在实例化 SEI 时避免缓慢检索远程 WSDL 的最简单方法是不在运行时从远程服务端点检索 WSDL。

这意味着您必须在服务提供者进行影响性更改的任何时候更新本地 WSDL 副本,但这也意味着您必须在服务提供者进行影响性更改时更新本地副本。

当我生成我的客户端存根时,我告诉 JAX-WS 运行时以这样一种方式注释 SEI,它将从类路径上的预定位置读取 WSDL。 默认情况下,该位置相对于 Service SEI 的包位置


<wsimport
    sourcedestdir="${dao.helter.dir}/build/generated"
    destdir="${dao.helter.dir}/build/bin/generated"
    wsdl="${dao.helter.dir}/src/resources/schema/helter/helterHttpServices.wsdl"
    wsdlLocation="./wsdl/helterHttpServices.wsdl"
    package="com.helter.esp.dao.helter.jaxws"
    >
    <binding dir="${dao.helter.dir}/src/resources/schema/helter" includes="*.xsd"/>
</wsimport>
<copy todir="${dao.helter.dir}/build/bin/generated/com/helter/esp/dao/helter/jaxws/wsdl">
    <fileset dir="${dao.helter.dir}/src/resources/schema/helter" includes="*" />
</copy>

wsldLocation 属性告诉 SEI 在哪里可以找到 WSDL,并且副本确保 wsdl(并支持 xsd.. 等)位于正确的位置。

由于该位置相对于 SEI 的包位置,我们创建了一个名为 wsdl 的新子包(目录),并将所有 wsdl 工件复制到那里。

此时您要做的就是确保在创建客户端存根工件 jar 文件时包含所有 *.wsdl、*.xsd 和所有 *.class。

(如果你好奇的话,@webserviceClient 注释是这个 wsdl 位置在 java 代码中实际设置的位置

@WebServiceClient(name = "httpServices", targetNamespace = "http://www.helter.com/schema/helter/httpServices", wsdlLocation = "./wsdl/helterHttpServices.wsdl")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM