springboot定时任务Scheduled注解的使用

    springboot提供的定时任务,使用起来很方便,不需要额外的依赖,重点在注解@Scheduled的使用,另外一个很重要的地方,就是在启动类上需要加入注解@EnableScheduling。

    直接show me the code:

    定义一个定时任务,并通过cron参数设置触发时间,本示例是表示两秒钟执行一次。

package com.xxx.springboot.task;

import java.util.Date;

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

@Component
public class DemoTask {
	
	@Scheduled(cron = "0/2 * * * * ?")
	public void task() {
		Date date = new Date();
		System.out.println("task run..."+date.toString());
	}
}

    启动类上添加@EnableScheduling注解:

package com.xxx.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class App {
    public static void main( String[] args ){
    	SpringApplication.run(App.class, args);
    }
}

   运行程序,查看打印信息:

task run...Fri Aug 13 23:35:06 CST 2021
task run...Fri Aug 13 23:35:08 CST 2021
task run...Fri Aug 13 23:35:10 CST 2021

   结果显示,两秒钟执行一次任务。

    这里需要注意的是cron参数默认是需要六位表示时间,分别是:秒 分 时 日 月 星期。而这个表达式跟我们的linux shell crontab的表达式有明显的区别,crontab只有五位,第一位表示秒的没有,其他几位都相同。

   如果你搞不清楚cron表达式,可以选择fixedRate参数,这个只需要给出一个时间间隔即可。

package com.xxx.springboot.task;

import java.util.Date;

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

@Component
public class DemoTask {
	
	/*
	@Scheduled(cron = "0/2 * * * * ?")
	public void task() {
		Date date = new Date();
		System.out.println("task run..."+date.toString());
	}*/
	
	@Scheduled(fixedRate = 2000)
	public void task2() {
		Date date = new Date();
		System.out.println("task2 run..."+date.toString());
	}
}

   打印结果,同样是间隔2秒运行一次任务:

task2 run...Fri Aug 13 23:44:42 CST 2021
task2 run...Fri Aug 13 23:44:44 CST 2021
task2 run...Fri Aug 13 23:44:46 CST 2021
task2 run...Fri Aug 13 23:44:48 CST 2021

  前面说了,定时任务执行的一个条件是,需要在启动类上开启注解@EnableScheduling。

   如果,你不想这么干,可以编写一个配置,然后同样的给出注解。

package com.xxx.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer{

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
		taskScheduler.setPoolSize(4);
		taskScheduler.initialize();
		taskRegistrar.setScheduler(taskScheduler);
	}
	
}

启动类变为这个样子:

package com.xxx.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@EnableScheduling
public class App {
    public static void main( String[] args ){
    	SpringApplication.run(App.class, args);
    }
}

总而言之,@EnableScheduling注解少不了。如果你的定时任务没有执行,可以检查一下是否少了@EnableScheduling注解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值