1、自动获取视频资源部门和通道信息过程中所使用的RestTemplate不使用框架定义的而使用新定义的(主要是重新配置超时时间)

This commit is contained in:
yitonglei 2022-07-29 11:29:18 +08:00
parent 0ccdf069f8
commit 63125fa2c5
2 changed files with 86 additions and 65 deletions

View File

@ -20,6 +20,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@ -39,6 +41,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
@ -1137,7 +1140,7 @@ public class MonitorService {
return Result.success(maps); return Result.success(maps);
} }
//以此作为获取视频资源 //以此作为获取视频资源的开始获取地区和部门信息
public void getAndSaveOrgenization(){ public void getAndSaveOrgenization(){
List<JSONObject> orgenizationByPage = this.getOrgenization(new ArrayList<JSONObject>(10000)); List<JSONObject> orgenizationByPage = this.getOrgenization(new ArrayList<JSONObject>(10000));
if(orgenizationByPage != null && orgenizationByPage.size() > 0){ if(orgenizationByPage != null && orgenizationByPage.size() > 0){
@ -1163,9 +1166,11 @@ public class MonitorService {
publisher.publishEvent(new SaveOrgenizationEndEvent(this,true)); publisher.publishEvent(new SaveOrgenizationEndEvent(this,true));
} }
} }
publisher.publishEvent(new SaveOrgenizationEndEvent(this,true));
} }
public List<JSONObject> getOrgenization(List<JSONObject> list) { public List<JSONObject> getOrgenization(List<JSONObject> list) {
RestTemplate template = this.getRestTemplate();
int count = 0; int count = 0;
boolean flag = true; boolean flag = true;
while(flag){ while(flag){
@ -1174,11 +1179,11 @@ public class MonitorService {
} }
count ++; count ++;
try { try {
List<JSONObject> list1 = this.getOrgenizationRoot(); List<JSONObject> list1 = this.getOrgenizationRoot(template);
list.addAll(list1); list.addAll(list1);
list1.forEach(a->{ list1.forEach(a->{
if(a.getBooleanValue("isParent")){ if(a.getBooleanValue("isParent")){
getOrgenizationByParent(list,a.getString("id")); getOrgenizationByParent(list,a.getString("id"),template);
} }
}); });
// if(list != null && list.size() > 0){ // if(list != null && list.size() > 0){
@ -1206,7 +1211,7 @@ public class MonitorService {
} }
//获取根组织 //获取根组织
public List<JSONObject> getOrgenizationRoot(){ public List<JSONObject> getOrgenizationRoot(RestTemplate restTemplate){
List<JSONObject> list = new ArrayList<>(); List<JSONObject> list = new ArrayList<>();
String url = monitorDomain + "/videoService/devicesManager/deviceTree?id=&nodeType=1&typeCode=01&page=1&pageSize=3000"; String url = monitorDomain + "/videoService/devicesManager/deviceTree?id=&nodeType=1&typeCode=01&page=1&pageSize=3000";
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -1221,7 +1226,7 @@ public class MonitorService {
} }
//组织递归根据父ID查,简化了分页 //组织递归根据父ID查,简化了分页
public List<JSONObject> getOrgenizationByParent(List<JSONObject> list,String id){ public List<JSONObject> getOrgenizationByParent(List<JSONObject> list,String id,RestTemplate restTemplate){
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("X-Subject-Token",token); headers.add("X-Subject-Token",token);
String url; String url;
@ -1241,7 +1246,7 @@ public class MonitorService {
list.addAll(jsonObjects); list.addAll(jsonObjects);
jsonObjects.forEach(js->{ jsonObjects.forEach(js->{
if(js.getBooleanValue("isParent")){ if(js.getBooleanValue("isParent")){
getOrgenizationByParent(list,js.getString("id")); getOrgenizationByParent(list,js.getString("id"),restTemplate);
} }
}); });
} }
@ -1260,48 +1265,48 @@ public class MonitorService {
} }
//保存通道信息 //保存通道信息
public Result saveChannelInfo() throws Exception { // public Result saveChannelInfo() throws Exception {
List<Map> orgenList = cameraOrgenMapper.listOrgenization(); // List<Map> orgenList = cameraOrgenMapper.listOrgenization();
for(Map m:orgenList){ // for(Map m:orgenList){
List<Map> cameChannels = getChannelInfo(m.get("id").toString()); // List<Map> cameChannels = getChannelInfo(m.get("id").toString());
List<Map> needSave = new ArrayList<>(); // List<Map> needSave = new ArrayList<>();
if(cameChannels.size() > 0){ // if(cameChannels.size() > 0){
boolean flag = false; // boolean flag = false;
for(Map j:cameChannels){ // for(Map j:cameChannels){
if(Integer.parseInt(j.get("nodeType").toString()) ==3){ // if(Integer.parseInt(j.get("nodeType").toString()) ==3){
flag = true; // flag = true;
String channelSn = j.get("channelSn").toString(); // String channelSn = j.get("channelSn").toString();
String channelOrngin = channelSn.substring(0,6); // String channelOrngin = channelSn.substring(0,6);
String deptName = cameraOrgenMapper.getNameByidPart(channelOrngin); // String deptName = cameraOrgenMapper.getNameByidPart(channelOrngin);
j.put("regionName",deptName); // j.put("regionName",deptName);
j.put("regionCode",channelOrngin); // j.put("regionCode",channelOrngin);
j.put("parentId",m.get("id").toString()); // j.put("parentId",m.get("id").toString());
j.put("nodeName",""); // j.put("nodeName","");
needSave.add(j); // needSave.add(j);
} // }
} // }
if(!flag){//更新count字段 // if(!flag){//更新count字段
cameraOrgenMapper.updateOrganizationCount(m.get("id").toString()); // cameraOrgenMapper.updateOrganizationCount(m.get("id").toString());
} // }
}else{//更新count字段 // }else{//更新count字段
cameraOrgenMapper.updateOrganizationCount(m.get("id").toString()); // cameraOrgenMapper.updateOrganizationCount(m.get("id").toString());
} // }
//
needSave.forEach(map->setNodeName(map,map.get("parentId").toString())); // needSave.forEach(map->setNodeName(map,map.get("parentId").toString()));
//保存并更新count字段 // //保存并更新count字段
if(needSave.size() > 0){ // if(needSave.size() > 0){
List<List<Map>> partition = Lists.partition(needSave, 100); // List<List<Map>> partition = Lists.partition(needSave, 100);
partition.forEach(list->{ // partition.forEach(list->{
cameraOrgenMapper.batchSaveCameraChannel(list); // cameraOrgenMapper.batchSaveCameraChannel(list);
}); // });
cameraOrgenMapper.updateOrganizationCount(m.get("id").toString()); // cameraOrgenMapper.updateOrganizationCount(m.get("id").toString());
} // }
} // }
return Result.success(); // return Result.success();
} // }
//根据组织id获取通道信息 //根据组织id获取通道信息
public List<Map> getChannelInfo(String orgenId) throws Exception{ public List<Map> getChannelInfo(String orgenId,RestTemplate restTemplate) throws Exception{
String url = monitorDomain +"/videoService/devicesManager/deviceTree?nodeType=1&typeCode=01;0;ALL;ALL&page=1&pageSize=3000&id="+orgenId; String url = monitorDomain +"/videoService/devicesManager/deviceTree?nodeType=1&typeCode=01;0;ALL;ALL&page=1&pageSize=3000&id="+orgenId;
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("X-Subject-Token",token); headers.add("X-Subject-Token",token);
@ -1331,8 +1336,8 @@ public class MonitorService {
} }
//2获取视频通道信息并保存,多线程版 //2获取视频通道信息并保存,多线程版
//@Async
public void saveChannelInfoAsync() throws Exception { public void saveChannelInfoAsync() throws Exception {
RestTemplate restTemplate = this.getRestTemplate();
AtomicInteger faulseCount = new AtomicInteger();//失败的次数 AtomicInteger faulseCount = new AtomicInteger();//失败的次数
//1-清空t_camera_channel //1-清空t_camera_channel
cameraOrgenMapper.truncate("t_camera_channel_cache"); cameraOrgenMapper.truncate("t_camera_channel_cache");
@ -1360,7 +1365,7 @@ public class MonitorService {
//4-根据地区id去查询视频通道信息 //4-根据地区id去查询视频通道信息
List<Map> cameraChannels = new ArrayList<>(); List<Map> cameraChannels = new ArrayList<>();
try { try {
cameraChannels = getChannelInfo(m.get("id").toString()); cameraChannels = getChannelInfo(m.get("id").toString(),restTemplate);
}catch (Exception e){ }catch (Exception e){
log.info("根据地区id:{}查询视频通道失败,这是第{}次重试",m.get("id").toString(),tryCount); log.info("根据地区id:{}查询视频通道失败,这是第{}次重试",m.get("id").toString(),tryCount);
@ -1380,8 +1385,6 @@ public class MonitorService {
} }
//5-保存视频通道信息 //5-保存视频通道信息
batchSaveChannelInfos(cameraChannels,m.get("id").toString()); batchSaveChannelInfos(cameraChannels,m.get("id").toString());
//6-更新地区表的count
//cameraOrgenMapper.updateOrganizationCount(m.get("id").toString());
flag = false; flag = false;
} }
}); });
@ -1399,12 +1402,6 @@ public class MonitorService {
//发布事件 //发布事件
publisher.publishEvent(new SaveCameraChannelEndEvent(this,true)); publisher.publishEvent(new SaveCameraChannelEndEvent(this,true));
//以下方法将在事件后处理
//将cache表数据保存到相应的主表中
//insertChannelCacheToCameraChannel);
//8-同步武伟达的t_channel_mtm_label数据
//synchronizeMtmLabel();
} }
//同步武伟达的t_channel_mtm_label数据 //同步武伟达的t_channel_mtm_label数据
@ -1511,17 +1508,17 @@ public class MonitorService {
return childs; return childs;
} }
public void test(){ public RestTemplate getRestTemplate(){
//保存cache表信息到正式表 SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
insertChannelCacheToCameraChannel(); factory.setReadTimeout(20000);//单位为ms
factory.setConnectTimeout(3000);//单位为ms
cameraOrgenMapper.updateRegionChannelCount(); factory.setOutputStreaming(false);
//更新武伟达的标签表
//synchronizeMtmLabel();
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
} }
} }

View File

@ -0,0 +1,24 @@
package io.renren.modules.monitor.task;
import io.renren.modules.job.task.ITask;
import io.renren.modules.monitor.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 定时获取视频资源的部门信息
* @author ytl
* @Date 2022/7/29 9:45
**/
@Component("getAndSaveOrgenizationTask")
public class GetAndSaveOrgenizationTask implements ITask {
@Autowired
private MonitorService monitorService;
@Override
public void run(String params) {
monitorService.getAndSaveOrgenization();
}
}