Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 21 章 Spring boot with Scheduling

目录

21.1. 启用计划任务
21.1.1. Application.java
21.1.2. 配置
21.1.3. Component
21.2. 计划任务控制开关
21.3. @Scheduled 详解
21.3.1. fixedRate 案例
21.3.2. timeUnit
21.4. cron 表达式
21.4.1. 每3秒钟一运行一次
21.4.2. 凌晨23点运行
21.4.3. 周一 ~ 周五
21.5. Timer 例子
21.6. ScheduledExecutorService 例子

项目中经常会用到计划任务,spring Boot 中通过@EnableScheduling启用计划任务,再通过@Scheduled注解配置计划任务如果运行。

21.1. 启用计划任务

			
@EnableScheduling		
			
		

21.1.1. Application.java

Application.java

启用计划任务, 在Spring Boot启动类加上注解@EnableScheduling,表示该项目启用计划任务

			
package api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({ "api.config", "api.web", "api.rest", "api.service","api.schedule" })
@EnableMongoRepositories
@EnableJpaRepositories
@EnableScheduling
public class Application {

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

}
				
			

开启计划任务 @EnableScheduling

确保你的计划任务在 @ComponentScan 包中。

21.1.2. 配置

		
# 任务调度线程池大小 默认 1 建议根据任务加大
spring.task.scheduling.pool.size=1
# 调度线程名称前缀 默认 scheduling-
spring.task.scheduling.thread-name-prefix=scheduling-
# 线程池关闭时等待所有任务完成
spring.task.scheduling.shutdown.await-termination=
# 调度线程关闭前最大等待时间,确保最后一定关闭
spring.task.scheduling.shutdown.await-termination-period=		
		
			

21.1.3. Component

在计划任务方法上加上@Scheduled注解,表示该方法是一个计划任务,项目启动后会去扫描该注解的方法并加入计划任务列表。

			
			
package api.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	public final static long ONE_DAY = 24 * 60 * 60 * 1000;
	public final static long ONE_HOUR = 60 * 60 * 1000;
	
	public ScheduledTasks() {
	// TODO Auto-generated constructor stub
	}
	
	@Scheduled(fixedRate = 5000) //5秒运行一次调度任务
		public void echoCurrentTime() {
		log.info("The time is now {}", dateFormat.format(new Date()));
	}
	
	@Scheduled(fixedRate = ONE_DAY)
		public void scheduledTask() {
		System.out.println("每隔一天执行一次调度任务");
	}
	
	@Scheduled(fixedDelay = ONE_HOUR)
		public void scheduleTask2() {
		System.out.println("运行完后隔一小时就执行任务");
	}
	
	@Scheduled(initialDelay = 1000, fixedRate = 5000)
	public void doSomething() {
		// something that should execute periodically
	}
	
	@Scheduled(cron = "0 0/1 * * * ? ")
		public void ScheduledTask3() {
		System.out.println(" 每隔一分钟执行一次任务");
	}

}