SpringCronTask.java 19 KB

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