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?
CodePudding user response:
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.
CodePudding user response:
Assuming you are referring to CompletableFuture
.
CompletableFuture
provides three methods to handle the result: handle()
, whenComplete()
and exceptionally()
.
For example, you can use handle()
to handle the result and exception, by either recovering from exception or returning the result RecordMetaData
directly:
CompletableFuture<String> cf1 =
cf0.handle((record, ex) -> {
if (ex != null) {
return ex.getMessage();
} else {
return record;
}
});
whenComplete()
may be a better choice depending on your situation.