序
本文主要研究一下cheddar的PendingResult
PendingResult
Cheddar/cheddar/cheddar-application/src/main/java/com/clicktravel/cheddar/application/pending/result/PendingResult.java
class PendingResult {
private static final long TIMEOUT_SECONDS = 30;
private final CountDownLatch countdownLatch = new CountDownLatch(1);
private Result result;
/**
* Offers a {@link Result}, returning immediately.
* @param result {@link Result} to offer
*/
public void offerResult(final Result result) {
this.result = result;
countdownLatch.countDown();
}
/**
* Polls for a {@link Result}, blocking until it is offered by some other thread. If the result has already been
* offered, the result is returned immediately.
* @return {@link Result} obtained, potentially after blocking
* @throws InterruptedException, PendingResultTimeoutException
*/
public Result pollResult() throws InterruptedException, PendingResultTimeoutException {
if (!countdownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
throw new PendingResultTimeoutException();
}
return result;
}
}
复制代码