MesSyncTask.java
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.iot.scheduler.task;
import com.iot.scheduler.service.MesSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* MES设备状态同步定时任务
* 每分钟执行一次,将设备状态同步到MES系统
*/
@Slf4j
@Component
@ConditionalOnProperty(name = "mes.sync.enabled", havingValue = "true", matchIfMissing = true)
public class MesSyncTask {
@Autowired
private MesSyncService mesSyncService;
@Value("${mes.sync.cron:0 */1 * * * ?}")
private String cronExpression;
/**
* 定时任务:每分钟同步设备状态到MES
* 默认cron: (每分钟的第0秒执行)
*/
@Scheduled(cron = "${mes.sync.cron:0 */1 * * * ?}")
public void syncDeviceStates() {
log.info("[MesSyncTask] 开始执行设备状态同步任务...");
long startTime = System.currentTimeMillis();
try {
mesSyncService.syncDeviceStatesToMes();
} catch (Exception e) {
log.error("[MesSyncTask] 设备状态同步任务执行异常", e);
} finally {
long costTime = System.currentTimeMillis() - startTime;
log.info("[MesSyncTask] 设备状态同步任务结束,耗时: {}ms", costTime);
}
}
}