使用spring boot自身定时任务。
1,在主类添加注解@EnableScheduling
@EnableDiscoveryClient //开启后才能接收到mqtt消息 @SpringBootApplication @EnableTransactionManagement //开启事务 @EnableScheduling public class RuoYiApplication { public static void main(String[] args) { ParserConfig.getGlobalInstance().setSafeMode(true); //处理 fastjson漏洞 System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication application = new SpringApplication(RuoYiApplication.class); application.setApplicationStartup(new BufferingApplicationStartup(2048)); application.run(args); System.out.println("(♥◠‿◠)ノ゙ 广核启动成功 ლ(´ڡ`ლ)゙"); }
2,在执行的方法的类上添加注解
@Component
@EnableScheduling
@EnableAsync
在方法上添加
@Scheduled(fixedRate = 2000)
@Component @EnableScheduling @EnableAsync @Log4j2 @RequiredArgsConstructor @PropertySource("classpath:config/path.properties") public class SensorMonitor { private final RedisTemplate<String, String> redisTemplate; @Value("${SensorStatusTimeout}") private long timeout; private final Rpc1PointMapper PointMapper; // public void startMonitoring() { // Timer timer = new Timer(true); // timer.scheduleAtFixedRate(new TimerTask() { // @Override // public void run() { // checkSensors(); // } // }, 0, 60 * 60 * 3); // 每1秒*60*60*3检查一次 ,每3小时监测一次 // } // @Scheduled(fixedRate =1000* 60 * 60 * 3 ) // 每3小时执行一次 // @Scheduled(fixedRate =1000* 60 * 5 ) // 每3小时执行一次 // @Scheduled(fixedRate = 60000) // 300000毫秒 = 5分钟 // @Scheduled(cron = "0/10 * * * * *") @Scheduled(fixedRate = 2000) private void checkSensors() { long currentTime = System.currentTimeMillis(); log.info("——————————————————————启动执行命令——————————————————————————————————————————————————————————————————————————————————————"); String macPattern = "SensorStatus:*"; Set<String> keys = redisTemplate.keys(macPattern); // 如果没有找到任何键,直接返回 if (keys == null || keys.isEmpty()) { return; }

浙公网安备 33010602011771号