Skip to content
Open
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,49 @@
/*
* ====================================================================
* 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;

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

/**
* Generates exchange identifiers for message exchanges.
*
* @since 5.7
*/
@Contract(threading = ThreadingBehavior.STATELESS)
public interface ExchangeIdGenerator {

/**
* Generates the next exchange identifier.
* <p>
* The returned identifier must be unique, non-null, and non-blank.
* </p>
*
* @return the next exchange identifier
*/
String getNextExchangeId();
}
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.client5.http.ExchangeIdGenerator;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;

/**
* The default implementation of {@link ExchangeIdGenerator}.
*
* @since 5.7
*/
@Contract(threading = ThreadingBehavior.STATELESS)
public class DefaultExechangeIdGenerator implements ExchangeIdGenerator {

public static final ExchangeIdGenerator INSTANCE = new DefaultExechangeIdGenerator();

@Override
public String getNextExchangeId() {
return ExecSupport.getNextExchangeId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.SchemePortResolver;
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
Expand All @@ -51,6 +52,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.DefaultExechangeIdGenerator;
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 @@ -182,6 +184,7 @@ private ExecInterceptorEntry(
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
private LinkedList<ExecInterceptorEntry> execInterceptors;

private ExchangeIdGenerator 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 {@link ExchangeIdGenerator} instance.
*
* @return this instance.
* @since 5.7
*/
public final H2AsyncClientBuilder setExchangeIdGenerator(final ExchangeIdGenerator exchangeIdGenerator) {
this.exchangeIdGenerator = exchangeIdGenerator;
return this;
}

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

ExchangeIdGenerator exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExechangeIdGenerator.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 @@ -42,6 +42,7 @@
import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.EarlyHintsListener;
import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.SchemePortResolver;
import org.apache.hc.client5.http.UserTokenHandler;
Expand All @@ -59,6 +60,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.DefaultExechangeIdGenerator;
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 @@ -233,6 +235,7 @@ private ExecInterceptorEntry(
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
private LinkedList<ExecInterceptorEntry> execInterceptors;

private ExchangeIdGenerator 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 {@link ExchangeIdGenerator} instance.
*
* @return this instance.
* @since 5.7
*/
public final HttpAsyncClientBuilder setExchangeIdGenerator(final ExchangeIdGenerator 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());
}

ExchangeIdGenerator exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExechangeIdGenerator.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 @@ -40,6 +40,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.async.AsyncExecCallback;
import org.apache.hc.client5.http.async.AsyncExecChain;
Expand All @@ -50,7 +51,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.protocol.HttpClientContext;
import org.apache.hc.client5.http.routing.RoutingSupport;
import org.apache.hc.core5.concurrent.Cancellable;
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 ExchangeIdGenerator 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 ExchangeIdGenerator 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.getNextExchangeId();
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 @@ -31,6 +31,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.async.AsyncExecRuntime;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
Expand Down Expand Up @@ -79,6 +80,7 @@ public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient
final AsyncPushConsumerRegistry pushConsumerRegistry,
final ThreadFactory threadFactory,
final InternalH2ConnPool connPool,
final ExchangeIdGenerator 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 @@ -32,6 +32,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.async.AsyncExecRuntime;
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
Expand Down Expand Up @@ -85,6 +86,7 @@ public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClie
final AsyncPushConsumerRegistry pushConsumerRegistry,
final ThreadFactory threadFactory,
final AsyncClientConnectionManager manager,
final ExchangeIdGenerator 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 @@ -41,6 +41,7 @@

import org.apache.hc.client5.http.AuthenticationStrategy;
import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.ExchangeIdGenerator;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.SchemePortResolver;
import org.apache.hc.client5.http.UserTokenHandler;
Expand All @@ -61,6 +62,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.DefaultExechangeIdGenerator;
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 @@ -194,6 +196,7 @@ private ExecInterceptorEntry(
}

private HttpRequestExecutor requestExec;
private ExchangeIdGenerator 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 {@link ExchangeIdGenerator} instance.
*
* @return this instance.
* @since 5.7
*/
public final HttpClientBuilder setExchangeIdGenerator(final ExchangeIdGenerator 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();
}
ExchangeIdGenerator exchangeIdGeneratorCopy = this.exchangeIdGenerator;
if (exchangeIdGeneratorCopy == null) {
exchangeIdGeneratorCopy = DefaultExechangeIdGenerator.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
Loading