I have tried this way, but I don't know whether it's a good way to solve the problem.
@FunctionalInterface
public interface RiskEngineFuncMessageProcessor<Void> extends Supplier<Void> {
@Override
default Void get() {
try {
return acceptThrows();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Void acceptThrows() throws Exception;
With the FunctionalInterface of Supplier, I can wrap the exception:
final MyFuncProcessor<Void> func = () -> {
processor.process(taskParam);
return null;
};
CompletableFuture<Void> asyncTaskCompletableFuture =
CompletableFuture.supplyAsync(func)
.thenAccept(act -> {
finishTask();
})
.exceptionally(exp -> {
log.error("Failed to consume task", exp);
failTask( exp.getMessage());
return null;
});