-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add async as config option #31
base: master
Are you sure you want to change the base?
Add async as config option #31
Conversation
d8a8f9a
to
ac44712
Compare
Thank you |
Ready for merge? |
Co-authored-by: Zike Yang <[email protected]>
Co-authored-by: Zike Yang <[email protected]>
Any further changes needed? @RobertIndie |
if (enableAsync){ | ||
getProducer(topic).newMessage() | ||
.value(s.getBytes()) | ||
.sendAsync(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need to add a callback to print a log when failing to publish messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)