SpringCronTask.java 19 KB

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