package com.landtool.lanbase.common.scheduletask; import cn.hutool.core.collection.CollectionUtil; import com.landtool.lanbase.common.utils.HttpUtils; import com.landtool.lanbase.modules.res.entity.Res_ExtMapUrl; import com.landtool.lanbase.modules.res.entity.Res_MainInfo; import com.landtool.lanbase.modules.res.entity.Res_ProblemFeedback; import com.landtool.lanbase.modules.res.service.impl.ResMainInfoServiceImpl; import com.landtool.lanbase.modules.res.service.impl.ResProblemFeedbackServiceImpl; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.transaction.Transaction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.landtool.lanbase.modules.res.controller.ResMainInfoController; import com.landtool.lanbase.modules.res.service.impl.ResExtMapUrlServiceImpl; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.List; @Component @Configuration public class ScheduelTask { public Logger log=LoggerFactory.getLogger(ScheduelTask.class); @Autowired ResMainInfoServiceImpl resMainInfoService; @Autowired ResExtMapUrlServiceImpl resExtMapUrlService; @Autowired private ResProblemFeedbackServiceImpl resProblemFeedbackService; /* * 定时检验正常服务 异常服务更新 * add by zsx * 每天凌晨6点 * 查询所有资源原始地址 http请求地址 * 1.请求正常 如果资源之前状态为异常则更新正常 * 2.请求异常 若存在异常记录 则更新记录时间 否则插入一条新的记录 (对应资源状态为正常则更新为异常) // */ @Scheduled(cron = "0 0 6 * * ?") public void checkServiceUrl() { //获取所有的服务资源 List list = resExtMapUrlService.checkAllUrls(); log.info("服务监控开始"); for (Res_ExtMapUrl res_extMapUrl : list) { int status=0; //插入记录数据 serverStatus 0 正常 1异常 boolean flag=false; Integer resourceid = res_extMapUrl.getResourceid(); Integer serverResource = res_extMapUrl.getServerResource(); try { String serverurl = res_extMapUrl.getServerurl(); if(StringUtils.isNotBlank(serverurl)){ String s = HttpUtils.get(serverurl); //正常 如果存在之前异常数据则更新状态为正常 if(1== serverResource){ updateServerStatus(resourceid,status); } } } catch (Exception e) { status=1; //异常抛出认为是服务不能正常使用 之后异常不重复记录(更新校验时间), List problemFeedbacks = resProblemFeedbackService.selectByResourceid(resourceid); if(!CollectionUtil.isNotEmpty(problemFeedbacks)){ flag=true; } //首次记录异常记录 InsertProblem(resourceid,status,flag); //如果存在之前正常数据则更新状态为异常 if(0== serverResource){ updateServerStatus(resourceid,status); } } } log.info("服务监控结束"); } //更新异常记录状态 private void updateServerStatus(Integer resourceid, Integer status) { Res_MainInfo mainInfo = new Res_MainInfo(); mainInfo.setResourceid(resourceid); mainInfo.setResourcestatus(status); //状态更新回来 resMainInfoService.updateByPrimaryKeySelective(mainInfo); } //插入问题消息 private void InsertProblem(Integer resourceid, Integer status,Boolean flag) { Res_ProblemFeedback model = new Res_ProblemFeedback(); java.sql.Timestamp time = new java.sql.Timestamp(new Date().getTime()); model.setResourceid(resourceid); model.setAddtime(time); if (flag){ model.setCreateuserid(0); //管理员操作 model.setFeedbacktype("系统"); model.setServerstatus(status); model.setFeedbackstatus("服务问题"); model.setRemark("资源打不开"); resProblemFeedbackService.insertSelective(model); }else { resProblemFeedbackService.updateByResourceId(model); } } public static void main(String[] args) { try { HttpUtils.get("http://71.3.251.104:8066/arcgis/rest/services/6257/MapServer"); } catch (Exception e) { e.printStackTrace(); } } }