When calling the Kafka Producer, is there a way to verify if the result is success or failure? I get back a Future<RecordMetaData>
but I do not know how to verify if it is successful or not successful. Or is it the fact that if this is returned, then the call is successful, if it failed then there would be an error thrown?
Asked
Active
Viewed 1,074 times
-2

stackerstack
- 243
- 4
- 16
-
Unfortunately, there’s a zoo of futures in Java. Which one are you referring to? Post a link to the class Javadoc. – Abhijit Sarkar Sep 24 '21 at 22:14
-
*Or is it the fact that if this is returned, then the call is successful,* The operation may not even have happened yet. The Future is a handle on the future success or failure of the operation. – user16632363 Sep 25 '21 at 01:01
1 Answers
0
The Future is a handle on the future successful or failed completion of the operation.
If for example you call `get()' on the Future object, then if it returns, you'll have a RecordMetaData. Otherwise, it'll throw an ExecutionException, whose cause will tell you the actual failure.
See the documentation.

user16632363
- 1,050
- 3
- 6
-
So the get() on the Future object either returns something if successful or throws an ExecutionException if the process was unsuccessful? – stackerstack Sep 27 '21 at 02:33
-
Yes. (After suspending the calling thread to wait for completion, if necessary). – user16632363 Sep 27 '21 at 02:34
-
This is for publishing kafka logs in a springboot app, so it'll be live and users would hit the endpoint to publish. Assuming this, the logic you stated is still valid correct? – stackerstack Sep 27 '21 at 03:33
-
I don't really understand your use-case, but clearly the Kafka Producer is async to caller, since it returns a Future. If you in turn have an endpoint to your users, such that the operation needs to be complete before you respond to a user request, then yes, you have to wait for the Future to be completed. – user16632363 Sep 27 '21 at 11:38