gpt4 book ai didi

java - 使用超时进行 SOAP 调用 - Spring Boot

转载 作者:行者123 更新时间:2023-11-29 04:21:31 26 4
gpt4 key购买 nike

我正在尝试为此方法设置超时。实际上,如您所见,我正在调用外部服务,我需要设置超时以防失败。你有什么建议吗? Spring有什么优雅的方法吗(注解)?

import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;


private SoapResponseWraper doCall(SOAPMessage request) throws SOAPException, IOException, TimeoutExceededException {
log(Level.DEBUG, createMessage, request, REQUEST_HEADER);
// Makes the call, get the response and prints it;
SOAPMessage response = SOAPConnectionFactory.newInstance().createConnection()
.call(request, new URL(urlStr));
SoapResponseWraper result = new SoapResponseWraper(response);
log(Level.DEBUG, createMessage, response, RESPONSE_HEADER);
return result;
}

最佳答案

对于 javax.xml.soap.SOAPConnection 客户端,您可以使用这个:

private SoapResponseWraper doCall(SOAPMessage request) throws SOAPException, IOException, TimeoutExceededException {
log(Level.DEBUG, createMessage, request, REQUEST_HEADER);
// Makes the call, get the response and prints it;
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();

URL endpoint = new URL(null,
urlStr,
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);
}
});
SOAPMessage response = connection.call(request, endpoint);
SoapResponseWraper result = new SoapResponseWraper(response);
log(Level.DEBUG, createMessage, response, RESPONSE_HEADER);
return result;
}

对于 Axis 客户端,您可以使用:

private SoapResponseWraper doCall(SOAPMessage request) throws SOAPException, IOException, TimeoutExceededException {
log(Level.DEBUG, createMessage, request, REQUEST_HEADER);
// Makes the call, get the response and prints it;
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
connection.setTimeout(10000);
SOAPMessage response = connection.call(request, new URL(urlStr));
SoapResponseWraper result = new SoapResponseWraper(response);
log(Level.DEBUG, createMessage, response, RESPONSE_HEADER);
return result;
}

关于java - 使用超时进行 SOAP 调用 - Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48863221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com