From 63125fa2c5d67b44f726a808fc07fd28bd9ca626 Mon Sep 17 00:00:00 2001 From: yitonglei Date: Fri, 29 Jul 2022 11:29:18 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E8=B5=84=E6=BA=90=E9=83=A8=E9=97=A8=E5=92=8C?= =?UTF-8?q?=E9=80=9A=E9=81=93=E4=BF=A1=E6=81=AF=E8=BF=87=E7=A8=8B=E4=B8=AD?= =?UTF-8?q?=E6=89=80=E4=BD=BF=E7=94=A8=E7=9A=84RestTemplate=E4=B8=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A1=86=E6=9E=B6=E5=AE=9A=E4=B9=89=E7=9A=84?= =?UTF-8?q?=E8=80=8C=E4=BD=BF=E7=94=A8=E6=96=B0=E5=AE=9A=E4=B9=89=E7=9A=84?= =?UTF-8?q?=EF=BC=88=E4=B8=BB=E8=A6=81=E6=98=AF=E9=87=8D=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/service/MonitorService.java | 127 +++++++++--------- .../task/GetAndSaveOrgenizationTask.java | 24 ++++ 2 files changed, 86 insertions(+), 65 deletions(-) create mode 100644 renren-admin/src/main/java/io/renren/modules/monitor/task/GetAndSaveOrgenizationTask.java diff --git a/renren-admin/src/main/java/io/renren/modules/monitor/service/MonitorService.java b/renren-admin/src/main/java/io/renren/modules/monitor/service/MonitorService.java index cb590328..cd487065 100644 --- a/renren-admin/src/main/java/io/renren/modules/monitor/service/MonitorService.java +++ b/renren-admin/src/main/java/io/renren/modules/monitor/service/MonitorService.java @@ -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 orgenizationByPage = this.getOrgenization(new ArrayList(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 getOrgenization(List list) { + RestTemplate template = this.getRestTemplate(); int count = 0; boolean flag = true; while(flag){ @@ -1174,11 +1179,11 @@ public class MonitorService { } count ++; try { - List list1 = this.getOrgenizationRoot(); + List 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 getOrgenizationRoot(){ + public List getOrgenizationRoot(RestTemplate restTemplate){ List 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 getOrgenizationByParent(List list,String id){ + public List getOrgenizationByParent(List 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 orgenList = cameraOrgenMapper.listOrgenization(); - for(Map m:orgenList){ - List cameChannels = getChannelInfo(m.get("id").toString()); - List 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> 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 orgenList = cameraOrgenMapper.listOrgenization(); +// for(Map m:orgenList){ +// List cameChannels = getChannelInfo(m.get("id").toString()); +// List 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> partition = Lists.partition(needSave, 100); +// partition.forEach(list->{ +// cameraOrgenMapper.batchSaveCameraChannel(list); +// }); +// cameraOrgenMapper.updateOrganizationCount(m.get("id").toString()); +// } +// } +// return Result.success(); +// } //根据组织id获取通道信息 - public List getChannelInfo(String orgenId) throws Exception{ + public List 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 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; } + } diff --git a/renren-admin/src/main/java/io/renren/modules/monitor/task/GetAndSaveOrgenizationTask.java b/renren-admin/src/main/java/io/renren/modules/monitor/task/GetAndSaveOrgenizationTask.java new file mode 100644 index 00000000..29e00bce --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/monitor/task/GetAndSaveOrgenizationTask.java @@ -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(); + } + +}