Spring boot 定时任务

demo测试通过,使用版本如下: spring boot版本:1.5.10.RELEASE java版本:1.8 maven版本:apache-maven-3.3.9

方式一:

新建一个java类,添加注解@Configuration和@EnableScheduling,开启调度任务。在类中新建一个定时的方法添加注解@Scheduled ,表明该方法是一个调度任务。cron表达式配置定时执行的规则

@Configuration
@EnableScheduling
public class SpringBootScheduledController {
    /**
     *每20秒执行一次
     */
    @Scheduled(cron="0/20 * * * * ?")
    public void methodScheduling(){
        System.out.println("methodScheduling定时任务启动了");
    }
}

方法二:

在启动类上添加注解@EnableScheduling,开启调度任务。创建一个任务类添加注解@Component,类中方法添加注解@Scheduled,表明该方法是一个调度任务。

@SpringBootApplication
@EnableScheduling
public class SpringbootschedulingApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringbootschedulingApplication.class, args);
	}
}
/**
 * Created by fangzy on 2018/2/9 17:33
 */
@Component
public class ScheduledController {
    /**
     *每20秒执行一次
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void methodScheduling(){
        System.out.println("methodScheduling定时任务启动了");
    }
}
  • 定时规则参考:
    @Scheduled(cron=”0/10 * * * * ?”)//通过cron表达式定义执行规则, 每隔十秒执行1次 @Scheduled(fixedRate=1000)//上次开始执行时间点之后1秒再执行
    @Scheduled(fixedDelay=1000)//上次执行完毕时间点之后1秒再执行 @Scheduled(initialDelay=1000,fixedRate=1000)//第一次延迟1秒后执行,之后按fixedRate的规则每1秒执行1次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值