在 Spring (HttpsUrlConnectionMessageSender) 中将超时设置为 WS https 调用 [英] Set timeout to WS https call in Spring (HttpsUrlConnectionMessageSender)

查看:20
本文介绍了在 Spring (HttpsUrlConnectionMessageSender) 中将超时设置为 WS https 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 WS 调用设置超时.我扩展了 WebServiceGatewaySupport 并试图像这样设置发件人超时:

I am trying to set timeout for WS call. I extended WebServiceGatewaySupport and was trying to set to Sender timeout like this:

public Object marshalSendAndReceive(Object requestPayload) {

        WebServiceTemplate wsTemplate = this.getWebServiceTemplate();
        for (WebServiceMessageSender sender : wsTemplate.getMessageSenders()) {
            try {
                HttpComponentsMessageSender httpSender = (HttpComponentsMessageSender) sender;
                httpSender.setReadTimeout(3000);
                httpSender.setConnectionTimeout(2000);
            } catch (ClassCastException | NumberFormatException cex) {
                logger.warn("Cannot set WS timeout: " + cex.getMessage());
            }
        }

        return wsTemplate.marshalSendAndReceive(requestPayload);
    }

(归功于问题 #6733744)

(credit to question #6733744)

但是我得到:Cannot set WS timeout: org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender cannot be cast to org.springframework.ws.transport.http.HttpComponentsMessageSender

可以以某种方式将超时设置为 HttpsUrlConnectionMessageSender 吗?或者有没有其他方法可以在 spring-boot 中将超时设置为 https ws 调用?

Can timeout be set to HttpsUrlConnectionMessageSender somehow? Or is there any other way to set timeout to https ws call in spring-boot?

谢谢.

推荐答案

我遇到了同样的问题,并设法使用 HttpComponentsMessageSender.这是我的代码:

I had the same issue, and managed to make it work using HttpComponentsMessageSender. Here is my code:

HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender();
HttpClient httpClient = HttpClientFactory.getHttpsClient(sslUtils, timeout);
messageSender.setHttpClient(httpClient);
webServiceTemplate.setMessageSender(messageSender);

我还创建了一个新的工厂类 HttpClientFactory 来设置 SSL 和超时:

I also created a new factory class HttpClientFactory that sets the SSL and timeout:

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

public class HttpClientFactory {

    private static CloseableHttpClient client;

    private HttpClientFactory() {
    }

    public static HttpClient getHttpsClient(SslUtils sslUtils, int timeout) throws Exception {

        if (client != null) {
            return client;
        }

        SSLContext sslcontext = getSSLContext(sslUtils);
        SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.addInterceptorFirst(new ContentLengthHeaderRemover());
        RequestConfig config = RequestConfig.custom()
                                    .setConnectTimeout(timeout)
                                    .setConnectionRequestTimeout(timeout)
                                    .setSocketTimeout(timeout)
                                    .build();

        return httpClientBuilder.setSSLSocketFactory(factory)
                    .setDefaultRequestConfig(config)
                    .build();
    }

    private static class ContentLengthHeaderRemover implements HttpRequestInterceptor {
        @Override
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            request.removeHeaders(HTTP.CONTENT_LEN);
        }
    }

    public static void releaseInstance() {
        client = null;
    }

    private static SSLContext getSSLContext(SslUtils sslUtils) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException {

        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(sslUtils.getKeystore().getInputStream(), sslUtils.getKeyPwd().toCharArray());
        sslUtils.getKeystore().getInputStream().close();

        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(sslUtils.getTrustStore().getInputStream(), sslUtils.getTrustPwd().toCharArray());
        sslUtils.getTrustStore().getInputStream().close();

        SSLContextBuilder sslContextBuilder = SSLContexts.custom();
        try {
            sslContextBuilder = SSLContexts.custom().loadKeyMaterial(ks, ssl.getKeyPwd().toCharArray());
        } catch (UnrecoverableKeyException e) {
            e.printStack();
        }
        sslContextBuilder.loadTrustMaterial(ts, new TrustSelfSignedStrategy());
        return sslContextBuilder.build();
    }
}

有关信息,SslUtils 只是一个保存密钥库和信任库信息的 bean 类:

For information the SslUtils is just a bean class that holds the keystore and truststore informations' :

public class SslUtils {

    private Resource keystore;
    private String keyPwd;
    private Resource trustStore;
    private String trustPwd;

    // Getters and Setters
}

这对我有用,让我同时使用 SSL 和超时.我希望这会对其他人有所帮助.

This works for me and let me use both SSL and timeout at the same. I hope this will help others.

这篇关于在 Spring (HttpsUrlConnectionMessageSender) 中将超时设置为 WS https 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆