如何在使用WebServiceTemplate和CommonsHttpMessageSender时为不同的URI设置不同的超时?

时间:2014-09-03 08:59:23

标签: httpclient spring-ws connection-timeout webservicetemplate

您好我有关于设置连接超时的问题。 我使用WebServiceTemplate,它有1个messageSender(CommonsHttpMessageSender)。 但是,是否可以为每个URI(端点)设置不同的连接超时?

...此致

public Message sendRequest(OutgoingRequest message, MessageHeaders headers,
                        EndpointInfoProvider endpointInfoProvider,
                        WebServiceMessageCallback requestCallback){
    Assert.notNull(endpointInfoProvider, "Destination provider is required!");
    final Credentials credentials = endpointInfoProvider.getCredentials();
    URI destinationUri = endpointInfoProvider.getDestination();
    for (WebServiceMessageSender messageSender : webServiceTemplate.getMessageSenders()) {
        if (messageSender instanceof CommonsHttpMessageSender) {
            HttpClient httpClient = ((CommonsHttpMessageSender) messageSender).getHttpClient();
            httpClient.getState().setCredentials(
                    new AuthScope(destinationUri.getHost(),
                            destinationUri.getPort(), AuthScope.ANY_REALM,
                            AuthScope.ANY_SCHEME), credentials
            );
            httpClient.getParams().setAuthenticationPreemptive(true);
            ((CommonsHttpMessageSender) messageSender)
                    .setConnectionTimeout(endpointInfoProvider
                            .getTimeOutDuration());
        }
    }
    webServiceTemplate.setDestinationProvider(endpointInfoProvider);
    //........
}

1 个答案:

答案 0 :(得分:0)

Spring WS不支持不同URI的不同超时,并且不打算添加此类功能。鉴于您的问题涉及WebServiceTemplate,您可以轻松配置具有不同超时的多个模板,并根据需要使用它们。