程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(3)

spring动态定时任务(超简单)

发布于2020-11-19 20:39     阅读(1087)     评论(0)     点赞(11)     收藏(5)



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Lazy(false)
@Component
@EnableScheduling
public class GenerateIndexTask implements SchedulingConfigurer {
    private static String CRON;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar){

        taskRegistrar.addTriggerTask(()->{
//            System.out.println("----定时开始----"+new Date().toString());
        },triggerContext->{
            //任务触发、可修改任务的执行周期
            CRON = "*/1 * * * * ?";//cron表达式 每秒触发
            CronTrigger trigger=new CronTrigger(CRON);
            return trigger.nextExecutionTime(triggerContext);
        });
    }

}

原文链接:https://blog.csdn.net/qq_44876173/article/details/109773487



所属网站分类: 技术文章 > 博客

作者:java之恋

链接:http://www.javaheidong.com/blog/article/990/811daeb50701d89ec18a/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

11 0
收藏该文
已收藏

评论内容:(最多支持255个字符)