Schedule定时任务。定时执行,将结果放在指定的表中。LineiceSchedule.java 判段装置接受的数据是否为空,过多,过少;判定(EQUAL_ICETHICKNESS )等值覆冰厚度;检查超时接受的数据。MonitorDataSchedule.java 筛选同一id对应的所有IP,和同一IP对应所有id。
方法名上加注解@Scheduled,cron表达式 控制执行时间与频率。例如 @Scheduled(cron = "0 0 2 * * ?")
启动类上加注解@EnableScheduling。
LineiceSchedule.java
package com.evan.schedule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.evan.bean.SdCagErrormonitors;
import com.evan.bean.SdCagLineice;
import com.evan.bean.SdCagParam;
import com.evan.constant.Constant;
import com.evan.service.ISdCagErrordatasService;
import com.evan.service.ISdCagErrormonitorsService;
import com.evan.service.ISdCagLineiceService;
import com.evan.service.ISdCagParamService;
import com.evan.service.ITsDeviceService;
@Service
public class LineiceSchedule{
@Autowired
private ISdCagErrormonitorsService sdCagErrormonitorsService;
@Autowired
private ISdCagLineiceService sdCagLineiceService;
@Autowired
private ITsDeviceService tsDeviceService;
@Autowired
private ISdCagParamService sdCagParamService;
@Autowired
private ISdCagErrordatasService sdCagErrordatasService;
/**
* 判段装置接受的数据是否为空,过多,过少
*/
@Scheduled(cron = "0 0 2 * * ?")
public void analyse() {
long end = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
long start = end - 24*60*60*1000;
String type = Constant.MTYPE_LINEICE;
SdCagErrormonitors errormonitors = null;
List errormonitorsList = null;
List
MonitorDataSchedule.java
package com.evan.schedule;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.evan.bean.SdCagErrordatas;
import com.evan.bean.SdCagFromipId;
import com.evan.bean.VMonitorData;
import com.evan.constant.Constant;
import com.evan.service.ISdCagErrordatasService;
import com.evan.service.ISdCagFromipIdService;
@Service
public class MonitorDataSchedule{
@Autowired
private ISdCagErrordatasService sdCagErrordatasService;
@Autowired
private ISdCagFromipIdService sdCagFromipIdservice;
@Scheduled(cron = "0 30 2 * * ?")
public void analyseMonitorData() {
long begin = System.currentTimeMillis();
long end = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
long start = end - 24*60*60*1000;
String sameId = Constant.ERROR_IP_SAMEID;
String sameIp = Constant.ERROR_IP_SAMEIP;
SdCagErrordatas sdCagErrordatas = null;
List sdCagErrordatasList= null;
//防止重复执行产生重复数据
sdCagErrordatasService.batchDeleteByTime(start, end);
//得到的List中 只包含distinct 后 cmd_id fromip 字段
List vMonitorDataList = sdCagErrordatasService.getMonitorDataList(start, end);
System.out.println(vMonitorDataList.size());
List cmdIdList = new ArrayList();
List fromipList = new ArrayList();
for(VMonitorData vMonitorData : vMonitorDataList) {
cmdIdList.add(vMonitorData.getCmd_id());
fromipList.add(vMonitorData.getFromip() );
}
//获取装置表fromipid的所有信息
List sdCagFromipIdList = sdCagFromipIdservice.getSdCagFromipIdList();
//将id数大于1的筛选出来
List sameIdList = getDuplicateElements(cmdIdList);
sdCagErrordatasList = new ArrayList();
for(String id : sameIdList) {
List ipList = new ArrayList();
//取同id对应的ip
for(VMonitorData vMonitorData :vMonitorDataList) {
if(id.equals(vMonitorData.getCmd_id())){
ipList.add(vMonitorData.getFromip() );
}
}
for(String ip:ipList) {
sdCagErrordatas = new SdCagErrordatas();
sdCagErrordatas.setCmd_id(id);
sdCagErrordatas.setFromip(ip);
sdCagErrordatas.setType(sameId);
sdCagErrordatas.setError_msg("同一ID:"+id+",对应多个IP"+ipList.toString());
sdCagErrordatas.setTime_stamp(start);
sdCagErrordatas.setRecvtime(start);
sdCagErrordatas.setCreatime(start);
sdCagErrordatasList.add(sdCagErrordatas);
}
}
sdCagErrordatasService.batchAddSdCagErrordatas(sdCagErrordatasList);
//将fromip数大于1的筛选出来
List sameIpList = getDuplicateElements(fromipList);
sdCagErrordatasList = new ArrayList();
//获取同fromip对应的id
for(String ip : sameIpList) {
Set poleSet = new HashSet();
List idList = new ArrayList();
for(VMonitorData vMonitorData :vMonitorDataList) {
if(ip.equals(vMonitorData.getFromip())) {
idList.add(vMonitorData.getCmd_id());
poleSet.add(vMonitorData.getPole_id());
}
}
if(poleSet.size() > 1 ) {
List cmdIdListFromipId = new ArrayList();
for(SdCagFromipId sdCagFromipId : sdCagFromipIdList) {
if(ip.equals(sdCagFromipId.getFromip())){
cmdIdListFromipId.add(sdCagFromipId.getCmd_id());
}
}
//求出差集
idList.removeAll(cmdIdListFromipId);
for(String id:idList) {
sdCagErrordatas = new SdCagErrordatas();
sdCagErrordatas.setCmd_id(id);
sdCagErrordatas.setFromip(ip);
sdCagErrordatas.setType(sameIp);
sdCagErrordatas.setError_msg("同一IP:"+ip+",对应多个ID"+idList.toString());
sdCagErrordatas.setTime_stamp(start);
sdCagErrordatas.setRecvtime(start);
sdCagErrordatas.setCreatime(start);
sdCagErrordatasList.add(sdCagErrordatas);
}
}
}
sdCagErrordatasService.batchAddSdCagErrordatas(sdCagErrordatasList);
System.out.println("analyseMonitorData 执行时间:" + (System.currentTimeMillis( )- begin));
}
private List getDuplicateElements(List list) {
return list.stream()
.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum))
.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(entry -> entry.getKey())
.collect(Collectors.toList());
}
}