Spring Boot中的Scheduled定时任务

Spring Boot中的Scheduled定时任务

1. Scheduled如何使用

在项目中大家经常会遇到这样的需求:每天早晨8点统计昨日历史订单数据,生成日报信息。
那么如何实现呢,在JavaEE项目中实现方式其实有很多种,今天主要讲的就是Scheduled,他是Spring3.0加入的新功能,使用起来非常简单。

@EnableScheduling
@SpringBootApplication
public class ScheduledTaskApplication {

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

	@Scheduled(fixedRate = 5000)
	public void fixRateWork() {
   			 System.out.println("每隔五秒钟执行一次: " + System.currentTimeMillis());
	}
	
    @Scheduled(cron = "0/1 * * * * ?")
    public void cronWork() throws InterruptedException {
        System.out.println("每隔1秒钟执行一次: " + System.currentTimeMillis());
    }
}

上面就是基本使用姿势,2个注解@EnableScheduling和 @Scheduled必须要有,其中 @EnableScheduling 注解的作用是发现注解@Scheduled的任务并后台执行。
2种设置时间间隔方式:1.@Scheduled(fixedRate = 5000),2.@Scheduled(cron = “* * * * * *”),根据需要选择其一即可。

2. @Scheduled中的cron表达式

cron表达式还是比较强大的,其中的每一个 * 都可以换成具体的参数值,但必须是cron表达式已经定义好的那些值,简单的参数却能实现强大的功能

参数说明
* 第一位,表示秒,取值 0-59
* 第二位,表示分,取值 0-59
* 第三位,表示小时,取值 0-23
* 第四位,日期,取值 1-31
* 第五位,月份,取值 1-12
* 第六位,星期几,取值 1-7
* 第七位,年份,可以留空,取值 1970-2099

(*) 星号:可以理解为“每”的意思,每秒、没分
(?) 问号:只能出现在日期和星期这两个位置,表示这个位置的值不确定
(-) 表达一个范围,如在小时字段中使用 10-12 ,表示从10点到12点
(,) 逗号,表达一个列表值,如在星期字段中使用 1,2,4 ,则表示星期一、星期二、星期四
(/) 斜杠,如 x/y ,x是开始值,y是步长,如在第一位(秒)使用 0/15,表示从0秒开始,每15秒
举个例子
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 
0 0 12 ? * WED 表示每个星期三中午12点 
"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

3. @Scheduled并行执行任务

Springboot本身默认的执行方式是串行执行,也就是说无论有多少task,都是一个线程串行执行,可以通过配置实现并行任务。

并行任务

继承SchedulingConfigurer类并重写其方法即可,如下:

@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }
 
    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }
}
异步并行任务
@Configuration
@EnableScheduling
@EnableAsync(mode = AdviceMode.PROXY, proxyTargetClass = false,order = Ordered.HIGHEST_PRECEDENCE)
@ComponentScan(basePackages = "hello")
public class RootContextConfiguration implements AsyncConfigurer, SchedulingConfigurer {
	@Bean
	public ThreadPoolTaskScheduler taskScheduler(){
			ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
			scheduler.setPoolSize(20);
			scheduler.setThreadNamePrefix("task-");
			scheduler.setAwaitTerminationSeconds(60);
			scheduler.setWaitForTasksToCompleteOnShutdown(true);
			return scheduler;
	}
 
	@Override
	public Executor getAsyncExecutor(){
			Executor executor = this.taskScheduler();
			return executor;
	}
 
	@Override
	public void configureTasks(ScheduledTaskRegistrar registrar){
			TaskScheduler scheduler = this.taskScheduler();
			registrar.setTaskScheduler(scheduler);
	}
}

Application中添加如下代码

@EnableScheduling
@SpringBootApplication
public class ScheduledTaskApplication {
 
    	public static void main(String[] args) throws Exception {
     			AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
 				rootContext.register(RootContextConfiguration.class);
   				rootContext.refresh();
    	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值