Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.impl;

import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.function.Supplier;

/**
* Default implementation of {@link Supplier} for generating exchange IDs.
*
* @since 5.7
*/
@Contract(threading = ThreadingBehavior.STATELESS)
public class DefaultExchangeIdGenerator implements Supplier<String> {

public static final Supplier<String> INSTANCE = new DefaultExchangeIdGenerator();

@Override
public String get() {
return ExecSupport.getNextExchangeId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.hc.client5.http.impl.ChainElement;
import org.apache.hc.client5.http.impl.CookieSpecSupport;
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
Expand All @@ -76,6 +77,7 @@
import org.apache.hc.core5.function.Callback;
import org.apache.hc.core5.function.Decorator;
import org.apache.hc.core5.function.Resolver;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequestInterceptor;
Expand Down Expand Up @@ -182,6 +184,7 @@ private ExecInterceptorEntry(
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
private LinkedList<ExecInterceptorEntry> execInterceptors;

private Supplier<String> exchangeIdGenerator;
private HttpRoutePlanner routePlanner;
private RedirectStrategy redirectStrategy;
private HttpRequestRetryStrategy retryStrategy;
Expand Down Expand Up @@ -563,6 +566,17 @@ public final H2AsyncClientBuilder setDefaultHeaders(final Collection<? extends H
return this;
}

/**
* Sets exchange ID generator instance.
*
* @return this instance.
* @since 5.7
*/
public final H2AsyncClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
this.exchangeIdGenerator = exchangeIdGenerator;
return this;
}

/**
* Sets {@link HttpRoutePlanner} instance.
*
Expand Down Expand Up @@ -855,6 +869,11 @@ public CloseableHttpAsyncClient build() {
ChainElement.RETRY.name());
}

Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
}

HttpRoutePlanner routePlannerCopy = this.routePlanner;
if (routePlannerCopy == null) {
SchemePortResolver schemePortResolverCopy = this.schemePortResolver;
Expand Down Expand Up @@ -983,6 +1002,7 @@ public CloseableHttpAsyncClient build() {
pushConsumerRegistry,
threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-main", true),
connPool,
exchangeIdGeneratorCopy,
routePlannerCopy,
cookieSpecRegistryCopy,
authSchemeRegistryCopy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
import org.apache.hc.core5.function.Callback;
import org.apache.hc.core5.function.Decorator;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ConnectionReuseStrategy;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
Expand Down Expand Up @@ -233,6 +235,7 @@ private ExecInterceptorEntry(
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
private LinkedList<ExecInterceptorEntry> execInterceptors;

private Supplier<String> exchangeIdGenerator;
private HttpRoutePlanner routePlanner;
private RedirectStrategy redirectStrategy;
private HttpRequestRetryStrategy retryStrategy;
Expand Down Expand Up @@ -695,6 +698,17 @@ public final HttpAsyncClientBuilder setProxy(final HttpHost proxy) {
return this;
}

/**
* Sets exchange ID generator instance.
*
* @return this instance.
* @since 5.7
*/
public final HttpAsyncClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
this.exchangeIdGenerator = exchangeIdGenerator;
return this;
}

/**
* Sets {@link HttpRoutePlanner} instance.
*
Expand Down Expand Up @@ -1140,6 +1154,10 @@ public CloseableHttpAsyncClient build() {
execChainDefinition.addFirst(new TlsRequiredAsyncExec(), ChainElement.TLS_REQUIRED.name());
}

Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
}

HttpRoutePlanner routePlannerCopy = this.routePlanner;
if (routePlannerCopy == null) {
Expand Down Expand Up @@ -1277,6 +1295,7 @@ public CloseableHttpAsyncClient build() {
pushConsumerRegistry,
threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-main", true),
connManagerCopy,
exchangeIdGeneratorCopy,
routePlannerCopy,
tlsConfig,
cookieSpecRegistryCopy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.CookieSpecFactory;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.ExecSupport;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.routing.RoutingSupport;
import org.apache.hc.core5.concurrent.Cancellable;
import org.apache.hc.core5.concurrent.ComplexFuture;
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
Expand Down Expand Up @@ -88,6 +88,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
private static final Logger LOG = LoggerFactory.getLogger(InternalAbstractHttpAsyncClient.class);

private final AsyncExecChainElement execChain;
private final Supplier<String> exchangeIdGenerator;
private final Lookup<CookieSpecFactory> cookieSpecRegistry;
private final Lookup<AuthSchemeFactory> authSchemeRegistry;
private final CookieStore cookieStore;
Expand All @@ -103,6 +104,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
final AsyncPushConsumerRegistry pushConsumerRegistry,
final ThreadFactory threadFactory,
final AsyncExecChainElement execChain,
final Supplier<String> exchangeIdGenerator,
final Lookup<CookieSpecFactory> cookieSpecRegistry,
final Lookup<AuthSchemeFactory> authSchemeRegistry,
final CookieStore cookieStore,
Expand All @@ -112,6 +114,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
final List<Closeable> closeables) {
super(ioReactor, pushConsumerRegistry, threadFactory);
this.execChain = execChain;
this.exchangeIdGenerator = exchangeIdGenerator;
this.cookieSpecRegistry = cookieSpecRegistry;
this.authSchemeRegistry = authSchemeRegistry;
this.cookieStore = cookieStore;
Expand Down Expand Up @@ -232,7 +235,7 @@ protected <T> Future<T> doExecute(
resolvedTarget,
request,
clientContext);
final String exchangeId = ExecSupport.getNextExchangeId();
final String exchangeId = exchangeIdGenerator.get();
clientContext.setExchangeId(exchangeId);
if (LOG.isDebugEnabled()) {
LOG.debug("{} preparing request execution", exchangeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
Expand Down Expand Up @@ -79,6 +80,7 @@ public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient
final AsyncPushConsumerRegistry pushConsumerRegistry,
final ThreadFactory threadFactory,
final InternalH2ConnPool connPool,
final Supplier<String> exchangeIdGenerator,
final HttpRoutePlanner routePlanner,
final Lookup<CookieSpecFactory> cookieSpecRegistry,
final Lookup<AuthSchemeFactory> authSchemeRegistry,
Expand All @@ -87,7 +89,7 @@ public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient
final RequestConfig defaultConfig,
final List<Closeable> closeables,
final int maxQueuedRequests) {
super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
super(ioReactor, pushConsumerRegistry, threadFactory, execChain, exchangeIdGenerator,
cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, HttpClientContext::castOrCreate,
defaultConfig, closeables);
this.connPool = connPool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
Expand Down Expand Up @@ -85,6 +86,7 @@ public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClie
final AsyncPushConsumerRegistry pushConsumerRegistry,
final ThreadFactory threadFactory,
final AsyncClientConnectionManager manager,
final Supplier<String> exchangeIdGenerator,
final HttpRoutePlanner routePlanner,
final TlsConfig tlsConfig,
final Lookup<CookieSpecFactory> cookieSpecRegistry,
Expand All @@ -95,7 +97,7 @@ public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClie
final RequestConfig defaultConfig,
final List<Closeable> closeables,
final int maxQueuedRequests) {
super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
super(ioReactor, pushConsumerRegistry, threadFactory, execChain, exchangeIdGenerator,
cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, contextAdaptor,
defaultConfig, closeables);
this.manager = manager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
Expand Down Expand Up @@ -89,6 +90,7 @@
import org.apache.hc.client5.http.protocol.ResponseProcessCookies;
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ConnectionReuseStrategy;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
Expand Down Expand Up @@ -194,6 +196,7 @@ private ExecInterceptorEntry(
}

private HttpRequestExecutor requestExec;
private Supplier<String> exchangeIdGenerator;
private HttpClientConnectionManager connManager;
private boolean connManagerShared;
private SchemePortResolver schemePortResolver;
Expand Down Expand Up @@ -257,6 +260,17 @@ public final HttpClientBuilder setRequestExecutor(final HttpRequestExecutor requ
return this;
}

/**
* Sets exchange ID generator instance.
*
* @return this instance.
* @since 5.7
*/
public final HttpClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
this.exchangeIdGenerator = exchangeIdGenerator;
return this;
}

/**
* Sets {@link HttpClientConnectionManager} instance.
*
Expand Down Expand Up @@ -865,6 +879,10 @@ public CloseableHttpClient build() {
if (requestExecCopy == null) {
requestExecCopy = new HttpRequestExecutor();
}
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
}
HttpClientConnectionManager connManagerCopy = this.connManager;
if (connManagerCopy == null) {
final PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
Expand Down Expand Up @@ -1139,6 +1157,7 @@ public CloseableHttpClient build() {
return new InternalHttpClient(
connManagerCopy,
requestExecCopy,
exchangeIdGeneratorCopy,
execChain,
routePlannerCopy,
cookieSpecRegistryCopy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.CookieSpecFactory;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.ExecSupport;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
Expand All @@ -52,6 +51,7 @@
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.concurrent.CancellableDependency;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpException;
Expand Down Expand Up @@ -85,6 +85,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {

private final HttpClientConnectionManager connManager;
private final HttpRequestExecutor requestExecutor;
private final Supplier<String> exchangeIdGenerator;
private final ExecChainElement execChain;
private final HttpRoutePlanner routePlanner;
private final Lookup<CookieSpecFactory> cookieSpecRegistry;
Expand All @@ -98,6 +99,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
public InternalHttpClient(
final HttpClientConnectionManager connManager,
final HttpRequestExecutor requestExecutor,
final Supplier<String> exchangeIdGenerator,
final ExecChainElement execChain,
final HttpRoutePlanner routePlanner,
final Lookup<CookieSpecFactory> cookieSpecRegistry,
Expand All @@ -110,6 +112,7 @@ public InternalHttpClient(
super();
this.connManager = Args.notNull(connManager, "Connection manager");
this.requestExecutor = Args.notNull(requestExecutor, "Request executor");
this.exchangeIdGenerator = Args.notNull(exchangeIdGenerator, "Exchange id generator");
this.execChain = Args.notNull(execChain, "Execution chain");
this.routePlanner = Args.notNull(routePlanner, "Route planner");
this.cookieSpecRegistry = cookieSpecRegistry;
Expand Down Expand Up @@ -173,7 +176,7 @@ protected CloseableHttpResponse doExecute(
resolvedTarget,
request,
localcontext);
final String exchangeId = ExecSupport.getNextExchangeId();
final String exchangeId = exchangeIdGenerator.get();
localcontext.setExchangeId(exchangeId);
if (LOG.isDebugEnabled()) {
LOG.debug("{} preparing request execution", exchangeId);
Expand Down
Loading