转载

SpringBoot定时任务

由于项目需要,需要使用定时任务推送数据,而且有时候需要开启多个定时任务去跑,后面研究了下定时任务的操作技能,有两张,一种是异步的(推荐),线程安全,一种不是异步的,非线程安全,假如有一个挂了,会影响后面输出。

使用

前提说明

SpringBoot项目开启定时任务,需要在启动类上添加注解 @EnableScheduling ,不然的话,就算项目启动,定时任务还是不可以使用。

异步定时任务

异步配置类

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;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步开启定时任务配置
 *
 * @date 2020/6/24 15:46
 */
@Configuration
@EnableAsync //开启异步事件的支持
public class AsyncConfig {

    /**
     * 核心线程数
     */
    private int corePoolSize = 20;
    /**
     * 最大线程数
     */
    private int maxPoolSize = 220;
    /**
     * 队列最大长度
     */
    private int queueCapacity = 20;
    /**
     * 线程池维护线程所允许的空闲时间
     */
    private int keepAliveSeconds = 20;
    /**
     * 线程池对拒绝任务(无线程可用)的处理策略
     * <p>
     * rejectedExectutionHandler参数字段用于配置绝策略,常用拒绝策略如下
     * <p>
     * AbortPolicy:用于被拒绝任务的处理程序,它将抛出RejectedExecutionException
     * <p>
     * CallerRunsPolicy:用于被拒绝任务的处理程序,它直接在execute方法的调用线程中运行被拒绝的任务。
     * <p>
     * DiscardOldestPolicy:用于被拒绝任务的处理程序,它放弃最旧的未处理请求,然后重试execute。
     * <p>
     * DiscardPolicy:用于被拒绝任务的处理程序,默认情况下它将丢弃被拒绝的任务。
     */
    private ThreadPoolExecutor.AbortPolicy rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setRejectedExecutionHandler(rejectedExecutionHandler);
        executor.initialize();
        return executor;
    }
}
复制代码

启动定时任务

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
 * 异步开启定时任务
 * 
 * @date 2020/6/24 15:46
 */
@Component
@Async //启动异步调用
public class TinfoDemoScheduleTask {
    private static final Logger logger = LoggerFactory.getLogger(TinfoDemoScheduleTask.class);


    @Scheduled(cron = "0/1 * * * * *")
    public void scheduled() {
        logger.info("测试cron表达式,线程名称:{}", Thread.currentThread().getName());
    }

    @Scheduled(fixedRate = 1000)
    public void scheduledDemo1() {
        logger.info("测试fixedRate,线程名称:{}", Thread.currentThread().getName());
    }

    @Scheduled(fixedDelay = 1000)
    public void scheduledDemo2() {
        logger.info("测试fixedDelay,线程名称:{}", Thread.currentThread().getName());
    }
}

复制代码

Console

2020-06-24 15:51:17.506  INFO 17092 --- [ taskExecutor-1] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试fixedRate,线程名称:taskExecutor-1
2020-06-24 15:51:17.506  INFO 17092 --- [ taskExecutor-2] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试fixedDelay,线程名称:taskExecutor-2
2020-06-24 15:51:18.001  INFO 17092 --- [ taskExecutor-3] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试cron表达式,线程名称:taskExecutor-3
2020-06-24 15:51:18.496  INFO 17092 --- [ taskExecutor-4] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试fixedRate,线程名称:taskExecutor-4
2020-06-24 15:51:18.500  INFO 17092 --- [ taskExecutor-5] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试fixedDelay,线程名称:taskExecutor-5
2020-06-24 15:51:19.001  INFO 17092 --- [ taskExecutor-6] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试cron表达式,线程名称:taskExecutor-6
2020-06-24 15:51:19.496  INFO 17092 --- [ taskExecutor-7] c.l.u.i.demo.TinfoDemoScheduleTask       : 测试fixedRate,线程名称:taskExecutor-7
复制代码

非异步定时任务

启动定时任务

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TinfoDemoScheduleTaskTwo {
    private static final Logger logger = LoggerFactory.getLogger(TinfoDemoScheduleTaskTwo.class);


    @Scheduled(cron = "0/1 * * * * *")
    public void scheduled() {
        logger.info("测试cron表达式,线程名称:{}", Thread.currentThread().getName());
    }

    @Scheduled(fixedRate = 1000)
    public void scheduledDemo1() {
        logger.info("测试fixedRate,线程名称:{}", Thread.currentThread().getName());
    }

    @Scheduled(fixedDelay = 1000)
    public void scheduledDemo2() {
        logger.info("测试fixedDelay,线程名称:{}", Thread.currentThread().getName());
    }
}

复制代码

Console

2020-06-24 16:05:32.277  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试fixedRate,线程名称:scheduling-1
2020-06-24 16:05:32.282  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试fixedDelay,线程名称:scheduling-1
2020-06-24 16:05:33.002  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试cron表达式,线程名称:scheduling-1
2020-06-24 16:05:33.276  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试fixedRate,线程名称:scheduling-1
2020-06-24 16:05:33.283  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试fixedDelay,线程名称:scheduling-1
2020-06-24 16:05:34.002  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试cron表达式,线程名称:scheduling-1
2020-06-24 16:05:34.277  INFO 9400 --- [   scheduling-1] c.l.u.i.demo.TinfoDemoScheduleTaskTwo    : 测试fixedRate,线程名称:scheduling-1
复制代码
原文  https://juejin.im/post/5ef30b035188252e59619fa4
正文到此结束
Loading...