- 第一需要配置启动类注解开启定时任务功能(开启才可以使用)
@EnableScheduling
public class Application {...}
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;
/**
* @ClassName:AsyncConfig
* @Description: * @Configuration:表明该类是一个配置类
* * @EnableAsync:开启异步事件的支持
* 在定时任务的类或者方法上添加@Async 。
* 最后重启项目,每一个任务都是在不同的线程中
* //------------------------------------------
* 项目中使用方法:
* 1.启动类中添加 @EnableScheduling 注解开启定时
* 2.业务类中添加 @Async(也可在方法中添加) 支持多线程
* @Component 注解
* 3.定时处理方法中添加 @Scheduled 使用 cron 表达式 如 每天一点十分执行:"0 10 1 * * *"
* 秒 0~59 , - * /
* 分 0~59 , - * /
* 小时 0~23 , - * /
* 日期 1-31 , - * ? / L W C
* 月份 1~12或者JAN~DEC , - * /
* 星期 1~7或者SUN~SAT , - * ? / L C #
* 年(可选) 留空,1970~2099 , - * /
* @Author:lids
* @CreateDate: 2018/6/5 9:58
*/
@Configuration
@EnableAsync
public class AsyncConfig {
/**
* 此处成员变量应该使用@Value从配置中读取
* corePoolSize:如果池中的