Java如何使用ReentrantLock實現(xiàn)長輪詢
Java代碼
1. ReentrantLock
加鎖阻塞,一個condition對應(yīng)一個線程,以便于喚醒時使用該condition一定會喚醒該線程
/**
* 獲取探測點數(shù)據(jù),長輪詢實現(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) {
// 等待超時, do nothing
} finally {
lock.unlock();
}
}
// 再次嘗試獲取
message = toutMessageCache.get(messageId);
if (message == null) {
// 如果還沒有, 返回空對象
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)用時,判斷返回的值是否為空,如果為空,與前端約定,當(dāng)返回該狀態(tài)值時,應(yīng)再次發(fā)起相同請求
/**
* 獲取探測數(shù)據(jù)(使用長輪詢實現(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.前端實現(xiàn)
簡單使用遞歸實現(xiàn)了當(dāng)數(shù)據(jù)返回?zé)o效時再次發(fā)起請求
let that = this
function 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實現(xiàn)長輪詢的詳細內(nèi)容,更多關(guān)于ReentrantLock長輪詢的資料請關(guān)注腳本之家其它相關(guān)文章!
- java?常規(guī)輪詢長輪詢Long?polling實現(xiàn)示例詳解
- Java實現(xiàn)一個簡單的長輪詢的示例代碼
- Java servlet通過事件驅(qū)動進行高性能長輪詢詳解
- Java?輪詢鎖使用時遇到問題解決方案
- Java?死鎖解決方案順序鎖和輪詢鎖
- Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解
- Java負載均衡算法實現(xiàn)之輪詢和加權(quán)輪詢
- Java 利用DeferredResult實現(xiàn)http輪詢實時返回數(shù)據(jù)接口
- 基于Rxjava實現(xiàn)輪詢定時器
- 告別無盡等待:Java中的輪詢終止技巧
相關(guān)文章
IDEA 當(dāng)前在線人數(shù)和歷史訪問量的示例代碼
這篇文章主要介紹了IDEA 當(dāng)前在線人數(shù)和歷史訪問量的實例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
springboot集成mybatis-maven插件自動生成pojo的詳細教程
這篇文章主要介紹了springboot集成mybatis-maven插件自動生成pojo的詳細教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
java計算任意位水仙花數(shù)示例(回文數(shù))
這篇文章主要介紹了java計算任意位水仙花數(shù)示例(回文數(shù)),需要的朋友可以參考下2014-05-05
SpringBoot配置多數(shù)據(jù)源的四種方式分享
在日常開發(fā)中我們都是以單個數(shù)據(jù)庫進行開發(fā),在小型項目中是完全能夠滿足需求的,但是,當(dāng)我們牽扯到大型項目的時候,單個數(shù)據(jù)庫就難以承受用戶的CRUD操作,那么此時,我們就需要使用多個數(shù)據(jù)源進行讀寫分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式2023-07-07

