提交 8974d8f9 编写于 作者: Q qinyingjie

fix:添加定时任务

上级 bda9f4ed
......@@ -2,7 +2,9 @@ package com.kwan.springbootkwan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class SpringBootKwanApplication {
......
package com.kwan.springbootkwan.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/**
* scheduler配置类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/9 14:01
*/
@Configuration
public class ScheduledTaskConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(2);
taskScheduler.initialize();
taskRegistrar.setTaskScheduler(taskScheduler);
}
}
package com.kwan.springbootkwan.schedule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* schedule测试 10s执行一次 记得在启动类或者Configuration类上添加了@EnableScheduling注解。
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/9 13:58
*/
@Component
@Slf4j
public class ScheduleTest {
@Scheduled(cron = "0/10 * * * * ?")
public void execute() {
log.info("Scheduled task is running ... ...");
}
}
package com.kwan.springbootkwan.schedule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* schedule测试
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/9 14:01
*/
@Component
@Slf4j
public class ScheduleTestTwo {
@Scheduled(cron = "0/10 * * * * *")
public void second() throws InterruptedException {
log.info("Second scheduled task is running... ...");
TimeUnit.SECONDS.sleep(5);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册