您的位置:首页 > 编程语言 > Java开发

springboot 计划任务

2018-02-24 16:11 776 查看
从spring3.1开始, 计划任务在spring中实现变得异常的简单,首先通过在配置类注解@EnableScheduling来开启计划任务的支持, 然后在要执行计划的方法上注解@Scheduled,声明这是一个计划任务。

spring通过@Scheduled支持多种类型的计划任务, 包含cron(指定时间执行),fixDelay, fixRate(没多长时间执行)

如@Scheduled(cron=”0 28 11 ? * *”)表示每天11点28分执行cron是类Unix系统下的定时任务。

package com.seengene.ar.conent.restful.restful.test;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
* @auther a123
* @create 2018-02-24 16:00
* @desc
*/
@Service
public class ScheduleTaskService {

@Scheduled(fixedRate = 3000)
public void test(){
System.out.println(System.currentTimeMillis());
}
}


package com.seengene.ar.conent.restful.restful.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* @auther a123
* @create 2018-02-24 16:01
* @desc
*/
@Configuration
@ComponentScan("com.seengene.ar.conent.restful.restful.test")
@EnableScheduling
public class ScheduleTaskConfig {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pringboot 计划任务