Java 设置 jax-ws 客户端超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3130913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting jax-ws client timeout
提问by Lukasz
I've trouble setting jax-ws timeout. My code is:
我在设置 jax-ws 超时时遇到问题。我的代码是:
@WebServiceClient(name = "VoipDBJDBCService", targetNamespace = "http://db.server.voipmeter.jextreme.eu/", wsdlLocation = "http://trace0.nyc.blinkmind.com:8080/voipdb?wsdl")
public class VoipDBJDBCService extends Service {
public VoipDBJDBCService(URL wsdlLocation) {
super(wsdlLocation, new QName("http://db.server.voipmeter.jextreme.eu/", "VoipDBJDBCService"));
}
@WebEndpoint(name = "VoipDBJDBCPort")
public VoipDB getVoipDBJDBCPort() {
return super.getPort(new QName("http://db.server.voipmeter.jextreme.eu/", "VoipDBJDBCPort"), VoipDB.class);
}
}
And the usage:
以及用法:
VoipDB db = new VoipDBJDBCService(new URL(url)).getVoipDBJDBCPort();
How do i Hook in timeouts ? I've found "solution" here: https://jax-ws.dev.java.net/guide/HTTP_Timeouts.htmlbut I don't know where I shall hook it in. How to obtain proxy ? When I call getPort client tries to connect and then hangs forever if server is not responding.
我如何挂钩超时?我在这里找到了“解决方案”:https: //jax-ws.dev.java.net/guide/HTTP_Timeouts.html但我不知道我应该把它挂在哪里。如何获得代理?当我调用 getPort 客户端尝试连接时,如果服务器没有响应,则永远挂起。
UPDATE: This code is invoked from within applets init() method if that makes any difference.
更新:如果有任何区别,此代码是从小程序 init() 方法中调用的。
回答by musiKk
You can cast your VoipDB
object to BindingProvider
. So in the example in the link you've given just replace proxy
by db
and you're good to go.
您可以将VoipDB
对象投射到BindingProvider
. 因此,在链接的例子你给只需更换proxy
的db
,你是好去。
回答by jarnbjo
If you are using a Sun JRE, you can set the following system properties for default network connect and read timeouts (in milliseconds). I haven't tried these with the JAX-WS client, but they ought work there as well:
如果您使用的是 Sun JRE,则可以为默认网络连接和读取超时(以毫秒为单位)设置以下系统属性。我还没有在 JAX-WS 客户端上尝试过这些,但它们也应该在那里工作:
sun.net.client.defaultConnectTimeout
sun.net.client.defaultReadTimeout
Addition: I missed your last part of the question where you said that you are doing this in an applet. If the applet is running with default permissions, you are probably not allowed to set the system properties.
另外:我错过了问题的最后一部分,您说您是在小程序中执行此操作。如果小程序以默认权限运行,则可能不允许您设置系统属性。
回答by Wanderson Santos
With Metro/Glassfish...
随着地铁/玻璃鱼...
//1 minute for connection
((BindingProvider) wsPort).getRequestContext().put("com.sun.xml.ws.connect.timeout", 1 * 60 * 1000);
//3 minutos for request
((BindingProvider) wsPort).getRequestContext().put("com.sun.xml.ws.request.timeout", 3 * 60 * 1000);
回答by Daniel Kaplan
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);
This worked for me.
这对我有用。
回答by kumar saurav
Here is one example
这是一个例子
public void testConfigureTimeout() throws Exception
{
//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");
port.echo("testTimeout");
}