|
@@ -1,68 +1,73 @@
|
|
|
package cn.com.ty.lift.ud.neety.task;
|
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-
|
|
|
import cn.com.ty.lift.ud.chat.mapper.entity.ChatMsgEntity;
|
|
|
-import cn.com.ty.lift.ud.chat.mapper.entity.ChatUnreadMsgEntity;
|
|
|
import cn.com.ty.lift.ud.chat.service.IChatMsgService;
|
|
|
import cn.com.ty.lift.ud.chat.service.IChatUnreadMsgService;
|
|
|
+import cn.com.ty.lift.ud.neety.protocol.IMMessage;
|
|
|
import cn.com.ty.lift.ud.neety.protocol.RoomVo;
|
|
|
import cn.com.ty.lift.ud.redis.ChatDo;
|
|
|
import cn.com.ty.lift.ud.redis.RedisUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 同步缓存数据到mysql
|
|
|
- *
|
|
|
- * @author sKing
|
|
|
*
|
|
|
+ * @author sKing
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class RedisTask {
|
|
|
-
|
|
|
- private @Autowired RedisUtil redisUtil;
|
|
|
- private @Autowired IChatUnreadMsgService chatUnreadMsgService;
|
|
|
- private @Autowired IChatMsgService msgService;
|
|
|
-
|
|
|
- private static RedisUtil redis;
|
|
|
- private static IChatUnreadMsgService unreadMsgService;
|
|
|
- private static IChatMsgService iChatMsgService;
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void initBean() {
|
|
|
- redis = redisUtil;
|
|
|
- unreadMsgService = chatUnreadMsgService;
|
|
|
- iChatMsgService = msgService;
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redis;
|
|
|
+ @Autowired
|
|
|
+ private IChatUnreadMsgService chatUnreadMsgService;
|
|
|
+ @Autowired
|
|
|
+ private IChatMsgService msgService;
|
|
|
|
|
|
public RedisTask() {
|
|
|
log.info("RedisTask init");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 每5分钟执行一次 同步redis缓存中未读消息的数据到mysql中
|
|
|
+ * 在凌晨时,讲聊天记录同步到数据库中,redis只放当天的数据
|
|
|
*/
|
|
|
-// @Scheduled(cron = "0 */5 * * * ?")
|
|
|
- public void updateMemberLevel() {
|
|
|
- // RedisUtil redis = ApplicationContextUtil.getBean(RedisUtil.class);
|
|
|
- // IChatUnreadMsgService unreadMsgService = ApplicationContextUtil.getBean(ChatUnreadMsgServiceImpl.class);
|
|
|
+ @Scheduled(cron = "0 0 1 * * ?")
|
|
|
+// @Scheduled(initialDelay = 5000, fixedDelay=3600000)
|
|
|
+ public void synRedisChatToDB() {
|
|
|
log.info("执行保存未读消息操作!");
|
|
|
- List<ChatUnreadMsgEntity> unreadMsgList = new ArrayList<>();
|
|
|
+ Set<String> keys = redis.keys("ALLCHATS:*");
|
|
|
+ for (String key : keys) {
|
|
|
+ List<ChatMsgEntity> chatList = new ArrayList<>();
|
|
|
+ List list = redis.get(key, List.class);
|
|
|
+ list.forEach(msg -> {
|
|
|
+ ChatMsgEntity entity = new ChatMsgEntity();
|
|
|
+ IMMessage imMessage = JSONObject.parseObject(msg.toString(), IMMessage.class);
|
|
|
+ entity.setMsg(imMessage.getMsg());
|
|
|
+ entity.setFromUser(imMessage.getFromUser());
|
|
|
+ entity.setToUser(imMessage.getReceiver());
|
|
|
+ entity.setType(imMessage.getType());
|
|
|
+ entity.setSessionid(imMessage.getSessionid());
|
|
|
+ entity.setStatuz(1);
|
|
|
+ entity.setCreateBy(imMessage.getName());
|
|
|
+ entity.setCreateTime(new Date(imMessage.getTime()).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime());
|
|
|
+ chatList.add(entity);
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!chatList.isEmpty()) {
|
|
|
+ msgService.saveBatch(chatList);
|
|
|
+ }
|
|
|
+ redis.delete(key);
|
|
|
+ }
|
|
|
+ /*List<ChatUnreadMsgEntity> unreadMsgList = new ArrayList<>();
|
|
|
Map<String, List<RoomVo>> redisRoom = redis.get("ROOM", Map.class);
|
|
|
ChatUnreadMsgEntity entity = new ChatUnreadMsgEntity();
|
|
|
// 获取所有房间的聊天记录
|
|
@@ -93,7 +98,7 @@ public class RedisTask {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- /** TODO 打开之后将清理redis中未读的缓存 暂时未用(使用之后用户上线查询未读消息需要结合缓存以及数据库的数据) */
|
|
|
+ *//** TODO 打开之后将清理redis中未读的缓存 暂时未用(使用之后用户上线查询未读消息需要结合缓存以及数据库的数据) *//*
|
|
|
// redisRoom.put(room, voList);
|
|
|
}
|
|
|
|
|
@@ -101,8 +106,8 @@ public class RedisTask {
|
|
|
// 重置缓存
|
|
|
redis.setValue("ROOM", redisRoom);
|
|
|
|
|
|
- unreadMsgService.saveBatch(unreadMsgList);
|
|
|
- }
|
|
|
+ chatUnreadMsgService.saveBatch(unreadMsgList);
|
|
|
+ }*/
|
|
|
log.info("End. 保存未读消息操作结束!");
|
|
|
|
|
|
}
|
|
@@ -141,7 +146,7 @@ public class RedisTask {
|
|
|
// 删除缓存中已提取的房间号
|
|
|
redis.delete(key);
|
|
|
|
|
|
- iChatMsgService.saveBatch(msg);
|
|
|
+ msgService.saveBatch(msg);
|
|
|
}
|
|
|
log.info("End. 保存聊天记录操作结束!");
|
|
|
}
|