yhzdys commented on PR #788:
URL: 
https://github.com/apache/httpcomponents-client/pull/788#issuecomment-3764876885

   > Just for curiosity, how do you plan to use this?
   
   @rschmitt Thank you for the feedback. I understand your curiosity. 
   In my initial PR changes, I planned to implement custom `exchangeId` by 
setting it in advance, like this:
   
   ```java
   // 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 `exchangeId` through a 
ThreadLocal, as shown below:
   Although there is some performance overhead, it is still acceptable for my 
use case.
   
   ```java
   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;
       }
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to