006 SpringBoot-整合定时任务详解(单点)

1.引入依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>

2.创建定时任务配置:

package com.cc.springboot.config;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling  //定时任务注解
public class TaskSchedulerConfig implements SchedulingConfigurer{

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //定时任务必须构建线程池
        taskRegistrar.setScheduler(schedulerThreadPool());
        
    }
    //构建线程池的方法,并设置shutdown关闭线程池
    @Bean(destroyMethod="shutdown")
    public Executor schedulerThreadPool() {
        // 使用自定义线程池可以,推荐使用ScheduledThreadPoolExecutor
        //Runtime.getRuntime().availableProcessors()当前系统允许的数量
        return new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 
                //创建一个线程工厂
                new ThreadFactory() {

                    @Override
                    public Thread newThread(Runnable r) {
                        // TODO Auto-generated method stub
                        Thread t = new Thread(r,"job-thread");
                        return t;
                    }
            
        }, 
        //最后拒绝策略
        new RejectedExecutionHandler() {
            
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                // TODO Auto-generated method stub
                System.out.println("当前任务执行失败:"+r);
                
            }
        });
    }

}

3.创建测试类:

package com.cc.springboot.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


//声明组件【@Service 标示应用服务,@Repository 标示持久层】
@Component
public class MyJob {
    
    //使用@Scheduled注释,initialDelay = 5000 初始化5秒启动,fixedDelay = 3000 每隔3秒启动
    //@Scheduled(initialDelay = 5000,fixedDelay = 3000)
    //指定细粒度的时间点去执行cron表达式,推荐使用cron表达式,比较灵活
    @Scheduled(cron = "0/10 * * * * *")
    public void job1() {
        System.out.println("--------定时任务1执行---------");
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值