|
|
@@ -18,6 +18,7 @@ import com.xunmei.host.websocket.redis.delay.RedisDelayedQueueUtil;
|
|
|
import com.xunmei.host.websocket.service.WebsocketService;
|
|
|
import com.xunmei.system.api.util.LogUtils;
|
|
|
import lombok.SneakyThrows;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
@@ -41,11 +42,17 @@ import java.util.List;
|
|
|
public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMapper, IotWebsocketMsg> implements IotWebsocketMsgService, RedisDelayQueueHandle {
|
|
|
|
|
|
@Resource
|
|
|
+ @Lazy
|
|
|
WebsocketService websocketService;
|
|
|
|
|
|
@Resource
|
|
|
IotServerInfoService serverInfoService;
|
|
|
|
|
|
+
|
|
|
+ public static final Integer maxRetryTimes = 3;
|
|
|
+
|
|
|
+ public static final Integer retryInterval = 1;
|
|
|
+
|
|
|
@Override
|
|
|
@Async(ThreadPoolConfig.HOST_EXECUTOR)
|
|
|
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
|
|
|
@@ -61,9 +68,9 @@ public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMappe
|
|
|
msg.setServerName(serverInfo.getIotName());
|
|
|
msg.setStatus(MessageStatusEnum.PROCESSING.getCode());
|
|
|
msg.setReceiveContent(receiveMsg);
|
|
|
- msg.setMaxRetryTimes(3);
|
|
|
+ msg.setMaxRetryTimes(maxRetryTimes);
|
|
|
msg.setCurRetryTimes(0);
|
|
|
- msg.setRetryInterval(10);
|
|
|
+ msg.setRetryInterval(retryInterval);
|
|
|
msg.setCreateTime(LocalDateTime.now());
|
|
|
msg.setUpdateTime(LocalDateTime.now());
|
|
|
save(msg);
|
|
|
@@ -71,7 +78,7 @@ public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMappe
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void sendAndSaveMsg(String iotCode, WebsocketResult res) {
|
|
|
+ public IotWebsocketMsg sendAndSaveMsg(String iotCode, WebsocketResult res) {
|
|
|
//刚刚收到消息,还没开始处理业务逻辑,先保存消息数据
|
|
|
|
|
|
final IotServerInfo serverInfo = serverInfoService.selectByToken(iotCode);
|
|
|
@@ -86,13 +93,14 @@ public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMappe
|
|
|
msg.setStatus(MessageStatusEnum.PROCESSING.getCode());
|
|
|
msg.setReplyContent(JacksonUtils.toJSONString(res));
|
|
|
msg.setReadySendTime(now);
|
|
|
- msg.setMaxRetryTimes(3);
|
|
|
+ msg.setMaxRetryTimes(maxRetryTimes);
|
|
|
msg.setCurRetryTimes(0);
|
|
|
- msg.setRetryInterval(10);
|
|
|
+ msg.setRetryInterval(retryInterval);
|
|
|
msg.setCreateTime(now);
|
|
|
msg.setUpdateTime(now);
|
|
|
save(msg);
|
|
|
LogUtils.WS_MSG_RETRY_LOG.info("开始下发消息到主机,msg: {}", JacksonUtils.toJSONString(msg));
|
|
|
+ return msg;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -147,7 +155,7 @@ public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMappe
|
|
|
return;
|
|
|
}
|
|
|
final LocalDateTime now = LocalDateTime.now();
|
|
|
- //final LocalDateTime firTime = websocketMsg.getFireTime() == null ? now : websocketMsg.getFireTime();
|
|
|
+ final LocalDateTime firTime = websocketMsg.getFireTime() == null ? now : websocketMsg.getFireTime();
|
|
|
final Integer curRetryTimes = websocketMsg.getCurRetryTimes();
|
|
|
if (curRetryTimes == 0) {
|
|
|
//收到消息处理完成后第一次发送,但是发送失败了
|
|
|
@@ -155,8 +163,8 @@ public class IotWebsocketMsgServiceImpl extends ServiceImpl<IotWebsocketMsgMappe
|
|
|
websocketMsg.setReplyContent(JacksonUtils.toJSONString(res));
|
|
|
websocketMsg.setStatus(MessageStatusEnum.RETRYING.getCode());
|
|
|
}
|
|
|
- websocketMsg.setFireTime(now.plusMinutes(websocketMsg.getRetryInterval()));
|
|
|
- //websocketMsg.setFireTime((firTime).plusSeconds(60));
|
|
|
+ //websocketMsg.setFireTime(now.plusMinutes(websocketMsg.getRetryInterval()));
|
|
|
+ websocketMsg.setFireTime((firTime).plusSeconds(30));
|
|
|
websocketMsg.setUpdateTime(now);
|
|
|
updateById(websocketMsg);
|
|
|
RedisDelayedQueueUtil.addDelayQueue(msgId, websocketMsg.getFireTime(), RedisDelayQueueEnum.WEBSOCKET_MSG_RETRY.getCode());
|