allthingssecurity commented on code in PR #27761:
URL: https://github.com/apache/camel/pull/27761#discussion_r4256024251
##########
components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramMessageHandler.java:
##########
@@ -63,18 +64,26 @@ public void sendMessage(Exchange exchange, AsyncCallback
callback, T message) {
builder.setHeader("Accept", "application/json");
builder.POST(bodyPublisher.newPublisher());
- try {
- final TelegramAsyncHandler telegramAsyncHandler
- = new TelegramAsyncHandler(uri, resultClass, mapper,
exchange, callback);
+ final TelegramAsyncHandler telegramAsyncHandler = new
TelegramAsyncHandler(uri, resultClass, mapper, exchange);
+ final CompletableFuture<Object> response
+ = apiClient.client().sendAsync(builder.build(),
HttpResponse.BodyHandlers.ofInputStream())
+
.thenApply(telegramAsyncHandler::handleCompressedResponse);
- apiClient.client().sendAsync(builder.build(),
HttpResponse.BodyHandlers.ofInputStream())
-
.thenApply(telegramAsyncHandler::handleCompressedResponse).get();
+ try {
+ // the calling thread waits for the answer, so a caller sends one
message after the other
+ response.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
- throw new RuntimeException(e);
+ // nobody waits for the answer any more: keep the stage from
writing to the completed exchange later
+ response.cancel(true);
Review Comment:
I checked this on JDK 17.0.19 and 21.0.11 with a server that reads the
request and never answers. The futures of the
JDK client are its own subclass, also the `thenApply` stage, and
`cancel(true)` on that stage closes the connection,
the same as on the `sendAsync` future. A plain `CompletableFuture` behaves
as you describe, and a custom `client` may
return one. So cancelling the `sendAsync` future as well, as you suggest, is
the safer code. The PR was merged before
I pushed it; it will be handled in a follow-up PR, with a test for both
cases.
_Claude Code on behalf of allthingssecurity_
--
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]