-
Notifications
You must be signed in to change notification settings - Fork 983
Add custom ExchangeId generator support #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java
Outdated
Show resolved
Hide resolved
d6326b4 to
5069736
Compare
httpclient5/src/main/java/org/apache/hc/client5/http/ExchangeIdGenerator.java
Outdated
Show resolved
Hide resolved
|
Just for curiosity, how do you plan to use this? |
@rschmitt Thank you for the feedback. I understand your curiosity. // My custom ExchangeId
final String exchangeId = "myexec-001";
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().build();
final SimpleHttpRequest request = new SimpleHttpRequest(Method.GET, URI.create("http://somehost"));
final HttpClientContext context = HttpClientContext.create();
// Sets my custom ExchangeId
context.setExchangeId(exchangeId);
client.execute(request, context, null);Of course, although this approach is simple to use, it is not appropriate. With the current changes, I will pass the custom final class MyRequestProducer implements AsyncRequestProducer {
private final String myExchangeId;
private final HttpRequest httpRequest;
private final AsyncEntityProducer entityProducer;
@Override
public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
// Sets my custom ExchangeId
MyExchangeIdGenerator.set(this.myExchangeId);
channel.sendRequest(this.httpRequest, this.entityProducer, context);
}
}
final class MyExchangeIdGenerator implements Supplier<String> {
static final ThreadLocal<String> THREADLOCAL = new ThreadLocal<>();
static void set(final String exchangeId) {
THREADLOCAL.set(exchangeId);
}
@Override
public String get() {
final String myExchangeId = THREADLOCAL.get();
if (myExchangeId == null) {
return ExecSupport.getNextExchangeId();
}
THREADLOCAL.remove();
return myExchangeId;
}
}final String exchangeId = "myexec-001";
final Supplier<String> exchangeIdGenerator = new MyExchangeIdGenerator();
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().setExchangeIdGenerator(exchangeIdGenerator).build();
final SimpleHttpRequest request = new SimpleHttpRequest(Method.GET, URI.create("http://somehost"));
final MyRequestProducer producer = new MyRequestProducer(exchangeId, request, null);
client.execute(producer, null, null); |
@yhzdys I was actually going to propose that as a simpler alternative, but your current approach is more generic and is fine with me as well @rschmitt Do you have any objections to merging the PR? |
Add ExchangeIdGenerator interface to allow users to customize exchange ID generation in HTTP clients.
This change introduces the ExchangeIdGenerator interface, which enables users to provide custom strategies for generating exchange identifiers. This is particularly useful in distributed systems where custom tracing and correlation mechanisms are required.