java concurrent Future任务在任何异常的情况下返回null并且不传播它

jun*_*sal 2 java concurrency multithreading spring-batch

我是java.util.concurrent包的新手,并编写了一个从DB中获取一些行的简单方法.我确保我的数据库调用会抛出异常来处理它.但我没有看到异常传播给我.而是调用我的方法返回null.

在这种情况下,有人可以帮助我吗?这是我的示例方法调用

private FutureTask<List<ConditionFact>> getConditionFacts(final Member member) throws Exception {
        FutureTask<List<ConditionFact>> task = new FutureTask<List<ConditionFact>>(new Callable<List<ConditionFact>>() {
            public List<ConditionFact> call() throws Exception {
                return saeFactDao.findConditionFactsByMember(member);
            }
        });
        taskExecutor.execute(task);
        return task;
    }
Run Code Online (Sandbox Code Playgroud)

我用Google搜索并找到了一些页面.但是没有看到任何具体的解决方案.专家请帮帮....

taskExecutor是org.springframework.core.task.TaskExecutor的对象

JB *_*zet 9

FutureTask将在新线程中执行,如果发生异常,则将其存储在实例字段中.只有当你要求执行结果时才会得到异常,包含在ExecutionException:

FutureTask<List<ConditionFact>> task = getConditionFacts(member);
// wait for the task to complete and get the result:
try {
    List<ConditionFact> conditionFacts = task.get();
}
catch (ExecutionException e) {
    // an exception occurred.
    Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
Run Code Online (Sandbox Code Playgroud)