本文主要研究一下cheddar的PendingResult
PendingResult
Cheddar/cheddar/cheddar-application/src/main/java/com/clicktravel/cheddar/application/pending/result/PendingResult.java
classPendingResult{
privatestaticfinallongTIMEOUT_SECONDS=30;
privatefinalCountDownLatchcountdownLatch=newCountDownLatch(1);
privateResultresult;
/**
*Offersa{@linkResult},returningimmediately.
*@paramresult{@linkResult}tooffer
*/
publicvoidofferResult(finalResultresult){
this.result=result;
countdownLatch.countDown();
}
/**
*Pollsfora{@linkResult},blockinguntilitisofferedbysomeotherthread.Iftheresulthasalreadybeen
*offered,theresultisreturnedimmediately.
*@return{@linkResult}obtained,potentiallyafterblocking
*@throwsInterruptedexception,PendingResultTimeoutException
*/
publicResultpollResult()throwsInterruptedException,PendingResultTimeoutException{
if(!countdownLatch.await(TIMEOUT_SECONDS,TimeUnit.SECONDS)){
thrownewPendingResultTimeoutException();
}
returnresult;
}
}
PendingResult定义了offerResult、pollResult方法;offerResult方法设置result,然后通过countdownLatch通过pollResult;pollResult方法等待TIMEOUT_SECONDS,若还没有结果抛出PendingResultTimeoutException
PendingResultHandlerCheddar/cheddar/cheddar-application/src/main/java/com/clicktravel/cheddar/application/pending/result/PendingResultHandler.java
@Component publicclassPendingResultHandler{ privatefinalPendingResultsHolderpendingResultsHolder; privatefinalStringapplicationName; privatefinalXStreamxStream=newXStream(); @Autowired publicPendingResultHandler(finalPendingResultsHolderpendingResultsHolder, @Value("${server.application.name}")finalStringapplicationName){ this.pendingResultsHolder=pendingResultsHolder; this.applicationName=applicationName; } /** *Createsa{@linkPendingResult}storedonthelocalapplicationinstance *@returnpendingResultIdUniqueidentifierofcreated{@PendingResult} */ publicStringcreatePendingResult(){ returnpendingResultsHolder.create(); } /** *Removesa{@linkPendingResult}storedonthelocalapplicationinstance *@parampendingResultIdIDof{@linkPendingResult}toremove */ publicvoidremovePendingResult(finalStringpendingResultId){ pendingResultsHolder.remove(pendingResultId); } /** *Pollsa{@linkPendingResult}storedonthelocalapplicationinstanceforareturnedvalue,blockinguntila *valueisofferedoranExceptionisthrown *@parampendingResultIdIDoflocallystored{@linkPendingResult} *@returnValuefromreturnedresult *@throwsExceptionIfexceptionwasthrown */ publicObjectpollValue(finalStringpendingResultId)throwsException{ finalPendingResultpendingResult=pendingResultsHolder.get(pendingResultId); returnpendingResult.pollResult().getValue(); } /** *Offersareturnvaluefora{@linkPendingResult},whichispossiblystoredonadifferent(remote)application *instance *@parampendingResultIdIDof{@linkPendingResult}tooffervalueto *@paramvalueValuetooffer */ publicvoidofferValue(finalStringpendingResultId,finalObjectvalue){ offerResult(pendingResultId,newSimpleResult(value)); } /** *Offersathrownexceptionfora{@linkPendingResult},whichispossiblystoredonadifferent(remote) *applicationinstance *@parampendingResultIdof{@linkPendingResult}toofferthrownexceptionto *@paramexceptionThrownexceptiontooffer */ publicvoidofferException(finalStringpendingResultId,finalExceptionexception){ offerResult(pendingResultId,newExceptionResult(exception)); } privatevoidofferResult(finalStringpendingResultId,finalResultresult){ finalPendingResultOfferedEventevent=newPendingResultOfferedEvent(); event.setTargetApplicationName(applicationName); event.setPendingResultId(pendingResultId); event.setResultXml(toCompactXml(result)); SystemEventPublisher.instance().publishEvent(event); } privateStringtoCompactXml(finalObjectobject){ finalStringWriterstringWriter=newStringWriter(); xStream.marshal(object,newCompactWriter(stringWriter)); returnstringWriter.toString(); } }
PendingResultHandler提供了removePendingResult、pollValue、offerValue、offerException方法;offerResult方法创建并发布PendingResultOfferedEvent
小结cheddar的PendingResult定义了offerResult、pollResult方法;PendingResultHandler提供了removePendingResult、pollValue、offerValue、offerException方法。
doc,
- Cheddar