0

可以CachingHttpAsyncClientAsyncRestTemplate吗?HttpComponentsAsyncClientHttpRequestFactory期望 aCloseableHttpAsyncClientCachingHttpAsyncClient不扩展它。

4

1 回答 1

0

对于最高 4.3.9 和 5.0.RC2 的版本,这称为问题SPR-15664 - 在 4.3.10 和 5.0.RC3 中已修复。唯一的方法是创建一个AsyncClientHttpRequestFactory基于现有的自定义实现HttpComponentsAsyncClientHttpRequestFactory

// package required for HttpComponentsAsyncClientHttpRequest visibility
package org.springframework.http.client;

import java.io.IOException;
import java.net.URI;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.Configurable;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpAsyncClient;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.protocol.HttpContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

// TODO add support for other CachingHttpAsyncClient otpions, e.g. HttpCacheStorage
public class HttpComponentsCachingAsyncClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory implements AsyncClientHttpRequestFactory, InitializingBean {

    private final CloseableHttpAsyncClient wrappedHttpAsyncClient;
    private final CachingHttpAsyncClient cachingHttpAsyncClient;

    public HttpComponentsCachingAsyncClientHttpRequestFactory() {
        this(HttpAsyncClients.createDefault(), CacheConfig.DEFAULT);
    }

    public HttpComponentsCachingAsyncClientHttpRequestFactory(final CacheConfig config) {
        this(HttpAsyncClients.createDefault(), config);
    }

    public HttpComponentsCachingAsyncClientHttpRequestFactory(final CloseableHttpAsyncClient client) {
        this(client, CacheConfig.DEFAULT);
    }

    public HttpComponentsCachingAsyncClientHttpRequestFactory(final CloseableHttpAsyncClient client, final CacheConfig config) {
        Assert.notNull(client, "HttpAsyncClient must not be null");
        wrappedHttpAsyncClient = client;
        cachingHttpAsyncClient = new CachingHttpAsyncClient(client, config);
    }

    @Override
    public void afterPropertiesSet() {
        startAsyncClient();
    }

    private void startAsyncClient() {
        if (!wrappedHttpAsyncClient.isRunning()) {
            wrappedHttpAsyncClient.start();
        }
    }

    @Override
    public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
        throw new IllegalStateException("Synchronous execution not supported");
    }

    @Override
    public AsyncClientHttpRequest createAsyncRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
        startAsyncClient();
        final HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
        postProcessHttpRequest(httpRequest);
        HttpContext context = createHttpContext(httpMethod, uri);
        if (context == null) {
            context = HttpClientContext.create();
        }
        // Request configuration not set in the context
        if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
            // Use request configuration given by the user, when available
            RequestConfig config = null;
            if (httpRequest instanceof Configurable) {
                config = ((Configurable) httpRequest).getConfig();
            }
            if (config == null) {
                config = createRequestConfig(cachingHttpAsyncClient);
            }
            if (config != null) {
                context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
            }
        }
        return new HttpComponentsAsyncClientHttpRequest(cachingHttpAsyncClient, httpRequest, context);
    }

    @Override
    public void destroy() throws Exception {
        try {
            super.destroy();
        } finally {
            wrappedHttpAsyncClient.close();
        }
    }

}
于 2017-06-14T08:14:01.203 回答