내 프로젝트에서 Guava의 ListeningExecutorService를 사용했지만 예외 처리에 대해 혼란스러워했습니다.ListeningExecutorService에서 예외를 throw하는 방법은 무엇입니까?
스레드 풀을 사용하여 스레드 풀을 제출하고 listenableFuture에 시간 제한을 설정하고 콜백을 추가했습니다.
final ListeningExecutorService threadPool = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
Futures.addCallback(listenableFuture, new FutureCallback<**>() {
@Override
public void onSuccess(@Nullable ** data) {
xxxxxxx;
}
public void onFailure(Throwable t) {
xxxxxxxxxx;
if (t instanceof CancellationException) {
throw new QueryException("yyyy");
} else {
throw new QueryException("zzzzz");
}
}
});
콜백 내부에서 예외를 catch 할 수 없습니다. 그래서 예외
ListenableFuture allFutures = Futures.allAsList(allFuturesList);
try {
allFutures.get();
} catch (CancellationException ce) {
throw new QueryException("");
} catch (InterruptedException t) {
throw new QueryException("");
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof QueryException)
throw (QueryException) t;
else
throw new QueryException();
} catch (QueryException qe) {
throw qe;
} catch (Exception e) {
throw new QueryException();
} finally {
}
을 얻기 위해 다른 ListenableFuture를 사용하지만 콜백은 allFutures에만 자세한 오류 메시지없이 CancellationException를 잡을 수 있고, allFutures 그것을 잡을 수 없어하는 QueryException을 던질 때.
세부 정보 오류 메시지는 어떻게받을 수 있습니까?
미래가 실패하고 특정 실행 컨텍스트가 사용자에게 표시 될 때 전체 스레드 풀을 종료하려고합니다. ListeningExecutorService로 할 수 있습니까? – lulijun
@lulijun 코드를 비동기 적으로 실행 한 다음 모든 결과 (실패 했든 성공 했든)를 고객에게 제시 할 수있는 하나의 객체로 집계하려고합니다. –
예, 스레드가 실패하면 집계 된 결과를 표시하지 않고 세부 오류 메시지를 표시합니다. – lulijun