cantroller 层
@PostMapping("/traffic/records")
public List
}
@GetMapping("/visitor/record")
public List
public List<DoorRecordInfoVO> doorRecordList(String typeFlag) {
reentrantLock.lock();
try {
//首先获取 token
String token = this.getToken();
if (StrUtil.isEmpty(token)) {
return null;
}
List<DoorRecordInfoVO> doorRecordInfoVOList = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = now.minusHours(12L);
return handleList(doorRecordInfoVOList, now, start, token, typeFlag, 0);
}finally {
reentrantLock.unlock();
}
}
private List<DoorRecordInfoVO> handleList(List<DoorRecordInfoVO> doorRecordInfoVOList,
LocalDateTime now,
LocalDateTime start,
String token,
String typeFlag,
int retryCount) {
//判断两个时间是不是在同一天
if (now.toLocalDate().equals(start.toLocalDate())) {
//在同一天组装时间范围
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String startTime = start.format(DateTimeFormatter.ofPattern("HHmmss"));
String endTime = now.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray = this.apiResult(token, date, startTime, endTime);
this.addArrayToList(recordArray, doorRecordInfoVOList, typeFlag);
} else {
//不在同一天
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String endTime = now.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray1 = this.apiResult(token, date, "000000", endTime);
this.addArrayToList(recordArray1, doorRecordInfoVOList, typeFlag);
//加上前一天时间段
String preDate = start.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String startTime = start.format(DateTimeFormatter.ofPattern("HHmmss"));
JSONArray recordArray2 = this.apiResult(token, preDate, startTime, "235959");
this.addArrayToList(recordArray2, doorRecordInfoVOList, typeFlag);
}
if (doorRecordInfoVOList.size() < 3) {
now = start;
start = start.minusHours(1L);
handleList(doorRecordInfoVOList, now, start, token, typeFlag, retryCount + 1);
}
return doorRecordInfoVOList;
}
private void addArrayToList(JSONArray array, List<DoorRecordInfoVO> doorRecordInfoVOList, String typeFlag) {
if (ObjectUtil.isEmpty(array)) return;
for (int i = 0; i < array.size(); i++) {
JSONObject record = array.getJSONObject(i);
DoorRecordInfoVO doorRecordInfoVO = new DoorRecordInfoVO();
String type = record.getStr("recogResult");
doorRecordInfoVO.setUserName(record.getStr("userName"));
doorRecordInfoVO.setDoorName(record.getStr("doorName"));
doorRecordInfoVO.setOpenTime(
HcDateUtil.dateFormat(record.getStr("openTime"), "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss")
);
doorRecordInfoVO.setRecordType(type);
if (StrUtil.isNotEmpty(typeFlag) && typeFlag.equals(type)) {
doorRecordInfoVOList.add(doorRecordInfoVO);
} else if (StrUtil.isEmpty(typeFlag)) {
doorRecordInfoVOList.add(doorRecordInfoVO);
}
}
}
private JSONArray apiResult(String token, String date, String startTime, String endTime) {
JSONArray jsonArray = null;
//组装请求参数
Map<String, Object> requestMap = new HashMap<>();
Map<String, Object> map = new HashMap<>();
map.put("beginTime", startTime);
map.put("endTime", endTime);
map.put("date", date);
requestMap.put("data", map);
//调用 api 获取数据
String body = HttpRequest
.post(getDoorRecordInfoUrl)
.header("token", token)
.header("appId", appId)
.header("Content-Type", "application/json")
.body(JSONUtil.toJsonStr(requestMap))
.execute()
.body();
JSONObject resultJson = JSONUtil.parseObj(body);
Integer code = resultJson.getInt("code");
if (code == 200) {
JSONObject data = resultJson.getJSONObject("data");
jsonArray = data.getJSONArray("data");
}
return jsonArray;
}
上述实现如果我不加上 reentrantLock ,doorRecordInfoVOList 线程安全问题,有一个接口直接返回空了,但是直接分开调用,都有返回,请问这个为啥会有线程安全问题,求大佬指教
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.