List<String> serverIds = tblGameActivityService.queryServersByChannelOrGameId(paramMap); //获取区服数组
if (serverIds == null){
Map<String,Object> reqOut=new HashMap<>();
reqOut.put("total",0);
reqOut.put("rows",new ArrayList<>());
return ResponseEntity.ok(reqOut);
}
long now = System.currentTimeMillis();
System.out.println("now==========> " +now);
//多数据源异步查询
List<Future<Map<String, Object>>> futureList = new ArrayList<>();
ExecutorService executorService = Executors.newCachedThreadPool();
for (String serverId : serverIds){ //异步查询数据总数
paramMap.put("serverId",serverId);
futureList.add(executorService.submit(()->{
Map<String, Object> map = new HashMap<>();
map.put("serverId",serverId);
System.out.println("Future==========> " +serverId);
map.put("num",logService.countItemLog(paramMap));
return map;//当前区服总数据量
}));
}
List<Map<String, Object>> list = new ArrayList<>();
Integer dataStart = Integer.valueOf(jsonObject.get("offset").toString()); //当前起始数据index
int count = 0; //记录数据总数
int remainder = 0; //前一个区服循环总数
boolean flg = true;
for (Future<Map<String, Object>> future : futureList){
try {
Map<String, Object> map = future.get();
paramMap.put("serverId",map.get("serverId"));
Integer num = Integer.parseInt(map.get("num").toString());
remainder = count;
count+=num;
System.out.println("server=> "+ map.get("serverId") +" num=> " + num + " remainder=> " + remainder + " count=> " + count + " dataStart=> " + dataStart);
Integer limit = Integer.valueOf(jsonObject.get("limit").toString());
if (dataStart < count && flg){ //当前页起始index 小于当前总数据时拉取详细数据
int offset = dataStart - remainder; //上一区查询数据剩余量差值
limit = offset < 0 ? limit+offset : limit; //若上小于0,上一区服当前页数据未满 本次查询补余量
paramMap.put("offset",offset < 0 ? 0 : offset);
paramMap.put("limit", limit);
System.out.println("offset=> "+ offset +" limit=> " + limit );
List<Map<String, Object>> logList = logService.queryItemLog(paramMap);
list.addAll(logList);
if (limit == logList.size()){ //查出数据若等于lim 当前页数据已拉完不再查询数据
flg = false;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Future学习记录
最新推荐文章于 2024-07-12 10:42:54 发布