Conversation
d8a8f9a to
ac44712
Compare
|
Thank you |
|
Ready for merge? |
Co-authored-by: Zike Yang <zike@apache.org>
Co-authored-by: Zike Yang <zike@apache.org>
|
Any further changes needed? @RobertIndie |
| if (enableAsync){ | ||
| getProducer(topic).newMessage() | ||
| .value(s.getBytes()) | ||
| .sendAsync(); |
There was a problem hiding this comment.
We still need to add a callback to print a log when failing to publish messages.
There was a problem hiding this comment.
This send function is only called inside a try/catch, where it logs "fail to send message" with the exception. See line 225-235.
There was a problem hiding this comment.
It won't catch the exception thrown from sendAsync. We need to use CompletableFuture.exceptionally to catch it. Please see: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#exceptionally-java.util.function.Function-
There was a problem hiding this comment.
Okay.. Am I reading this right?
CompletableFuture.exceptionally description:
Returns a new CompletableFuture that is completed when this CompletableFuture completes, with the result of the given function of the exception triggering this CompletableFuture's completion when it completes exceptionally; otherwise, if this CompletableFuture completes normally, then the returned CompletableFuture also completes normally with the same value.
This doesn't throw a custom Exception that inherits from the class Exception as usual? I don't think I understand what this is describing. I'm trying to decipher it, but does it mean that it returns the function that triggered an Exception rather than actually throwing the Exception. In that case I guess I couldn't try/catch? Or does it mean that I am supposed to just provide a function that is throwable, so when CompletableFuture throws, it triggers my function with where it failed?
There was a problem hiding this comment.
Sorry for the late reply.
The sendAsync will not throw any exceptions. The producer already catches all the exceptions and places them into the Future. You could check the code here.
Therefore if there are any exceptions thrown in the send operation, the function fn in the exceptionally will be called. And I think we need to print the error log in that function.
Or does it mean that I am supposed to just provide a function that is throwable, so when CompletableFuture throws, it triggers my function with where it failed?
That's correct.
However, you still need to use try/catch here, because we couldn't guarantee that this code won't throw the exception:
getProducer(topic).newMessage()
.value(s.getBytes())
.sendAsync();
Based on this comment it seemed wise to make async as a config option.
#25 (comment)