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注解。

猜你喜欢

转载自blog.csdn.net/feinifi/article/details/119669198