如何在Future#get()上处理InterruptException


public void produceMessage(String dataPushNotif) {
PushNotif msg = PushNotif.newBuilder()
.setDatapushnotif(dataPushNotif)
.build();
ListenableFuture<SendResult<String, PushNotif>> future =
kafkaTemplate.send(destTopic, msg);
try {
var result = future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
log.debug(buildSuccesLogMessage(result));
}
catch (InterruptedException | ExecutionException | TimeoutException e) {
log.debug("FAILED: {}", e);
}
}

通过上面的代码,我得到了一个sonarlint警告要么重新中断这个方法,要么重新抛出";中断异常";可以在这里找到(java:S2142(

我可以用addCallback((优雅地处理发送的成功和失败,但我看不出有任何其他方法可以设置线程执行的超时。

有什么办法处理sonarlint警告吗?或者用超时选项优雅地处理未来?

不要在InterruptedExceptioncatch块中使用multi-catch并调用Thread.currentThread().interrupt(),这样下一个可中断操作将获得异常。

";燕子;中断。

最新更新