Springboot 2.0.* 及低版本定时任务@Scheduled多线程配置

1. 首先在springboot启动类上添加 @EnableScheduling 注解。java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class KittyApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(KittyApiApplication.class, args);
    }

}

2. 将任务类注册到spring容器,而后在其任务方法上添加 @Scheduled 注解,@Scheduled有多个属性:linux

 ① cron:cron表达式,指定任务在特定时间执行;web

 ② fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;spring

 ③ fixedDelayString:与fixedDelay含义同样,只是参数类型变为String;apache

 ④ fixedRate:表示按必定的频率执行任务,参数类型为long,单位ms;centos

 ⑤ fixedRateString: 与fixedRate的含义同样,只是将参数类型变为String;api

 ⑥ initialDelay:表示延迟多久后第一次执行任务,参数类型为long,单位ms;tomcat

 ⑦ initialDelayString:与initialDelay的含义同样,只是将参数类型变为String;springboot

 ⑧ zone:时区,默认为当前时区,通常没有用到。多线程

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 
 * @version 2019/4/15 下午 05:00
 */
@Slf4j
@Service
public class TimedTaskServiceImpl {

    @Scheduled(cron = "*/10 * * * * ?")
    public void task01() {
        log.info("task01: " + System.currentTimeMillis());
    }

    @Scheduled(cron = "*/10 * * * * ?")
    public void task02() {
        log.info("task02: " + System.currentTimeMillis());
    }

    @Scheduled(cron = "*/10 * * * * ?")
    public void task03() {
        log.info("task03: " + System.currentTimeMillis());
    }
}

3. springboot 2.0 使用的是spring framework 5.0,在spring 3.0 中就引入了TaskScheduler接口进行异步执行和任务调度的抽象,spring默认是以单线程执行任务调度,如下展现如何设置多线程在2.1及之前的版本。

(1)springboot 2.1.*之后能够直接经过在properties中经过属性配置:

# 线程池大小 
spring.task.scheduling.pool.size=10 
# 线程名前缀 
spring.task.scheduling.thread-name-prefix=task-pool-

(2)springboot 2.0及之前就须要实现 SchedulingConfigurer 接口,点到@EnableScheduling注解中,能够看到有SchedulingConfigurer接口、SchedulingTaskRegistrar类和ScheduledAnnotationBeanPostProcessor类等。

SchedulingConfigurer接口内容以下,只有一个抽象方法。

@EnableScheduling 注解的注释里也给出了例子,实现这个SchedulingConfigurer接口就能够实现多线程定时任务。值得注意的是最后一段,将线程池交由spring容器管理,指定销毁回调在Bean销毁时调用线程池的shutdown方法,保证spring容器关闭前销毁线程池中的线程,防止线程未终结而驻留。(若是不指定在linux tomcat中可能会使tomcat进程没法经过shutdown命令关闭,致使内存泄露)

实现SchedulingConfigurer接口的configureTasks()。

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

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    @Bean(destroyMethod="shutdownNow")
    public ScheduledExecutorService taskExecutors() {
        return Executors.newScheduledThreadPool(10);
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        //参数传入一个线程池
        scheduledTaskRegistrar.setScheduler(taskExecutors());
    }

}

在项目部署到tomcat中,若是按照例子中建立线程池须要指定销毁方法为shutdownNow,shutdown会继续执行而且完成全部未执行的任务,shutdownNow 会清除全部未执行的任务而且在运行线程上调用interrupt() 。

ps:博主因为刚开始未设置销毁方法为shutdownNow,在centos上执行tomcat的shutdown命令没法结束tomcat进程,tomcat结束线程在结束spring容器时,没法结束线程池中线程,线程池中线程在spring容器销毁后还未被杀死,从而致使tomcat进程一直驻留,错误信息以下:(web应用启动了一个线程,但未能中止它,这极可能形成内存泄漏)

16-May-2019 23:19:15.018 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
16-May-2019 23:19:15.019 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
16-May-2019 23:19:15.070 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
16-May-2019 23:19:15.121 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
16-May-2019 23:19:15.240 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [kittyapi] appears to have started a thread named [pool-2-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 java.lang.Thread.run(Thread.java:748)

以上是jdk的Executors建立的线程池,也可使用spring的线程池ThreadPoolTaskScheduler(ScheduledThreadPoolExecutor的包装)来建立线程池,具体以下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;

@Slf4j
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    /**
     * 定时任务线程池
     *
     * @return
     */
    @Bean("scheduledThreadPoolExecutor")
    public Executor scheduledThreadPoolExecutor() {
        log.info("start scheduledThreadPoolExecutor");
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 配置核心线程数
        scheduler.setPoolSize(10);
        return scheduler;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        // 参数传入一个线程池
        scheduledTaskRegistrar.setScheduler(scheduledThreadPoolExecutor());
    }

}

ThreadPoolTaskScheduler类的继承和实现关系以下:

ThreadPoolTaskScheduler是TaskScheduler接口的实现类,它的父类ExecutorConfigurationSupport重写了销毁方法destroy( )和shutdown( ),经过查看源码能够看到,若是不设置等待任务在关闭容器时完成(waitForTasksToCompleteOnShutdown = true),那么就默认调用了ScheduledThreadPoolExecutor(后续解释为何是这个类)类的shutdownNo方法

ThreadPoolTaskScheduler类是spring对jdk中ScheduledThreadPoolExecutor类的包装,当建立一个ThreadPoolTaskScheduler对象的bean时,它的内部就已经自动建立了一个默认池大小为1的ScheduledThreadPoolExecutor线程池对象,要注意是在spring容器注册bean时才会去初始化内部的线程池。

下面来看ScheduledTaskRegistrar类,能够经过它里面的方法设置TaskScheduler的子类也就是ThreadPoolTaskScheduler类,因此在实现SchedulingConfigurer接口时能够经过调用ThreadPoolTaskScheduler中的set***方法注入外部线程池。

最后看看ScheduledAnnotationBeanPostProcessor这个类,它内部实例化了一个ScheduledTaskRegistrar对象。

接着往下看,在完成注册的方法里,若是有对象有TaskScheduler或者ScheduledExecutorService对象那么直接使用该对象,往下走若是知足条件定时线程池为null,那么就调用BeanFactory先使用class类型去获取容器中的TaskScheduler的子类,若是容器中存在多个TaskScheduler类型的bean,那么使用默认bean名称去获取,默认名称就是taskScheduler。

接着往下走,能够看到,若是刚开始没有设置TaskScheduler线程池,而且容器中也没有注册TaskScheduler线程池对象,那么ScheduledTaskRegistrar会建立一个单线程线程池来执行定时任务。

总结以上,就会就会发现多线程实现只须要直接在spring容器中注册一个TaskScheduler子类也就是ThreadPoolTaskScheduler就能够了(若是存在多个,要指定bean名称为taskScheduler,spring默认使用此名称的bean,这里只是设置了线程数量,其余属性可根据具体需求设置),就不用去实现SchedulingConfigurer接口了。若是定时任务较多复杂,建议集成Quartz

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Configuration
public class ExecutorConfig {

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        return scheduler;
    }
}
相关文章
相关标签/搜索