How to use connection pooling with HttpsUrlConnectionMessageSender

前提是你 提交于 2021-02-07 10:35:21

问题


I am trying to make a Soap request using Spring WS. The request needs to send across client certificate to the server. I have figured out the configuration that makes the correct request to the server. Code presented below

@Bean
    public HttpsUrlConnectionMessageSender messageSender() throws Exception {
        HttpsUrlConnectionMessageSender httpsUrlConnectionMessageSender = new HttpsUrlConnectionMessageSender();
        httpsUrlConnectionMessageSender.setTrustManagers(<TrustManager>);
        httpsUrlConnectionMessageSender.setKeyManagers(<KeyManager>);
        httpsUrlConnectionMessageSender.setHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return httpsUrlConnectionMessageSender;
    }

    @Bean
    @Scope
    public WebServiceTemplate webServiceTemplate() throws Exception {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate(<messageFactory>);
        webServiceTemplate.setMarshaller(<jaxb2Marshaller>);
        webServiceTemplate.setUnmarshaller(<jaxb2Marshaller>);
        webServiceTemplate.setDefaultUri(<URL>);
        webServiceTemplate.setMessageSender(());
        return webServiceTemplate;
    }

Now I was wondering how does HttpsUrlConnectionMessageSender manage connections? Does it create new connection for every request or it has internal connection pooling. How do we use external connection pooling here?


回答1:


The client has default connection pool of 20 and default connection pool per request 2.

But you can overwrite it like this:

public HttpComponentsMessageSender httpComponentsMessageSender(Integer readTimeout, Integer connectionTimeOut,
            Integer throttlingLimit) {
    HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
    sender.setReadTimeout(readTimeout);
    sender.setConnectionTimeout(connectionTimeOut);
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    CloseableHttpClient poolingHttpSoapClient = HttpClients.custom().setConnectionManager(connManager)
            .addInterceptorFirst(new RemoveSoapHeadersInterceptor()).setMaxConnPerRoute(throttlingLimit)
            .setMaxConnTotal(throttlingLimit + 1).build();
    sender.setHttpClient(poolingHttpSoapClient);
    return sender;
}


来源:https://stackoverflow.com/questions/48380342/how-to-use-connection-pooling-with-httpsurlconnectionmessagesender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!