package com.kun.seckill.scheduled;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author zhoukun 86547462@qq.com
* @version 1.0
* @date 2021/1/10 13:15
* @EnableScheduling//开启定时任务
*/
/**
* 定时任务:
* @EnableScheduling//开启定时任务
* @Scheduled//开启一个定时任务
* 配置类:TaskSchedulingAutoConfiguration
* 异步任务:
* @EnableAsync//开启异步任务功能
* @Async给需要异步的方法上添加注解
* 配置类:TaskExecutionAutoConfiguration
* spring.task.execution.pool.core-size: 5
* spring.task.execution.pool.max-size: 50
*/
@Component
@EnableScheduling//开启定时任务
@Slf4j
@EnableAsync
public class HelloScheduled {
/**
* cron表达式
* https://cron.qqe2.com/
* con 从0秒开始,每2秒执行一次
* 注意:spring中6位,不存在年
* 定时任务不应该阻塞,默认是阻塞的,解决方法
* 1,可以让方法异步执行,自己提交到自己的线程池
* 2.支持定时任务线程池,配置
* spring.task.scheduling.pool.size: 5
* 3.让定时任务异步执行
* @EnableAsync//开启异步任务功能
* @Async给需要异步的方法上添加注解
*/
@Async
@Scheduled(cron = "0/2 * * * * ? ")
public void helloSchd()
{
log.info("======>hello");
}
}
定时任务和异步任务测试
最新推荐文章于 2024-05-17 04:49:33 发布