Browse Source

性能优化

master
xsx 2 years ago
parent
commit
5273ff21ab
  1. 36
      im-client/src/main/java/com/bx/imclient/sender/IMSender.java
  2. 2
      im-platform/src/main/java/com/bx/implatform/service/impl/GroupServiceImpl.java
  3. 7
      im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java

36
im-client/src/main/java/com/bx/imclient/sender/IMSender.java

@ -12,10 +12,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Service
@ -81,22 +78,29 @@ public class IMSender {
public<T> void sendGroupMessage(IMGroupMessage<T> message) {
// 根据群聊每个成员所连的IM-server,进行分组
List<IMUserInfo> offLineUsers = Collections.synchronizedList(new LinkedList<>());
// 格式:map<服务器id,list<接收方>>
Map<Integer, List<IMUserInfo>> serverMap = new ConcurrentHashMap<>();
Map<String, IMUserInfo> sendMap = new HashMap<>();
for (Integer terminal : message.getRecvTerminals()) {
message.getRecvIds().parallelStream().forEach(id -> {
message.getRecvIds().stream().forEach(id -> {
String key = String.join(":", IMRedisKey.IM_USER_SERVER_ID, id.toString(), terminal.toString());
Integer serverId = (Integer)redisTemplate.opsForValue().get(key);
if (serverId != null) {
List<IMUserInfo> list = serverMap.computeIfAbsent(serverId, o -> Collections.synchronizedList(new LinkedList<>()));
list.add(new IMUserInfo(id, terminal));
} else {
// 加入离线列表
offLineUsers.add(new IMUserInfo(id, terminal));
}
sendMap.put(key,new IMUserInfo(id, terminal));
});
}
// 批量拉取
List<Object> serverIds = redisTemplate.opsForValue().multiGet(sendMap.keySet());
// 格式:map<服务器id,list<接收方>>
Map<Integer, List<IMUserInfo>> serverMap = new HashMap<>();
List<IMUserInfo> offLineUsers = Collections.synchronizedList(new LinkedList<>());
int idx = 0;
for (Map.Entry<String,IMUserInfo> entry : sendMap.entrySet()) {
Integer serverId = (Integer)serverIds.get(idx++);
if (serverId != null) {
List<IMUserInfo> list = serverMap.computeIfAbsent(serverId, o -> Collections.synchronizedList(new LinkedList<>()));
list.add(entry.getValue());
} else {
// 加入离线列表
offLineUsers.add(entry.getValue());
}
};
// 逐个server发送
for (Map.Entry<Integer, List<IMUserInfo>> entry : serverMap.entrySet()) {
IMRecvInfo recvInfo = new IMRecvInfo();

2
im-platform/src/main/java/com/bx/implatform/service/impl/GroupServiceImpl.java

@ -203,7 +203,7 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群组不存在");
}
if(group.getDeleted()){
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群组已解散");
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群组'"+group.getName()+"'已解散");
}
return group;
}

7
im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java

@ -9,6 +9,7 @@ import com.bx.imcommon.contant.IMConstant;
import com.bx.imcommon.enums.IMTerminalType;
import com.bx.imcommon.model.IMPrivateMessage;
import com.bx.imcommon.model.IMUserInfo;
import com.bx.implatform.entity.Friend;
import com.bx.implatform.vo.PrivateMessageVO;
import com.bx.implatform.entity.PrivateMessage;
import com.bx.implatform.enums.MessageStatus;
@ -169,10 +170,14 @@ public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper,
if (!imClient.isOnline(session.getUserId())) {
throw new GlobalException(ResultCode.PROGRAM_ERROR, "用户未建立连接");
}
List<Friend> friends = friendService.findFriendByUserId(session.getUserId());
List<Long> friendIds = friends.stream().map(Friend::getFriendId).collect(Collectors.toList());
// 获取当前用户所有未读消息
LambdaQueryWrapper<PrivateMessage> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(PrivateMessage::getRecvId, session.getUserId())
.eq(PrivateMessage::getStatus, MessageStatus.UNREAD);
.eq(PrivateMessage::getStatus, MessageStatus.UNREAD)
.in(PrivateMessage::getSendId,friendIds);
List<PrivateMessage> messages = this.list(queryWrapper);
// 上传至redis,等待推送
for(PrivateMessage message:messages){

Loading…
Cancel
Save