摘要:项目中需要定时统计数据,发邮件,清理数据等操作。这时候就要用到定时任务,SpringBoot中,创建定时任务比较简单,具体步骤如下:
1、开启定时任务:
在程序的入口类中添加@EnableScheduling注解
package com.bonc.energymanagement;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication//(exclude = {DataSourceAutoConfiguration.class})
@ServletComponentScan(basePackages = "com.xx")
@MapperScan("com.xx.energymanagement.dao") //扫描dao
@EnableScheduling //开启定时任务注解
public class EnergymanagementApplication {
public static void main(String[] args) {
SpringApplication.run(EnergymanagementApplication.class, args);
}
}
2、创建定时任务:
单独创建一个类,用来存放定时任务,然后在每个定时任务方法上,用注解标明定时任务的执行周期。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Description:定时任务
*/
@Component
public class ScheduledTask {
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* 每间隔10秒输出时间
*
* @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
* @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
* @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,
* 之后按fixedRate的规则每5秒执行一次
* @Scheduled(cron="*/5*****") :通过cron表达式定义规则
*/
@Scheduled(fixedRate = 2000)
@Async
public void logTime() {
logger.info("定时任务,现在时间1-:" + System.currentTimeMillis());
}
//每隔5秒执行
@Scheduled(fixedRate = 5000)
@Async
public void logTime3() {
logger.info("定时任务,现在时间3-:" + System.currentTimeMillis());
}
//每天下午14点执行
@Scheduled(cron = "0 0 14 * * *")
@Async//该注解保证每个定时任务一个线程
public void logTime2() {
logger.info("定时任务,现在时间2:" + System.currentTimeMillis());
}
}
3、运行项目:
如果不加@Async注解,定时任务可以执行,并且在同一个线程中串行执行,如果只有一个定时任务,这样做肯定没问题,当定时任务增多,如果一个任务卡死,会导致其他任务也无法执行。所以这里用@Async注解和线程池保证每个定时任务在不同的线程里执行。
4、多线程执行:
在SpringBoot项目中一般使用config配置类的方式添加配置,所以新建一个AsyncConfig类。
package com.bonc.energymanagement.task;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* 通过多线程的方式执行定时任务,使每一个任务都是在不同的线程中
* 在定时任务的类或者方法上添加@Async 。
*/
@Configuration //表明该类是一个配置类
@EnableAsync //开启异步事件的支持
public class AsyncConfig {
/*
此处成员变量应该使用@Value从配置中读取
*/
private int corePoolSize = 10;
private int maxPoolSize = 200;
private int queueCapacity = 10;
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
}
}
运行项目,发现每个定时任务都在不同的线程里执行。