SpringCronTask.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package com.steerinfo.task;
  2. import com.steerinfo.ems.Utils.DateUtils;
  3. import com.steerinfo.ems.bfhyd.service.IBfhydService;
  4. import com.steerinfo.ems.dzmaterialsf.service.IDzMaterialSfService;
  5. import com.steerinfo.ems.emsetiming.service.IEmsETimingService;
  6. import com.steerinfo.ems.emsgmpcjh.service.IEmsGmPcJhService;
  7. import com.steerinfo.ems.emsscqklg.service.IEmsScqkLgService;
  8. import com.steerinfo.ems.emsscqklg.service.ILgRealService;
  9. import com.steerinfo.ems.emswaterrealtime.service.IEmsWaterRealtimeService;
  10. import com.steerinfo.ems.foldthevalues.service.IFoldTheValuesService;
  11. import com.steerinfo.ems.ifemsmesactitem.service.IIfEmsMesActitemService;
  12. import com.steerinfo.ems.ifmesemsproductorder.service.IIfMesEmsProductorderService;
  13. import com.steerinfo.ems.ifmesemsswapfile.service.IIfMesEmsSwapfileService;
  14. import com.steerinfo.ems.ifmesemswatermonitor.service.IIfMesEmsWatermonitorService;
  15. import com.steerinfo.ems.mesemsdieseloil.service.IMesEmsDieseloilService;
  16. import com.steerinfo.ems.rttable.service.IRttableService;
  17. import com.steerinfo.ems.tcm0312.controller.TCm0312Controller;
  18. import com.steerinfo.ems.tcm0318.service.ITCm0318Service;
  19. import com.steerinfo.ems.tcm0348.service.ITCm0348Service;
  20. import com.steerinfo.ems.trmactvalue.service.ITRmActValueService;
  21. import com.steerinfo.ems.trmbalancevalue.service.ITRmBalanceValueService;
  22. import com.steerinfo.ems.trmcalpoint.service.ITRmCalpointService;
  23. import com.steerinfo.ems.trmcalpointvalue.service.ITRmCalpointValueService;
  24. import com.steerinfo.ems.trmdfvalue.service.ITRmDfValueService;
  25. import com.steerinfo.ems.trmdwvalue.service.ITRmDwValueService;
  26. import com.steerinfo.ems.trmindexvalue.service.ITRmIndexValueService;
  27. import com.steerinfo.ems.trmplanvalue.service.ITRmPlanValueService;
  28. import com.steerinfo.ems.trmtransfereactvalue.service.ITRmTransfereActValueService;
  29. import com.steerinfo.ems.trmworkprocmaterialvalue.service.ITRmWorkprocMaterialValueService;
  30. import com.steerinfo.ems.trmworkprocproductvalue.service.ITRmWorkprocProductValueService;
  31. import com.steerinfo.ems.usagedata.service.IUsageDataService;
  32. import com.steerinfo.ems.zghbjkbzlog.service.IZghbjkbzLogService;
  33. import org.apache.axis2.AxisFault;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.jdbc.core.JdbcTemplate;
  38. import org.springframework.scheduling.SchedulingException;
  39. import org.springframework.scheduling.TaskScheduler;
  40. import org.springframework.scheduling.annotation.EnableScheduling;
  41. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  42. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  43. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  44. import org.springframework.scheduling.support.CronTrigger;
  45. import org.springframework.stereotype.Service;
  46. import java.text.DateFormat;
  47. import java.text.ParseException;
  48. import java.text.SimpleDateFormat;
  49. import java.util.*;
  50. import java.util.concurrent.ConcurrentHashMap;
  51. import java.util.concurrent.ScheduledFuture;
  52. @EnableScheduling
  53. @Service(value = "springCronTask")
  54. public class SpringCronTask implements SchedulingConfigurer{
  55. private static final Logger logger = LoggerFactory.getLogger(SpringCronTask.class);
  56. private ScheduledTaskRegistrar taskRegistrar;
  57. private Map<String, ScheduledFuture<?>> taskFutures = new ConcurrentHashMap<String, ScheduledFuture<?>>();
  58. @Autowired
  59. private JdbcTemplate jdbcTemplate;
  60. @Autowired
  61. IIfMesEmsSwapfileService ifMesEmsSwapfileService;
  62. @Autowired
  63. IIfMesEmsProductorderService ifMesEmsProductorderService;
  64. @Autowired
  65. ITRmWorkprocMaterialValueService tRmWorkprocMaterialValueService;
  66. @Autowired
  67. ITRmWorkprocProductValueService tRmWorkprocProductValueService;
  68. @Autowired
  69. ITRmCalpointValueService tRmCalpointValueService;
  70. @Autowired
  71. IUsageDataService usageDataService;
  72. @Autowired
  73. ITRmActValueService tRmActValueService;
  74. @Autowired
  75. ITRmPlanValueService tRmPlanValueService;
  76. @Autowired
  77. IEmsETimingService emsETimingService;
  78. @Autowired
  79. TCm0312Controller tCm0312Controller;
  80. @Autowired
  81. IIfMesEmsWatermonitorService ifMesEmsWatermonitorService;
  82. @Autowired
  83. IIfEmsMesActitemService ifEmsMesActitemService;
  84. @Autowired
  85. ITRmIndexValueService tRmIndexValueService;
  86. @Autowired
  87. ITRmBalanceValueService tRmBalanceValueService;
  88. @Autowired
  89. IZghbjkbzLogService zghbjkbzLogService;
  90. @Autowired
  91. IMesEmsDieseloilService mesEmsDieseloilService;
  92. @Autowired
  93. ITRmTransfereActValueService tRmTransfereActValueService;
  94. @Autowired
  95. ITRmDwValueService tRmDwValueService;
  96. @Autowired
  97. ITRmCalpointService tRmCalpointService;
  98. @Autowired
  99. ITRmDfValueService tRmDfValueService;
  100. @Autowired
  101. IBfhydService bfhydService;
  102. @Autowired
  103. ITCm0348Service tCm0348Service;
  104. @Autowired
  105. ITCm0318Service tCm0318Service;
  106. @Autowired
  107. IEmsWaterRealtimeService emsWaterRealtimeService;
  108. @Autowired
  109. IEmsGmPcJhService emsGmPcJhService;
  110. @Autowired
  111. IDzMaterialSfService dzMaterialSfService;
  112. @Autowired
  113. IRttableService rttableService;
  114. @Autowired
  115. IEmsScqkLgService emsScqkLgService;
  116. @Autowired
  117. ILgRealService lgRealService;
  118. @Autowired
  119. IFoldTheValuesService foldTheValuesService;
  120. /**
  121. * SchedulingConfigurer接口的方法实现,加载后自动运行
  122. */
  123. @Override
  124. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  125. ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
  126. taskScheduler.setThreadNamePrefix("ems-task-");
  127. taskScheduler.setPoolSize(4);
  128. taskScheduler.initialize();
  129. taskRegistrar.setTaskScheduler(taskScheduler);
  130. this.taskRegistrar = taskRegistrar;
  131. // 初始化任务
  132. String sql = "select * from TASK_CONF order by id";
  133. List<Map<String, Object>> l = jdbcTemplate.queryForList(sql);
  134. if (l!=null && l.size()>0){
  135. for(int i =0; i<l.size(); i++){
  136. Map<String, Object> m = (Map<String, Object>)l.get(i);
  137. String taskId = m.get("ID").toString();
  138. String cron = m.get("CRON").toString();
  139. String status = m.get("STATUS") == null ? "" : m.get("STATUS").toString();
  140. if(status.equalsIgnoreCase("1") && !taskFutures.containsKey(taskId)){
  141. addTask(taskId, cron);
  142. }
  143. if(status.equalsIgnoreCase("0") && taskFutures.containsKey(taskId)){
  144. cancelTriggerTask(taskId);
  145. }
  146. logger.info("定时任务-" + taskId + " cron表达式:" + cron);
  147. }
  148. }
  149. }
  150. /**
  151. * 添加定时任务(初始化)
  152. *
  153. * @param taskId
  154. * @param cron
  155. */
  156. public void addTask(String taskId, String cron) {
  157. if (!taskFutures.containsKey(taskId)) {
  158. TaskScheduler scheduler = taskRegistrar.getScheduler();
  159. ScheduledFuture<?> future = scheduler.schedule(doTask(taskId), new CronTrigger(cron));
  160. taskFutures.put(taskId, future);
  161. logger.info("当前定时任务队列长度:" + taskFutures.size());
  162. } else {
  163. throw new SchedulingException("the taskId[" + taskId + "] was added.");
  164. }
  165. }
  166. /**
  167. * 添加或者更新定时任务
  168. *
  169. * @param taskId
  170. */
  171. public void updateTask(String taskId) {
  172. if(taskFutures.containsKey(taskId)){
  173. cancelTriggerTask(taskId);
  174. }
  175. TaskScheduler scheduler = taskRegistrar.getScheduler();
  176. ScheduledFuture<?> future = scheduler.schedule(doTask(taskId), new CronTrigger(getCron(taskId)));
  177. taskFutures.put(taskId, future);
  178. logger.info("当前定时任务队列长度:" + taskFutures.size());
  179. }
  180. /**
  181. * 取消任务
  182. * @param taskId
  183. */
  184. public void cancelTriggerTask(String taskId){
  185. ScheduledFuture<?> future = taskFutures.get(taskId);
  186. if (future != null){
  187. future.cancel(true);
  188. }
  189. taskFutures.remove(taskId);
  190. }
  191. /**
  192. * 业务处理
  193. *
  194. * @param taskId
  195. * @return
  196. */
  197. private Runnable doTask(String taskId) {
  198. Properties props = System.getProperties(); // 系统属性
  199. return new Runnable() {
  200. @Override
  201. public void run() {
  202. // if(!props.getProperty("os.name").equalsIgnoreCase("linux")){
  203. // System.out.println("操作系统:" + props.getProperty("os.name") + ", 非linux环境不运行定时任务");
  204. // return;
  205. // }
  206. // if (taskId.equalsIgnoreCase("101")) { // 定时任务1的业务处理-MES数据同步
  207. // logger.info("定时任务触发时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  208. // long start = System.currentTimeMillis();
  209. // ifMesEmsSwapfileService.synchronousMesAnEmsData(); // 获取MES投入产出数据
  210. // ifMesEmsProductorderService.synchronousMesAnEmsData(); // 获取MES生产计划数据
  211. // //usageDataService.getAutoSum(DateUtils.dateStr(new Date(start - 24*3600*1000L), "yyyy-MM-dd")); // 统计自动上传的能源上报数据(昨日)
  212. // //mesEmsDieseloilService.synchronousMesEmsData(); // 获取MES柴油数据
  213. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  214. // } else if (taskId.equalsIgnoreCase("102")) {
  215. // // 计量点定时任务
  216. // long start = System.currentTimeMillis();
  217. // tRmCalpointValueService.statData(new Date());
  218. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  219. // } else if (taskId.equalsIgnoreCase("103")) {
  220. // long start = System.currentTimeMillis();
  221. // tRmActValueService.statDataforDay();// 每日数据统计(实绩定时任务)
  222. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  223. // } else if (taskId.equalsIgnoreCase("104")) {
  224. // long start = System.currentTimeMillis();
  225. // tRmPlanValueService.state();// 每日数据统计(计划定时任务)
  226. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  227. // }
  228. if (taskId.equalsIgnoreCase("105")) {
  229. // 先计算产品,因为原材料计算单耗需要产品实绩数据参与计算
  230. long start = System.currentTimeMillis();
  231. ifMesEmsSwapfileService.getLgDateProd();
  232. try {
  233. ifMesEmsSwapfileService.getLgDateProd2();
  234. } catch (ParseException e) {
  235. e.printStackTrace();
  236. }
  237. tRmWorkprocProductValueService.setDate(new Date()); // 产品实绩产量、产品计划数据维护
  238. tRmWorkprocMaterialValueService.setDate(new Date()); // 原材料数据维护
  239. //新的业务逻辑,从报表获取数据。
  240. DateFormat dateForm = new SimpleDateFormat("YYYY-MM-dd");
  241. Calendar calendar=Calendar.getInstance();
  242. calendar.set(Calendar.HOUR_OF_DAY,-24);
  243. HashMap<String,Object> hs =new HashMap<>(4);
  244. hs.put("clock",dateForm.format(calendar.getTime()));
  245. hs.put("clocke", DateUtils.dateStr(new Date(),"YYYY-MM-dd"));
  246. tRmWorkprocProductValueService.getReportProductValue(hs);
  247. tRmWorkprocMaterialValueService.getMaterialValue(hs);
  248. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  249. }
  250. else if (taskId.equalsIgnoreCase("119")) {
  251. long start = System.currentTimeMillis();
  252. bfhydService.getBfhyds();
  253. try {
  254. tCm0348Service.getDate();
  255. } catch (ParseException e) {
  256. e.printStackTrace();
  257. }
  258. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  259. }
  260. else if (taskId.equalsIgnoreCase("122")) {
  261. long start = System.currentTimeMillis();
  262. tCm0318Service.getDataAsLg(DateUtils.getDate());
  263. tCm0318Service.getDataAsYlc(DateUtils.getDate());
  264. tCm0318Service.getDataAsYgx(DateUtils.getDate());
  265. tCm0318Service.getDataAsEgx(DateUtils.getDate());
  266. tCm0318Service.getDataAsFdc(DateUtils.getDate());
  267. tCm0318Service.getDataAsJjc(DateUtils.getDate());
  268. tCm0318Service.getDataAsWscj(DateUtils.getDate());
  269. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  270. }
  271. else if(taskId.equalsIgnoreCase("120")){
  272. long start = System.currentTimeMillis();
  273. // 获取大用户水系统数据、获取动力数据 5分钟一次
  274. emsWaterRealtimeService.getWaterListByArmMeter();
  275. emsWaterRealtimeService.dataGeneration();
  276. rttableService.dataGeneration();
  277. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  278. } else if(taskId.equalsIgnoreCase("121")){
  279. // 根据计量点配置生成动力数据、水数据 10分钟一次,传输到mosaic
  280. long start = System.currentTimeMillis();
  281. emsWaterRealtimeService.getWaterTenMinutes();
  282. rttableService.getPowerTenMinutes();
  283. try {
  284. emsWaterRealtimeService.getWaterListToMosaic();
  285. } catch (AxisFault e) {
  286. e.printStackTrace();
  287. } finally {
  288. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  289. }
  290. }
  291. else if(taskId.equalsIgnoreCase("116")){
  292. // 动力、水(计量点)小时表定时统计(自动补全24小时内的小时用量值)
  293. long start = System.currentTimeMillis();
  294. tRmCalpointService.statHourData();
  295. ////水计量点统计,获取水系统历史数据,弃用,数据不准确
  296. // emsWaterRealtimeService.getWaterTenMinutes();
  297. // emsWaterRealtimeService.getWaterHour();
  298. // emsWaterRealtimeService.getHistoryWaterToHour();
  299. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  300. }
  301. else if (taskId.equalsIgnoreCase("102")) {
  302. // 计量点定时任务(天)
  303. long start = System.currentTimeMillis();
  304. tRmCalpointValueService.statData(new Date());
  305. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  306. }
  307. else if (taskId.equalsIgnoreCase("123")) {
  308. // 大宗物料收发数据,从炼钢mes调度日报生成生产日报每天(0点57分,4点57分,8点57分)
  309. long start = System.currentTimeMillis();
  310. dzMaterialSfService.dataGeneration();
  311. emsScqkLgService.dateGenerate();
  312. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  313. }
  314. else if(taskId.equalsIgnoreCase("124")){
  315. long start = System.currentTimeMillis();
  316. try {
  317. foldTheValuesService.getData();
  318. foldTheValuesService.getDataForwl();
  319. Calendar c = Calendar.getInstance();
  320. if(c.get(Calendar.DATE) == 1){
  321. foldTheValuesService.getMonthData();
  322. }
  323. foldTheValuesService.insertNewDate();
  324. } catch (Exception e) {
  325. e.printStackTrace();
  326. }
  327. logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  328. }// else if (taskId.equalsIgnoreCase("106")) {
  329. // // 获取MES水质监测数据
  330. // long start = System.currentTimeMillis();
  331. // ifMesEmsWatermonitorService.synchronousMesAnEmsData();
  332. // // 生成人工录入监测数据
  333. // tCm0312Controller.getmanualdata();
  334. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  335. // } else if (taskId.equalsIgnoreCase("107")) { // 电统计(从南瑞到EMS)
  336. // long start = System.currentTimeMillis();
  337. // emsETimingSersvice.getData();
  338. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  339. // } else if (taskId.equalsIgnoreCase("108")) {
  340. // // 从IF_MES_EMS_WATERMONITOR表获取水质监测数据到T_CM_0318表
  341. // tCm0312Controller.synchronousData();
  342. // } else if (taskId.equalsIgnoreCase("109")) { // 电计量点班数据计算
  343. // tRmCalpointValueService.statDataShift(new Date());
  344. // } else if (taskId.equalsIgnoreCase("110")) {
  345. // // 能耗数据获取
  346. // ifEmsMesActitemService.getUploadData();
  347. // } else if (taskId.equalsIgnoreCase("111")) {
  348. // // 指标管理数据生成
  349. // long start = System.currentTimeMillis();
  350. // tRmIndexValueService.statData();
  351. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  352. // } else if (taskId.equalsIgnoreCase("112")) {
  353. // // 能源上报_晋泰信和
  354. // usageDataService.autoUpload();
  355. // } else if(taskId.equalsIgnoreCase("113")){
  356. // //平衡数据维护
  357. // long start = System.currentTimeMillis();
  358. // tRmBalanceValueService.state();
  359. // // 上报项目数据维护
  360. // tRmDwValueService.state(new Date());
  361. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  362. // } else if(taskId.equalsIgnoreCase("114")){
  363. // // 环保监控记录定时任务
  364. // zghbjkbzLogService.record();
  365. // } else if(taskId.equalsIgnoreCase("115")){
  366. // // 月初自动生成宾馆转供电记录
  367. // long start = System.currentTimeMillis();
  368. // tRmTransfereActValueService.createRecord();
  369. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  370. // } else if(taskId.equalsIgnoreCase("116")){
  371. // // 动力、水小时表定时统计(自动补全24小时内的小时用量值)
  372. // long start = System.currentTimeMillis();
  373. // tRmCalpointService.statHourData();
  374. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  375. // } else if(taskId.equalsIgnoreCase("117")){
  376. // // 财务投入产出数据生成
  377. // long start = System.currentTimeMillis();
  378. // tRmDfValueService.state(new Date());
  379. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  380. // } else if(taskId.equalsIgnoreCase("118")){
  381. // // 财务投入产出数据MES产量补全
  382. // long start = System.currentTimeMillis();
  383. // tRmDfValueService.state(new Date());
  384. // logger.info("定时任务"+taskId+"执行时长:" + (System.currentTimeMillis() - start)/1000 + "秒");
  385. // }
  386. }
  387. };
  388. }
  389. /**
  390. * 获取任务时间表达式
  391. * @param taskId
  392. * @return
  393. */
  394. public String getCron(String taskId) {
  395. String sql = "select cron from TASK_CONF t where id = ?";
  396. return jdbcTemplate.queryForObject(sql, new Object[]{taskId}, String.class);
  397. }
  398. }