springboot2 task scheduler 定时任务调度器四种方式

本文介绍了SpringBoot2中使用@EnableScheduling、XML配置、Bean创建工厂以及ThreadPoolTaskScheduler Runnable方式来实现定时任务调度。详细讲解了每种方式的配置和使用方法,并提供了相关代码示例。
摘要由CSDN通过智能技术生成

作者:o_cYj
博客专栏:
https://blog.csdn.net/o_cYj

项目git:
https://github.com/chenyingjun/springboot2-task
欢迎Star

使用@EnableScheduling方式

@Component
@Configurable
@EnableScheduling
public class Task1 {
    
    private static Log logger = LogFactory.getLog(Task1.class);
    @Scheduled(cron = "0/2 * * * * * ")
    public void execute() {
    
        logger.info("Task1>>" + new Date());
    }
}

xml配置方式

application启动加入读取xml文件

@SpringBootApplication
@ImportResource(value = {
     "classpath:applicationContext*.xml" })
public class Springboot2TaskApplication {
    

	public static void main(String[] args) {
    
		SpringApplication.run(Springboot2TaskApplication.class, args);
	}
}
<context:component-scan base-package="com.chenyingjun.task.schedual"></context:component-scan>
<task:scheduler id="appScheduler" pool-size="2" /> 
<!-- 调整定时任务 -->
<task:scheduled-tasks>    
	<task:scheduled ref="task2" method="method2" cron="0/10 * * * * ?"/>
</task:scheduled-tasks>
@Service
public class Task2 {
    
    private
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值