gpt4 book ai didi

java - Spring webServiceTemplate 连接超时属性

转载 作者:行者123 更新时间:2023-11-30 03:23:21 26 4
gpt4 key购买 nike

看起来 Spring 的 WebServiceTemplate 忽略了连接超时的设置。那,或者我误解了连接超时。

这是我的配置:

@Bean
public SoapTemplate soapTemplate(Jaxb2Marshaller marshaller, WebServiceMessageSender webServiceMessageSender ) {

SoapTemplate template = new SoapTemplate();
template.setDefaultUri("some_url");
template.setMarshaller(marshaller);
template.setUnmarshaller(marshaller);
template.setMessageSender(webServiceMessageSender);
return template;
}

@Bean
public WebServiceMessageSender webServiceMessageSender() {

HttpComponentsMessageSender sender = new HttpComponentsMessageSender();

sender.setReadTimeout(5*1000);
sender.setConnectionTimeout(5*1000);

return sender;

}

Soap 模板扩展了 WebServiceGateway 并具有一些特定的日志记录、自定义异常功能。

当我启动应接收 WebServiceTemplate 生成的 SOAP 消息的目标应用程序时,读取超时按预期工作。如果服务器在指定时间内没有响应,则抛出异常。

我的理解是,如果 TCP 3 次握手未在指定时间内发生,则应发生连接超时。然而,情况并非如此,因为无论我的配置如何,超时总是在大约 2 秒后发生。

这是一个有效的测试,还是应该使用在线但无法接受新连接的拥塞服务器来测试连接超时?我的客户当时遇到了哪种超时?可以控制吗?

最佳答案

这并不能解释问题,但 HttpComponentsMessageSender 的默认构造函数使用已弃用的 org.apache.http.impl.client.DefaultHttpClient。尝试使用 org.apache.http.client.HttpClient:

@Bean
public HttpComponentsMessageSender webServiceMessageSender() {

final HttpClient httpClient = buildHttpClient(5*1000, 5*1000, 2, true);
final HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(httpClient);
return messageSender;
}

@SuppressWarnings("resource")
private static HttpClient buildHttpClient(final int connectionTimeout, final int socketTimeout, final int retryCount, final boolean trustAll) {

// timeout config
final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(socketTimeout).build();

// trust all (if selected)
SSLConnectionSocketFactory sslSocketFactory = null;
if (trustAll) {
try {
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustAllStrategy()).build();
sslSocketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new RuntimeException("could not initialize SSL context, reason: " + e.getMessage(), e);
}
}

// socket factory
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", trustAll ? sslSocketFactory : SSLConnectionSocketFactory.getSocketFactory()).build();

// retry handler
final HttpRequestRetryHandler retryHandler = new SimpleRetryHandler(retryCount);

// build client
final HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)).setRetryHandler(retryHandler)
.addInterceptorFirst(new RemoveSoapHeadersInterceptor()).build();

return httpClient;
}

更新:在我们使用 SSL 连接的项目中使用了自定义 buildHttpClient 方法。如果您不需要 SSL 部分,请尝试逐步将其删除。

关于java - Spring webServiceTemplate 连接超时属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30803905/

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