Java 使用超时进行SOAP调用-Spring引导
我正在尝试为此方法设置超时。实际上,正如您所看到的,我正在调用一个外部服务,需要设置一个超时,以防失败。 你有什么建议吗?春天有什么优雅的方式吗Java 使用超时进行SOAP调用-Spring引导,java,spring,spring-boot,soap,soap-client,Java,Spring,Spring Boot,Soap,Soap Client,我正在尝试为此方法设置超时。实际上,正如您所看到的,我正在调用一个外部服务,需要设置一个超时,以防失败。 你有什么建议吗?春天有什么优雅的方式吗 对于javax.xml.soap.SOAPConnection客户端,您可以使用: private SoapResponseWraper doCall(SOAPMessage request) throws SOAPException, IOException, TimeoutExceededException { log(Level.DEBU
对于
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;
}
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;
}
对于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();
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;
}
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;
}
你为什么不试着用这个词呢? SpringCloudNetflix提供as实现。只需使用
@EnableCircuitBreaker
或者更具体地说,使用@EnableHystrix
注释您的应用程序类,并使用@HystrixCommand(commandProperties={@HystrixProperty(name=“execution.isolation.thread.timeoutInms”,value=“10000”)}注释您的方法doCall(SOAPMessage请求)
您还可以在@HystrixCommand
注释中添加fallbackMethod
,以提供一个方法,如果注释方法出现错误(包括超时),将调用该方法
这是您需要添加到pom中的依赖项:
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>RELEASE</version>
</dependency>
com.netflix.hystrix
这是针对axis客户端的。你能告诉我你在使用什么特定的库吗?当然javax.xml.soap.SOAPConnection
我扩展了javax.xml.soap.soapconnectionxx的答案!我将对其进行测试,并将其作为已解决的问题发布在这里!:)