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