使用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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏