Java如何使用ReentrantLock實(shí)現(xiàn)長(zhǎng)輪詢
加鎖阻塞,一個(gè)condition對(duì)應(yīng)一個(gè)線程,以便于喚醒時(shí)使用該condition一定會(huì)喚醒該線程
/** * 獲取探測(cè)點(diǎn)數(shù)據(jù),長(zhǎng)輪詢實(shí)現(xiàn) * @param messageId * @return */ public JSONObject getToutData(String messageId) {Message message = toutMessageCache.get(messageId);if (message == null) { // 等待 lock.lock(); try {Condition condition = lock.newCondition();conditionMap.put(messageId + '_data', condition);condition.await(CONNECTION_HOLD_TIMEOUT, TimeUnit.SECONDS); // 等待60s } catch (InterruptedException e) {// 等待超時(shí), do nothing } finally {lock.unlock(); }}// 再次嘗試獲取message = toutMessageCache.get(messageId);if (message == null) { // 如果還沒(méi)有, 返回空對(duì)象 return null;}byte[] bytes = message.getDataBytes();if (bytes == null) { return null;}String resStr = new String(bytes, StandardCharsets.UTF_8);//log.info('resStr: {}', resStr);JSONObject resObj;try { resObj = new JSONObject(resStr); resObj.put('invokeTime', DateUtil.format(new Date(resObj.getLong('invokeTime')), DatePattern.NORM_DATETIME_MS_PATTERN));} catch (Exception e) { resObj = new JSONObject();}return resObj; }2. 回調(diào)
當(dāng)異步數(shù)據(jù)返回,使用上一步的condition喚醒線程
public void callback(Message message) { String messageId = message.getId(); toutMessageCache.put(message.getId(), message); String messageDataId = messageId + '_data'; if (conditionMap.containsKey(messageDataId)) {lock.lock();try { Condition condition = conditionMap.get(messageDataId); condition.signal();} catch (Exception e) { e.printStackTrace();} finally { lock.unlock(); conditionMap.remove(messageDataId);} }}3. 喚醒
執(zhí)行回調(diào)操作
public void distribute(Message message, ChannelHandlerContext ctx) { MessageType messageType = message.getMessageType(); switch (messageType) { case TOUT_DATA_RESPONSE: // 數(shù)據(jù)響應(yīng) toutService.callback(message); break; }}4. 調(diào)用
調(diào)用時(shí),判斷返回的值是否為空,如果為空,與前端約定,當(dāng)返回該狀態(tài)值時(shí),應(yīng)再次發(fā)起相同請(qǐng)求
/*** 獲取探測(cè)數(shù)據(jù)(使用長(zhǎng)輪詢實(shí)現(xiàn))* @param linkId* @return*/@GetMapping('/data')public ResultVO getToutData(String linkId) { JSONObject resObj = toutService.getToutData(linkId); if (resObj == null || resObj.isEmpty()) { return ResultVOUtil.error(ResultEnum.NO_MESSAGE_HOLD_CONNECTION); } return ResultVOUtil.success(resObj);}5.前端實(shí)現(xiàn)
簡(jiǎn)單使用遞歸實(shí)現(xiàn)了當(dāng)數(shù)據(jù)返回?zé)o效時(shí)再次發(fā)起請(qǐng)求
let that = thisfunction getData() { if (toutStatus === statusEnum.start) { getToutData({ linkId }).then(res => { if (res.code === ERROR_CODE_OK) { that.toutData = res.data toutStatus = statusEnum.resData that._btnStatus() } else { getData() } }) } } // 遞歸循環(huán)調(diào)用 getData()
以上就是如何使用ReentrantLock實(shí)現(xiàn)長(zhǎng)輪詢的詳細(xì)內(nèi)容,更多關(guān)于ReentrantLock長(zhǎng)輪詢的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. java實(shí)現(xiàn)圖形化界面計(jì)算器2. IntelliJ Idea2017如何修改緩存文件的路徑3. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟4. IIS Express 取代 ASP.NET Development Server的配置方法5. python flask框架快速入門(mén)6. Spring-Richclient 0.1.0 發(fā)布7. javascript設(shè)計(jì)模式 ? 建造者模式原理與應(yīng)用實(shí)例分析8. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式9. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考10. Express 框架中使用 EJS 模板引擎并結(jié)合 silly-datetime 庫(kù)進(jìn)行日期格式化的實(shí)現(xiàn)方法
