fix : 流程解析存在多线程并发问题
This commit is contained in:
@@ -37,17 +37,6 @@ public class ProcessNodeService {
|
||||
|
||||
@DubboReference
|
||||
private DubboUserService userService;
|
||||
/**
|
||||
* 用户id缓存
|
||||
*/
|
||||
private final Map<String, List<Long>> cacheUserIdMap;
|
||||
//角色用户缓存
|
||||
private final Map<Long, List<UserVo>> cacheRoleCacheUserMap;
|
||||
//部门用户缓存
|
||||
private final Map<Long, UserInfo> cacheDeptCacheUserMap;
|
||||
|
||||
//用户缓存信息
|
||||
private final Map<Long, UserInfo> cacheUserMap;
|
||||
|
||||
|
||||
public ProcessNodeService() {
|
||||
@@ -57,10 +46,6 @@ public class ProcessNodeService {
|
||||
TimeUnit.SECONDS, new LinkedBlockingDeque<>(128),
|
||||
new ThreadFactoryBuilder().setNameFormat("process_%d").build(),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
cacheUserIdMap = new ConcurrentHashMap<>();
|
||||
cacheRoleCacheUserMap = new ConcurrentHashMap<>();
|
||||
cacheDeptCacheUserMap = new ConcurrentHashMap<>();
|
||||
cacheUserMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
|
||||
@@ -92,29 +77,11 @@ public class ProcessNodeService {
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
clearCache();
|
||||
wrapper.clearCache();
|
||||
System.out.println("用时 :" + (new Date().getTime() - startTime.getTime()));
|
||||
return wrapper.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
|
||||
/**
|
||||
* 清除缓存,当空间大于16时进行清除
|
||||
*/
|
||||
private void clearCache() {
|
||||
if (cacheRoleCacheUserMap.size() > 16) {
|
||||
cacheRoleCacheUserMap.clear();
|
||||
}
|
||||
if (cacheUserIdMap.size() > 16) {
|
||||
cacheUserIdMap.clear();
|
||||
}
|
||||
if (cacheUserMap.size() > 16) {
|
||||
cacheUserMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色查询
|
||||
@@ -122,12 +89,12 @@ public class ProcessNodeService {
|
||||
* @param roleIds 角色id列表
|
||||
* @return 用户信息
|
||||
*/
|
||||
private List<UserInfo> searchUserListByRoleIds(List<Long> roleIds) {
|
||||
private List<UserInfo> searchUserListByRoleIds(List<Long> roleIds, ProcessNodeWrapper wrapper) {
|
||||
List<UserVo> result = new ArrayList<>();
|
||||
List<Long> roleIdsNew = new ArrayList<>();
|
||||
roleIds.forEach(roleId -> {
|
||||
if (cacheRoleCacheUserMap.containsKey(roleId)) {
|
||||
result.addAll(cacheRoleCacheUserMap.get(roleId));
|
||||
if (wrapper.cacheRoleCacheUserMap.containsKey(roleId)) {
|
||||
result.addAll(wrapper.cacheRoleCacheUserMap.get(roleId));
|
||||
} else {
|
||||
roleIdsNew.add(roleId);
|
||||
}
|
||||
@@ -135,7 +102,7 @@ public class ProcessNodeService {
|
||||
if (!roleIdsNew.isEmpty()) {
|
||||
List<UserVo> userList = userService.searchUserListByRoleIds(roleIdsNew);
|
||||
Map<Long, List<UserVo>> userRole = userList.stream().collect(Collectors.groupingBy(UserVo::getRoleId));
|
||||
cacheRoleCacheUserMap.putAll(userRole);
|
||||
wrapper.cacheRoleCacheUserMap.putAll(userRole);
|
||||
result.addAll(userList);
|
||||
}
|
||||
return result.stream().map(userVo ->
|
||||
@@ -148,12 +115,12 @@ public class ProcessNodeService {
|
||||
* @param deptIds 部门id列表
|
||||
* @return 用户信息
|
||||
*/
|
||||
private List<UserInfo> searchUserByDeptIds(List<Long> deptIds) {
|
||||
private List<UserInfo> searchUserByDeptIds(List<Long> deptIds, ProcessNodeWrapper wrapper) {
|
||||
List<UserInfo> result = new ArrayList<>();
|
||||
List<Long> deptIdsNew = new ArrayList<>();
|
||||
deptIds.forEach(deptId -> {
|
||||
if (cacheDeptCacheUserMap.containsKey(deptId)) {
|
||||
result.add(cacheDeptCacheUserMap.get(deptId));
|
||||
if (wrapper.cacheDeptCacheUserMap.containsKey(deptId)) {
|
||||
result.add(wrapper.cacheDeptCacheUserMap.get(deptId));
|
||||
} else {
|
||||
deptIdsNew.add(deptId);
|
||||
}
|
||||
@@ -163,7 +130,7 @@ public class ProcessNodeService {
|
||||
userList.forEach(userVo -> {
|
||||
UserInfo value = UserInfo.toUserInfo(userVo, OperationStateEnums.UNACTIVATED);
|
||||
result.add(value);
|
||||
cacheDeptCacheUserMap.put(userVo.getLeaderDeptId(), value);
|
||||
wrapper.cacheDeptCacheUserMap.put(userVo.getLeaderDeptId(), value);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
@@ -175,12 +142,12 @@ public class ProcessNodeService {
|
||||
* @param userIds
|
||||
* @return 用户信息列表
|
||||
*/
|
||||
private List<UserInfo> searchUserListByUserIds(List<Long> userIds) {
|
||||
private List<UserInfo> searchUserListByUserIds(List<Long> userIds, ProcessNodeWrapper wrapper) {
|
||||
List<UserInfo> result = new ArrayList<>();
|
||||
List<Long> userIdsNew = new ArrayList<>();
|
||||
userIds.forEach(userId -> {
|
||||
if (cacheUserMap.containsKey(userId)) {
|
||||
result.add(cacheUserMap.get(userId));
|
||||
if (wrapper.cacheUserMap.containsKey(userId)) {
|
||||
result.add(wrapper.cacheUserMap.get(userId));
|
||||
} else {
|
||||
userIdsNew.add(userId);
|
||||
}
|
||||
@@ -190,7 +157,7 @@ public class ProcessNodeService {
|
||||
userList.forEach(userVo -> {
|
||||
UserInfo value = UserInfo.toUserInfo(userVo, OperationStateEnums.UNACTIVATED);
|
||||
result.add(value);
|
||||
cacheUserMap.put(userVo.getUserId(), value);
|
||||
wrapper.cacheUserMap.put(userVo.getUserId(), value);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
@@ -304,7 +271,7 @@ public class ProcessNodeService {
|
||||
List<Long> parentIds = Arrays.stream(dept.getAncestors().split(",")).map(Long::valueOf).collect(Collectors.toList());
|
||||
switch (endCondition) {
|
||||
case "TOP"://直接到最上层
|
||||
assignedUser = searchUserByDeptIds(parentIds.subList(1, parentIds.size()));
|
||||
assignedUser = searchUserByDeptIds(parentIds.subList(1, parentIds.size()), wrapper);
|
||||
break;
|
||||
case "LEAVE"://到指定级别
|
||||
int level = Integer.parseInt(leaderTop.get("level").toString());
|
||||
@@ -315,7 +282,7 @@ public class ProcessNodeService {
|
||||
} else {
|
||||
leaderDeptIds = parentIds.subList(parentIds.size() - level, parentIds.size());
|
||||
}
|
||||
assignedUser = searchUserByDeptIds(leaderDeptIds);
|
||||
assignedUser = searchUserByDeptIds(leaderDeptIds, wrapper);
|
||||
break;
|
||||
default:
|
||||
throw new CustomException("当前选项还未实现!");
|
||||
@@ -331,7 +298,7 @@ public class ProcessNodeService {
|
||||
int level = Integer.parseInt(props.getLeader().get("level").toString());
|
||||
Dept dept = wrapper.user.getDept();
|
||||
if (1 == level) {
|
||||
assignedUser = searchUserListByUserIds(Collections.singletonList(dept.getDeptId()));
|
||||
assignedUser = searchUserListByUserIds(Collections.singletonList(dept.getDeptId()), wrapper);
|
||||
} else {
|
||||
List<Long> parentIds = Arrays.stream(dept.getAncestors().split(",")).map(Long::valueOf).collect(Collectors.toList());
|
||||
Long leaderDeptId = null;
|
||||
@@ -341,7 +308,7 @@ public class ProcessNodeService {
|
||||
} else {
|
||||
leaderDeptId = parentIds.get(parentIds.size() - level);
|
||||
}
|
||||
assignedUser = searchUserByDeptIds(Collections.singletonList(leaderDeptId));
|
||||
assignedUser = searchUserByDeptIds(Collections.singletonList(leaderDeptId), wrapper);
|
||||
}
|
||||
props.setAssignedUser(assignedUser);
|
||||
props.setAssignedType(AssigneeTypeEnums.ASSIGN_USER);
|
||||
@@ -374,7 +341,7 @@ public class ProcessNodeService {
|
||||
List<UserInfo> assignedUser;
|
||||
List<Long> roleIds = props.getRoleList().stream().map(RoleInfo::getRoleId).collect(Collectors.toList());
|
||||
//rpc远程通过角色id获取到当前角色下的所有用户数据
|
||||
assignedUser = searchUserListByRoleIds(roleIds);
|
||||
assignedUser = searchUserListByRoleIds(roleIds, wrapper);
|
||||
props.setAssignedUser(assignedUser);
|
||||
props.setAssignedType(AssigneeTypeEnums.ASSIGN_USER);
|
||||
setResult(processNode, wrapper);
|
||||
@@ -383,12 +350,15 @@ public class ProcessNodeService {
|
||||
|
||||
private static class ProcessNodeWrapper {
|
||||
|
||||
public ProcessNodeWrapper(Map<String, List<UserInfo>> userOperationInfoMap, JSONObject optionalUser, User user, Boolean isStart) {
|
||||
this.userOperationInfoMap = userOperationInfoMap;
|
||||
this.optionalUser = optionalUser;
|
||||
this.user = user;
|
||||
this.isStart = isStart;
|
||||
}
|
||||
|
||||
//用户id缓存
|
||||
private final Map<String, List<Long>> cacheUserIdMap;
|
||||
//角色用户缓存
|
||||
private final Map<Long, List<UserVo>> cacheRoleCacheUserMap;
|
||||
//部门用户缓存
|
||||
private final Map<Long, UserInfo> cacheDeptCacheUserMap;
|
||||
//用户缓存信息
|
||||
private final Map<Long, UserInfo> cacheUserMap;
|
||||
|
||||
//操作历史缓存
|
||||
private final Map<String, List<UserInfo>> userOperationInfoMap;
|
||||
@@ -397,5 +367,33 @@ public class ProcessNodeService {
|
||||
private final List<ProcessNode> result = new ArrayList<>();
|
||||
private final User user;
|
||||
private final Boolean isStart;
|
||||
|
||||
|
||||
public ProcessNodeWrapper(Map<String, List<UserInfo>> userOperationInfoMap, JSONObject optionalUser, User user, Boolean isStart) {
|
||||
this.userOperationInfoMap = userOperationInfoMap;
|
||||
this.optionalUser = optionalUser;
|
||||
this.user = user;
|
||||
this.isStart = isStart;
|
||||
this.cacheUserIdMap = new ConcurrentHashMap<>();
|
||||
this.cacheRoleCacheUserMap = new ConcurrentHashMap<>();
|
||||
this.cacheDeptCacheUserMap = new ConcurrentHashMap<>();
|
||||
this.cacheUserMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存,当空间大于16时进行清除
|
||||
*/
|
||||
private void clearCache() {
|
||||
if (cacheRoleCacheUserMap.size() > 16) {
|
||||
cacheRoleCacheUserMap.clear();
|
||||
}
|
||||
if (cacheUserIdMap.size() > 16) {
|
||||
cacheUserIdMap.clear();
|
||||
}
|
||||
if (cacheUserMap.size() > 16) {
|
||||
cacheUserMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user