Skip to content

Commit f457c8b

Browse files
yhzdysok2c
authored andcommitted
Add custom ExchangeId generator support
1 parent c20b312 commit f457c8b

File tree

9 files changed

+151
-8
lines changed

9 files changed

+151
-8
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.client5.http.impl;
28+
29+
import org.apache.hc.core5.annotation.Contract;
30+
import org.apache.hc.core5.annotation.ThreadingBehavior;
31+
import org.apache.hc.core5.function.Supplier;
32+
33+
/**
34+
* Default implementation of {@link Supplier} for generating exchange IDs.
35+
*
36+
* @since 5.7
37+
*/
38+
@Contract(threading = ThreadingBehavior.STATELESS)
39+
public class DefaultExchangeIdGenerator implements Supplier<String> {
40+
41+
public static final Supplier<String> INSTANCE = new DefaultExchangeIdGenerator();
42+
43+
@Override
44+
public String get() {
45+
return ExecSupport.getNextExchangeId();
46+
}
47+
}

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.hc.client5.http.impl.ChainElement;
5252
import org.apache.hc.client5.http.impl.CookieSpecSupport;
5353
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
54+
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
5455
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
5556
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
5657
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
@@ -76,6 +77,7 @@
7677
import org.apache.hc.core5.function.Callback;
7778
import org.apache.hc.core5.function.Decorator;
7879
import org.apache.hc.core5.function.Resolver;
80+
import org.apache.hc.core5.function.Supplier;
7981
import org.apache.hc.core5.http.Header;
8082
import org.apache.hc.core5.http.HttpHost;
8183
import org.apache.hc.core5.http.HttpRequestInterceptor;
@@ -182,6 +184,7 @@ private ExecInterceptorEntry(
182184
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
183185
private LinkedList<ExecInterceptorEntry> execInterceptors;
184186

187+
private Supplier<String> exchangeIdGenerator;
185188
private HttpRoutePlanner routePlanner;
186189
private RedirectStrategy redirectStrategy;
187190
private HttpRequestRetryStrategy retryStrategy;
@@ -563,6 +566,17 @@ public final H2AsyncClientBuilder setDefaultHeaders(final Collection<? extends H
563566
return this;
564567
}
565568

569+
/**
570+
* Sets exchange ID generator instance.
571+
*
572+
* @return this instance.
573+
* @since 5.7
574+
*/
575+
public final H2AsyncClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
576+
this.exchangeIdGenerator = exchangeIdGenerator;
577+
return this;
578+
}
579+
566580
/**
567581
* Sets {@link HttpRoutePlanner} instance.
568582
*
@@ -855,6 +869,11 @@ public CloseableHttpAsyncClient build() {
855869
ChainElement.RETRY.name());
856870
}
857871

872+
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
873+
if (exchangeIdGeneratorCopy == null) {
874+
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
875+
}
876+
858877
HttpRoutePlanner routePlannerCopy = this.routePlanner;
859878
if (routePlannerCopy == null) {
860879
SchemePortResolver schemePortResolverCopy = this.schemePortResolver;
@@ -983,6 +1002,7 @@ public CloseableHttpAsyncClient build() {
9831002
pushConsumerRegistry,
9841003
threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-main", true),
9851004
connPool,
1005+
exchangeIdGeneratorCopy,
9861006
routePlannerCopy,
9871007
cookieSpecRegistryCopy,
9881008
authSchemeRegistryCopy,

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
5959
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
6060
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
61+
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
6162
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
6263
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
6364
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
@@ -90,6 +91,7 @@
9091
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
9192
import org.apache.hc.core5.function.Callback;
9293
import org.apache.hc.core5.function.Decorator;
94+
import org.apache.hc.core5.function.Supplier;
9395
import org.apache.hc.core5.http.ConnectionReuseStrategy;
9496
import org.apache.hc.core5.http.Header;
9597
import org.apache.hc.core5.http.HttpHost;
@@ -230,6 +232,7 @@ private ExecInterceptorEntry(
230232
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
231233
private LinkedList<ExecInterceptorEntry> execInterceptors;
232234

235+
private Supplier<String> exchangeIdGenerator;
233236
private HttpRoutePlanner routePlanner;
234237
private RedirectStrategy redirectStrategy;
235238
private HttpRequestRetryStrategy retryStrategy;
@@ -692,6 +695,17 @@ public final HttpAsyncClientBuilder setProxy(final HttpHost proxy) {
692695
return this;
693696
}
694697

698+
/**
699+
* Sets exchange ID generator instance.
700+
*
701+
* @return this instance.
702+
* @since 5.7
703+
*/
704+
public final HttpAsyncClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
705+
this.exchangeIdGenerator = exchangeIdGenerator;
706+
return this;
707+
}
708+
695709
/**
696710
* Sets {@link HttpRoutePlanner} instance.
697711
*
@@ -1137,6 +1151,10 @@ public CloseableHttpAsyncClient build() {
11371151
execChainDefinition.addFirst(new TlsRequiredAsyncExec(), ChainElement.TLS_REQUIRED.name());
11381152
}
11391153

1154+
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
1155+
if (exchangeIdGeneratorCopy == null) {
1156+
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
1157+
}
11401158

11411159
HttpRoutePlanner routePlannerCopy = this.routePlanner;
11421160
if (routePlannerCopy == null) {
@@ -1271,6 +1289,7 @@ public CloseableHttpAsyncClient build() {
12711289
pushConsumerRegistry,
12721290
threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-main", true),
12731291
connManagerCopy,
1292+
exchangeIdGeneratorCopy,
12741293
routePlannerCopy,
12751294
tlsConfig,
12761295
cookieSpecRegistryCopy,

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
import org.apache.hc.client5.http.config.RequestConfig;
5151
import org.apache.hc.client5.http.cookie.CookieSpecFactory;
5252
import org.apache.hc.client5.http.cookie.CookieStore;
53-
import org.apache.hc.client5.http.impl.ExecSupport;
5453
import org.apache.hc.client5.http.protocol.HttpClientContext;
5554
import org.apache.hc.client5.http.routing.RoutingSupport;
5655
import org.apache.hc.core5.concurrent.Cancellable;
5756
import org.apache.hc.core5.concurrent.ComplexFuture;
5857
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
5958
import org.apache.hc.core5.concurrent.FutureCallback;
59+
import org.apache.hc.core5.function.Supplier;
6060
import org.apache.hc.core5.http.EntityDetails;
6161
import org.apache.hc.core5.http.HttpException;
6262
import org.apache.hc.core5.http.HttpHost;
@@ -88,6 +88,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
8888
private static final Logger LOG = LoggerFactory.getLogger(InternalAbstractHttpAsyncClient.class);
8989

9090
private final AsyncExecChainElement execChain;
91+
private final Supplier<String> exchangeIdGenerator;
9192
private final Lookup<CookieSpecFactory> cookieSpecRegistry;
9293
private final Lookup<AuthSchemeFactory> authSchemeRegistry;
9394
private final CookieStore cookieStore;
@@ -103,6 +104,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
103104
final AsyncPushConsumerRegistry pushConsumerRegistry,
104105
final ThreadFactory threadFactory,
105106
final AsyncExecChainElement execChain,
107+
final Supplier<String> exchangeIdGenerator,
106108
final Lookup<CookieSpecFactory> cookieSpecRegistry,
107109
final Lookup<AuthSchemeFactory> authSchemeRegistry,
108110
final CookieStore cookieStore,
@@ -112,6 +114,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa
112114
final List<Closeable> closeables) {
113115
super(ioReactor, pushConsumerRegistry, threadFactory);
114116
this.execChain = execChain;
117+
this.exchangeIdGenerator = exchangeIdGenerator;
115118
this.cookieSpecRegistry = cookieSpecRegistry;
116119
this.authSchemeRegistry = authSchemeRegistry;
117120
this.cookieStore = cookieStore;
@@ -232,7 +235,7 @@ protected <T> Future<T> doExecute(
232235
resolvedTarget,
233236
request,
234237
clientContext);
235-
final String exchangeId = ExecSupport.getNextExchangeId();
238+
final String exchangeId = exchangeIdGenerator.get();
236239
clientContext.setExchangeId(exchangeId);
237240
if (LOG.isDebugEnabled()) {
238241
LOG.debug("{} preparing request execution", exchangeId);

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.hc.core5.annotation.Contract;
4444
import org.apache.hc.core5.annotation.Internal;
4545
import org.apache.hc.core5.annotation.ThreadingBehavior;
46+
import org.apache.hc.core5.function.Supplier;
4647
import org.apache.hc.core5.http.HttpException;
4748
import org.apache.hc.core5.http.HttpHost;
4849
import org.apache.hc.core5.http.HttpRequest;
@@ -79,6 +80,7 @@ public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient
7980
final AsyncPushConsumerRegistry pushConsumerRegistry,
8081
final ThreadFactory threadFactory,
8182
final InternalH2ConnPool connPool,
83+
final Supplier<String> exchangeIdGenerator,
8284
final HttpRoutePlanner routePlanner,
8385
final Lookup<CookieSpecFactory> cookieSpecRegistry,
8486
final Lookup<AuthSchemeFactory> authSchemeRegistry,
@@ -87,7 +89,7 @@ public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient
8789
final RequestConfig defaultConfig,
8890
final List<Closeable> closeables,
8991
final int maxQueuedRequests) {
90-
super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
92+
super(ioReactor, pushConsumerRegistry, threadFactory, execChain, exchangeIdGenerator,
9193
cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, HttpClientContext::castOrCreate,
9294
defaultConfig, closeables);
9395
this.connPool = connPool;

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.hc.core5.annotation.Contract;
4747
import org.apache.hc.core5.annotation.Internal;
4848
import org.apache.hc.core5.annotation.ThreadingBehavior;
49+
import org.apache.hc.core5.function.Supplier;
4950
import org.apache.hc.core5.http.HttpException;
5051
import org.apache.hc.core5.http.HttpHost;
5152
import org.apache.hc.core5.http.HttpRequest;
@@ -85,6 +86,7 @@ public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClie
8586
final AsyncPushConsumerRegistry pushConsumerRegistry,
8687
final ThreadFactory threadFactory,
8788
final AsyncClientConnectionManager manager,
89+
final Supplier<String> exchangeIdGenerator,
8890
final HttpRoutePlanner routePlanner,
8991
final TlsConfig tlsConfig,
9092
final Lookup<CookieSpecFactory> cookieSpecRegistry,
@@ -95,7 +97,7 @@ public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClie
9597
final RequestConfig defaultConfig,
9698
final List<Closeable> closeables,
9799
final int maxQueuedRequests) {
98-
super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
100+
super(ioReactor, pushConsumerRegistry, threadFactory, execChain, exchangeIdGenerator,
99101
cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, contextAdaptor,
100102
defaultConfig, closeables);
101103
this.manager = manager;

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
6161
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
6262
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
63+
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
6364
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
6465
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
6566
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
@@ -88,6 +89,7 @@
8889
import org.apache.hc.client5.http.protocol.ResponseProcessCookies;
8990
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
9091
import org.apache.hc.core5.annotation.Internal;
92+
import org.apache.hc.core5.function.Supplier;
9193
import org.apache.hc.core5.http.ConnectionReuseStrategy;
9294
import org.apache.hc.core5.http.Header;
9395
import org.apache.hc.core5.http.HttpEntity;
@@ -191,6 +193,7 @@ private ExecInterceptorEntry(
191193
}
192194

193195
private HttpRequestExecutor requestExec;
196+
private Supplier<String> exchangeIdGenerator;
194197
private HttpClientConnectionManager connManager;
195198
private boolean connManagerShared;
196199
private SchemePortResolver schemePortResolver;
@@ -254,6 +257,17 @@ public final HttpClientBuilder setRequestExecutor(final HttpRequestExecutor requ
254257
return this;
255258
}
256259

260+
/**
261+
* Sets exchange ID generator instance.
262+
*
263+
* @return this instance.
264+
* @since 5.7
265+
*/
266+
public final HttpClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
267+
this.exchangeIdGenerator = exchangeIdGenerator;
268+
return this;
269+
}
270+
257271
/**
258272
* Sets {@link HttpClientConnectionManager} instance.
259273
*
@@ -862,6 +876,10 @@ public CloseableHttpClient build() {
862876
if (requestExecCopy == null) {
863877
requestExecCopy = new HttpRequestExecutor();
864878
}
879+
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
880+
if (exchangeIdGeneratorCopy == null) {
881+
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
882+
}
865883
HttpClientConnectionManager connManagerCopy = this.connManager;
866884
if (connManagerCopy == null) {
867885
final PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
@@ -1133,6 +1151,7 @@ public CloseableHttpClient build() {
11331151
return new InternalHttpClient(
11341152
connManagerCopy,
11351153
requestExecCopy,
1154+
exchangeIdGeneratorCopy,
11361155
execChain,
11371156
routePlannerCopy,
11381157
cookieSpecRegistryCopy,

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.apache.hc.client5.http.config.RequestConfig;
4444
import org.apache.hc.client5.http.cookie.CookieSpecFactory;
4545
import org.apache.hc.client5.http.cookie.CookieStore;
46-
import org.apache.hc.client5.http.impl.ExecSupport;
4746
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
4847
import org.apache.hc.client5.http.protocol.HttpClientContext;
4948
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
@@ -52,6 +51,7 @@
5251
import org.apache.hc.core5.annotation.Internal;
5352
import org.apache.hc.core5.annotation.ThreadingBehavior;
5453
import org.apache.hc.core5.concurrent.CancellableDependency;
54+
import org.apache.hc.core5.function.Supplier;
5555
import org.apache.hc.core5.http.ClassicHttpRequest;
5656
import org.apache.hc.core5.http.ClassicHttpResponse;
5757
import org.apache.hc.core5.http.HttpException;
@@ -85,6 +85,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
8585

8686
private final HttpClientConnectionManager connManager;
8787
private final HttpRequestExecutor requestExecutor;
88+
private final Supplier<String> exchangeIdGenerator;
8889
private final ExecChainElement execChain;
8990
private final HttpRoutePlanner routePlanner;
9091
private final Lookup<CookieSpecFactory> cookieSpecRegistry;
@@ -98,6 +99,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable {
9899
public InternalHttpClient(
99100
final HttpClientConnectionManager connManager,
100101
final HttpRequestExecutor requestExecutor,
102+
final Supplier<String> exchangeIdGenerator,
101103
final ExecChainElement execChain,
102104
final HttpRoutePlanner routePlanner,
103105
final Lookup<CookieSpecFactory> cookieSpecRegistry,
@@ -110,6 +112,7 @@ public InternalHttpClient(
110112
super();
111113
this.connManager = Args.notNull(connManager, "Connection manager");
112114
this.requestExecutor = Args.notNull(requestExecutor, "Request executor");
115+
this.exchangeIdGenerator = Args.notNull(exchangeIdGenerator, "Exchange id generator");
113116
this.execChain = Args.notNull(execChain, "Execution chain");
114117
this.routePlanner = Args.notNull(routePlanner, "Route planner");
115118
this.cookieSpecRegistry = cookieSpecRegistry;
@@ -173,7 +176,7 @@ protected CloseableHttpResponse doExecute(
173176
resolvedTarget,
174177
request,
175178
localcontext);
176-
final String exchangeId = ExecSupport.getNextExchangeId();
179+
final String exchangeId = exchangeIdGenerator.get();
177180
localcontext.setExchangeId(exchangeId);
178181
if (LOG.isDebugEnabled()) {
179182
LOG.debug("{} preparing request execution", exchangeId);

0 commit comments

Comments
 (0)