首页 > 编程语言 > 详细

spring Boot定时任务

时间:2019-05-08 11:03:03      阅读:146      评论:0      收藏:0      [点我收藏+]

Spring boot 定时任务

开启定时任务注解

@SpringBootApplication(scanBasePackages = "org.mxn.sample")
@MapperScan("org.mxn.sample.sampledao.mapper")
@ServletComponentScan
@EnableTransactionManagement  //开启事务
@EnableScheduling //开启任务调度
public class SampleWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleWebApplication.class, args);
    }

}

编写定时任务

@Component
public class ScheduledTasks {

    public static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    /**
     * 通过在方法上加@Scheduled注解,表明该方法是一个调度任务
     * @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
     * @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
     * @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
     * @Scheduled(cron="0/3 * * * * ? ") :通过cron表达式定义规则,在线定义获取cron表达式 http://www.bejson.com/othertools/cron/
     */

    @Scheduled(cron = "0/3 * * * * ? ")
    public void reportCurrentTime(){
        //打印当前时间
        logger.info("ScheduledTasks.reportCurrentTime info:" + DateHelper.datetime(new Date(), DateHelper.DEFAULT_DATETIME_FORMAT));
    }
}

spring Boot定时任务

原文:https://www.cnblogs.com/menxn/p/10830405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!