|
@@ -1,5 +1,7 @@
|
|
|
package com.nb.im.room;
|
|
package com.nb.im.room;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateUnit;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.nb.im.entity.ImRoomUserEntity;
|
|
import com.nb.im.entity.ImRoomUserEntity;
|
|
|
import com.nb.im.service.LocalImRoomService;
|
|
import com.nb.im.service.LocalImRoomService;
|
|
@@ -8,12 +10,11 @@ import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
-import java.lang.ref.Reference;
|
|
|
|
|
-import java.lang.ref.WeakReference;
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.Iterator;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.WeakHashMap;
|
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -26,6 +27,7 @@ import java.util.stream.Collectors;
|
|
|
@Component
|
|
@Component
|
|
|
public class ImRoomOperatorManager {
|
|
public class ImRoomOperatorManager {
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private final Map<String,ImRoomOperator> imRoomOperatorMap=new HashMap<>();
|
|
private final Map<String,ImRoomOperator> imRoomOperatorMap=new HashMap<>();
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -36,6 +38,35 @@ public class ImRoomOperatorManager {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private LocalImRoomUserService roomUserService;
|
|
private LocalImRoomUserService roomUserService;
|
|
|
|
|
+
|
|
|
|
|
+ private final ScheduledExecutorService singleThreadEventExecutor = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
|
+ public ImRoomOperatorManager(RedissonClient redissonClient, LocalImRoomService roomService, LocalImRoomUserService roomUserService) {
|
|
|
|
|
+ this.redissonClient = redissonClient;
|
|
|
|
|
+ this.roomService = roomService;
|
|
|
|
|
+ this.roomUserService = roomUserService;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostConstruct
|
|
|
|
|
+ public void init(){
|
|
|
|
|
+ //定时清除map中的过期房间
|
|
|
|
|
+ singleThreadEventExecutor.scheduleAtFixedRate(()->{
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ imRoomOperatorMap.values()
|
|
|
|
|
+ .removeIf(roomOperator -> {
|
|
|
|
|
+ Date startTime = roomOperator.getStartTime();
|
|
|
|
|
+ long betweenHours = DateUtil.between(startTime, now, DateUnit.HOUR);
|
|
|
|
|
+ if(betweenHours>48){
|
|
|
|
|
+ //超过48小时强制删除
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }else if(betweenHours>24&&roomOperator.isOnlineDoctor()){
|
|
|
|
|
+ //超过24小时且医生不在线删除
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ });
|
|
|
|
|
+ },24,24, TimeUnit.HOURS);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public ImRoomOperator getRoomOperator(String roomId){
|
|
public ImRoomOperator getRoomOperator(String roomId){
|
|
|
return imRoomOperatorMap.computeIfAbsent(new String(roomId),k->
|
|
return imRoomOperatorMap.computeIfAbsent(new String(roomId),k->
|
|
|
new ImRoomOperator(roomId,
|
|
new ImRoomOperator(roomId,
|