java 如何为 SOAP 调用设置超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7099772/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:37:31  来源:igfitidea点击:

How to set a timeout to a SOAP call

javasoaptimeout

提问by user393381

I need to set a timeout to a SOAP call using javax.xml.soap over HTTPS However I don't know how to do that, there must be a trick to do it but I could not find it.

我需要通过 HTTPS 使用 javax.xml.soap 为 SOAP 调用设置超时但是我不知道如何做到这一点,必须有一个技巧来做到这一点,但我找不到它。

SOAPMessage sm = null;
SOAPMessage response = null;

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();

MessageFactory mf = MessageFactory.newInstance();
sm = mf.createMessage();
...
...
URL url = new URL("https://server:XXXX/blablabla);
response = connection.call(sm, url);

I saw someone doing:

我看到有人在做:

if (xxxSoapService instanceof Stub)
            ((Stub) xxxSoapService).setTimeout(10000);

xxxSoapService extends java.rmi.Remote and Stub is from import org.apache.axis.client.Stub;

xxxSoapService 扩展了 java.rmi.Remote 并且 Stub 来自 import org.apache.axis.client.Stub;

There is probably something I am missing there.

那里可能有我想念的东西。

采纳答案by George

Assuming you're already doing your .call() in a background thread. You can have a timer fire on a different thread and kill the loading thread.

假设您已经在后台线程中执行 .call() 。您可以在不同的线程上触发计时器并终止加载线程。

Alternately, since SOAPMessage has all of your data you can just use HttpUrlConnection to send the message.

或者,由于 SOAPMessage 拥有您的所有数据,您可以只使用 HttpUrlConnection 来发送消息。

HttpUrlConnection connection = // initialize me!
connection.setReadTimeout(TIMEOUT_VALUE);
SOAPMessage sm = // initialize me!

// more stuff for your message

connection.connect();

sm.writeTo(connection.getOutputStream());

This should work, unless I'm mistaken about the behavior of writeTo().

这应该有效,除非我对 writeTo() 的行为有误解。