Skip to content

Commit 47358db

Browse files
author
yhzdys
committed
Add custom ExchangeId generator support
1 parent 3159813 commit 47358db

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
@@ -59,6 +59,7 @@
5959
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
6060
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
6161
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
62+
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
6263
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
6364
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
6465
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
@@ -91,6 +92,7 @@
9192
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
9293
import org.apache.hc.core5.function.Callback;
9394
import org.apache.hc.core5.function.Decorator;
95+
import org.apache.hc.core5.function.Supplier;
9496
import org.apache.hc.core5.http.ConnectionReuseStrategy;
9597
import org.apache.hc.core5.http.Header;
9698
import org.apache.hc.core5.http.HttpHost;
@@ -233,6 +235,7 @@ private ExecInterceptorEntry(
233235
private LinkedList<ResponseInterceptorEntry> responseInterceptors;
234236
private LinkedList<ExecInterceptorEntry> execInterceptors;
235237

238+
private Supplier<String> exchangeIdGenerator;
236239
private HttpRoutePlanner routePlanner;
237240
private RedirectStrategy redirectStrategy;
238241
private HttpRequestRetryStrategy retryStrategy;
@@ -695,6 +698,17 @@ public final HttpAsyncClientBuilder setProxy(final HttpHost proxy) {
695698
return this;
696699
}
697700

701+
/**
702+
* Sets exchange ID generator instance.
703+
*
704+
* @return this instance.
705+
* @since 5.7
706+
*/
707+
public final HttpAsyncClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
708+
this.exchangeIdGenerator = exchangeIdGenerator;
709+
return this;
710+
}
711+
698712
/**
699713
* Sets {@link HttpRoutePlanner} instance.
700714
*
@@ -1140,6 +1154,10 @@ public CloseableHttpAsyncClient build() {
11401154
execChainDefinition.addFirst(new TlsRequiredAsyncExec(), ChainElement.TLS_REQUIRED.name());
11411155
}
11421156

1157+
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
1158+
if (exchangeIdGeneratorCopy == null) {
1159+
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
1160+
}
11431161

11441162
HttpRoutePlanner routePlannerCopy = this.routePlanner;
11451163
if (routePlannerCopy == null) {
@@ -1277,6 +1295,7 @@ public CloseableHttpAsyncClient build() {
12771295
pushConsumerRegistry,
12781296
threadFactory != null ? threadFactory : new DefaultThreadFactory("httpclient-main", true),
12791297
connManagerCopy,
1298+
exchangeIdGeneratorCopy,
12801299
routePlannerCopy,
12811300
tlsConfig,
12821301
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
@@ -61,6 +61,7 @@
6161
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
6262
import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
6363
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
64+
import org.apache.hc.client5.http.impl.DefaultExchangeIdGenerator;
6465
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
6566
import org.apache.hc.client5.http.impl.DefaultRedirectStrategy;
6667
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
@@ -89,6 +90,7 @@
8990
import org.apache.hc.client5.http.protocol.ResponseProcessCookies;
9091
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
9192
import org.apache.hc.core5.annotation.Internal;
93+
import org.apache.hc.core5.function.Supplier;
9294
import org.apache.hc.core5.http.ConnectionReuseStrategy;
9395
import org.apache.hc.core5.http.Header;
9496
import org.apache.hc.core5.http.HttpEntity;
@@ -194,6 +196,7 @@ private ExecInterceptorEntry(
194196
}
195197

196198
private HttpRequestExecutor requestExec;
199+
private Supplier<String> exchangeIdGenerator;
197200
private HttpClientConnectionManager connManager;
198201
private boolean connManagerShared;
199202
private SchemePortResolver schemePortResolver;
@@ -257,6 +260,17 @@ public final HttpClientBuilder setRequestExecutor(final HttpRequestExecutor requ
257260
return this;
258261
}
259262

263+
/**
264+
* Sets exchange ID generator instance.
265+
*
266+
* @return this instance.
267+
* @since 5.7
268+
*/
269+
public final HttpClientBuilder setExchangeIdGenerator(final Supplier<String> exchangeIdGenerator) {
270+
this.exchangeIdGenerator = exchangeIdGenerator;
271+
return this;
272+
}
273+
260274
/**
261275
* Sets {@link HttpClientConnectionManager} instance.
262276
*
@@ -865,6 +879,10 @@ public CloseableHttpClient build() {
865879
if (requestExecCopy == null) {
866880
requestExecCopy = new HttpRequestExecutor();
867881
}
882+
Supplier<String> exchangeIdGeneratorCopy = this.exchangeIdGenerator;
883+
if (exchangeIdGeneratorCopy == null) {
884+
exchangeIdGeneratorCopy = DefaultExchangeIdGenerator.INSTANCE;
885+
}
868886
HttpClientConnectionManager connManagerCopy = this.connManager;
869887
if (connManagerCopy == null) {
870888
final PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
@@ -1139,6 +1157,7 @@ public CloseableHttpClient build() {
11391157
return new InternalHttpClient(
11401158
connManagerCopy,
11411159
requestExecCopy,
1160+
exchangeIdGeneratorCopy,
11421161
execChain,
11431162
routePlannerCopy,
11441163
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)