KafkaTemplate生产者回调
例子如下:@AutowiredKafkaTemplate<String, String> kafkaTemplate;public void testKafkaTemplate () {ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topic", "{\"key
·
例子如下:
@Autowired
KafkaTemplate<String, String> kafkaTemplate;
public void testKafkaTemplate () {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topic", "{\"key\": \"value\"}");
CompletableFuture<SendResult<String, String>> completable = future.completable();
completable.whenCompleteAsync((n, e) -> {
if (null != e) {
System.out.println("发送报错了");
} else {
System.out.println("发送成功了!");
}
});
}
public void testKafkaTemplate1() {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topic", "{\"key\": \"value\"}");
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onFailure(Throwable throwable) {
//
}
@Override
public void onSuccess(SendResult<String, String> stringStringSendResult) {
//
}
});
}
更多推荐
已为社区贡献5条内容
所有评论(0)