ReentrantLock实现的长轮询
Java代码
ReentrantLock
/**
* 获取探测点数据,长轮询实现
* @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);
} 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;
}
回调
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);
}
}
}
唤醒
public void distribute(Message message, ChannelHandlerContext ctx) {
MessageType messageType = message.getMessageType();
switch (messageType) {
case TOUT_DATA_RESPONSE:
// 探点数据响应
toutService.callback(message);
break;
}
}
前端实现
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()
}
})
}
}
// 递归循环调用
getData()