使用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;
        }
复制代码

 

posted @   党王  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示