淺析java中常用的定時任務框架-單體

一、閱讀收獲

1. 瞭解常用的單體應用定時任務框架

2. 掌握定時任務在單體中如何使用

二、本章源碼下載

本章源碼下載已分享github

三、Timer+TimerTask

  • 這是jdk自帶的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。
  • 使用這種方式可以讓你的程序按照某一個頻度執行,但不能在指定時間運行,一般用的較少。
/**
 * @Description: 1. Timer+TimerTask:(jdk自帶)
 * 這是java自帶的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。
 * 使用這種方式可以讓你的程序按照某一個頻度執行,但不能在指定時間運行。一般用的較少。
 * @Author: jianweil
 * @date: 2021/12/14 13:36
 */
public class TimerTest {
    public static void main(String[] args) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task  run:" + new Date());
            }
        };

        TimerTask timerTask2 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task2  run:" + new Date());
                //多線程並行處理定時任務時,Timer運行多個TimeTask時,隻要其中之一沒有捕獲拋出的異常,其它任務便會自動終止運行,使用ScheduledExecutorService則沒有這個問題。
                int i = 1/0;
            }
        };
        //idea會提示:使用ScheduledExecutorService代替Timer吧
        Timer timer = new Timer();
        System.out.println("begin:" + new Date());
        //安排指定的任務在指定的時間開始進行重復的固定延遲執行。這裡是延遲5秒開始執行,之後每3秒執行一次
        timer.schedule(timerTask, 5000, 3000);
        timer.schedule(timerTask2, 5000, 3000);
    }


}

多線程並行處理定時任務時,Timer運行多個TimeTask時,隻要其中之一沒有捕獲拋出的異常,其它任務便會自動終止運行,使用ScheduledExecutorService則沒有這個問題。

四、ScheduledExecutorService

ScheduledExecutorService也是jdk自帶的定時類,可以替代Timer

package com.ljw.springboottimer.scheduledExecutorservice;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 2. ScheduledExecutorService代替Timer(jdk自帶)
 * 多線程並行處理定時任務時,Timer運行多個TimeTask時,隻要其中之一沒有捕獲拋出的異常,其它任務便會自動終止運行,
 * 使用ScheduledExecutorService則沒有這個問題。
 * @Author: jianweil
 * @date: 2021/12/14 13:42
 */
public class ScheduledExecutorServiceTest {
    public static void main(String[] args) throws InterruptedException {

        //當所有的非守護線程結束時,程序也就終止瞭,同時會殺死進程中的所有守護線程。反過來說,隻要任何非守護線程還在運行,程序就不會終止。
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(false).build());
        System.out.println("begin:" + new Date());
        // 參數:1、任務體 2、首次執行的延時時間 3、任務執行間隔 4、間隔時間單位
        //延遲5秒執行,之後每3秒執行一次
        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                //do something
                System.out.println("begin:" + new Date());
            }
        }, 5, 3, TimeUnit.SECONDS);
    }
}

五、Spring Task

spring提供的類,可引入依賴:

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

開啟定時任務:@EnableScheduling

使用:在相應的任務方法前加上註解@Scheduled即可

5.1 單線程串行執行-@Scheduled

@Scheduled註解默認使同一個線程中串行執行,如果隻有一個定時任務,這樣做肯定沒問題,當定時任務增多,如果一個任務卡死,會導致其他任務也無法執行。

業務測試:

@Component
@EnableScheduling
public class SpringTaskTest {
    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用cron  {}" + (System.currentTimeMillis() / 1000));
    }
} 

5.2 多線程並發運行-@Scheduled+配置定時器的程池(推薦)

  • 解決單線程串行執行任務的問題,需要配置定時器的程池,推薦這種方法
  • 配置並註入一個TaskScheduler類bean即可
  • 配置定時器的線程池類如下:
package com.ljw.springboottimer.springtask;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @Description: 解決單線程串行執行 方式2:@Scheduled+配置定時器的線程池
 * @Author: jianweil
 * @date: 2021/12/14 14:44
 */
@Configuration
public class TaskSchedulerConfig {
    /**
     * 初始化瞭一個線程池大小為 5  的 TaskScheduler, 避免瞭所有任務都用一個線程來執行
     *
     * @return
     */
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(5);
        taskScheduler.setThreadNamePrefix("TaskSchedulerConfig-ljw");
        return taskScheduler;
    }
}

業務測試

@Component
@EnableScheduling
public class SpringTaskTest {

    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用cron  {}" + (System.currentTimeMillis() / 1000));
    }
    
    @Scheduled(fixedRate = 5000)
    public void run1() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用fixedRate  {}" + (System.currentTimeMillis() / 1000));
    }

} 

5.3 多線程並發執行-@Scheduled+@Async+配置異步線程池

解決單線程串行執行任務的問題,也可以結合異步註解@Async實現,但這種方法並不推薦,需要兩個註解,代碼編寫的工作量大

還可以解決fixedRate在遇到某些執行任務時間超過配置的時間隔,下次任務時間到瞭還要等待上次任務執行完成的情況,這是3.2不能解決的。

配置異步線程池類如下:

package com.ljw.springboottimer.springtask;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

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

/**
 * @Description: 解決單線程串行執行 方式1:@Scheduled+@Async+配置異步線程池
 * @Author: jianweil
 * @date: 2021/12/14 14:35
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    /**
     * 定義@Async默認的線程池
     * ThreadPoolTaskExecutor不是完全被IOC容器管理的bean,可以在方法上加上@Bean註解交給容器管理,這樣可以將taskExecutor.initialize()方法調用去掉,容器會自動調用
     *
     * @return
     */
    @Override
    public Executor getAsyncExecutor() {
        int processors = Runtime.getRuntime().availableProcessors();
        //常用的執行器
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //核心線程數
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        //線程隊列最大線程數,默認:50
        taskExecutor.setQueueCapacity(100);
        //線程名稱前綴
        taskExecutor.setThreadNamePrefix("AsyncConfig-ljw-");
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //執行初始化(重要)
        taskExecutor.initialize();
        return taskExecutor;
    }
}

業務測試需要加上@Async註解

@Component
@EnableScheduling
public class SpringTaskTest {

    @Scheduled(cron = "0/5 * * * * *")
    @Async
    public void run() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用cron  {}" + (System.currentTimeMillis() / 1000));
    }
    
    @Scheduled(fixedRate = 5000)
    @Async
    public void run1() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "=====>>>>>使用fixedRate  {}" + (System.currentTimeMillis() / 1000));
    }

} 

如果同時配置瞭3.2配置定時器的程池和3.3配置異步線程池,並且註解使用瞭@Scheduled+@Async,則定時任務使用的線程池為:配置異步線程池

5.4 @Scheduled參數解析

cron:通過cron表達式來配置任務執行時間(默認是fixedDelay)

initialDelay :定義該任務延遲多少時間才開始第一次執行

fixedRate:定義一個按一定頻率執行的定時任務。fixedRate 每次任務結束後會從任務編排表中找下一次該執行的任務,判斷是否到時機執行,fixedRate的任務某次執行時間再長也不會造成兩次任務實例同時執行,也要等到上次任務完成,判斷是否到時機執行,到就立即執行,與線程池無關,除非用瞭@Async註解,使方法異步,即是使用5.3步驟的配置。(5.2是配置線程池,達不到效果)

fixedDelay:定義一個按一定頻率執行的定時任務。fixedDelay總是在前一次任務完成後,延時固定時間長度然後再執行下一次任務

六、Quartz

在開發Quartz相關應用時,隻要定義瞭Job(任務),JobDetail(任務描述),Trigger(觸發器)和Scheduler(調度器),即可實現一個定時調度能力。

如果SpringBoot版本是2.0.0以後的,則在spring-boot-starter中已經包含瞭quart的依賴,則可以直接引入依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

6.1. 創建任務類

方式1:實現Job類的execute方法即可實現一個任務(推薦)

任務1如下:

package com.ljw.springboottimer.quartz.do1;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

/**
 * @Description: 我的定時任務-方法1
 * @Author: jianweil
 * @date: 2021/12/14 16:06
 */
public class MyTaskService1 implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println(Thread.currentThread().getName() + "------ Job ------" + new Date());
    }
}

方式2:繼承QuartzJobBean類重寫方法即可實現一個任務

任務2如下:

package com.ljw.springboottimer.quartz.do1;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * @Description: 我的定時任務-方法2
 * @Author: jianweil
 * @date: 2021/12/14 16:06
 */
public class MyTaskService2 extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println(Thread.currentThread().getName() + "---QuartzJobBean-----" + new Date());
    }
}

6.2. 配置任務描述和觸發器

配置類要分別要為每個任務聲明兩個bean

  • 1.JobDetail(任務描述)
  • 2.Trigger(觸發器)

配置調度器信息使用SimpleScheduleBuilder或者CronScheduleBuilder

package com.ljw.springboottimer.quartz.do1;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @Description: 每個任務都要兩步配置
 * 1.配置任務描述JobDetail 2. 配置觸發器Trigger
 * @Author: jianweil
 * @date: 2021/12/14 16:08
 */
@Configuration
public class QuartzConfig {

    /**
     * 創建任務1的 JobDetail1
     *
     * @return
     */
    @Bean
    public JobDetail teatQuartzDetail1() {
        return JobBuilder.newJob(MyTaskService1.class)
                //job的描述
                .withDescription("this is a job1")
                //job 的name和group
                .withIdentity("myTrigger1", "myTriggerGroup1")
                .storeDurably().build();
    }

    /**
     * 創建任務2的 JobDetail2
     *
     * @return
     */
    @Bean
    public JobDetail teatQuartzDetail2() {
        return JobBuilder.newJob(MyTaskService2.class)
                //job的描述
                .withDescription("this is a job2")
                //job 的name和group
                .withIdentity("myTrigger2", "myTriggerGroup2")
                .storeDurably().build();
    }

    /**
     * 創建任務1的 Trigger1
     *
     * @return
     */
    @Bean
    public Trigger testQuartzTrigger1() {
        //使用SimpleScheduleBuilder或者CronScheduleBuilder
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //設置時間周期單位秒
                .withIntervalInSeconds(10)
                .repeatForever();

        //兩秒執行一次,Quartz表達式,支持各種牛逼表達式
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");
        //任務運行的時間,SimpleSchedle類型觸發器有效,3秒後啟動任務
        long time = System.currentTimeMillis() + 3 * 1000L;
        Date statTime = new Date(time);


        return TriggerBuilder.newTrigger()
                .withDescription("")
                .forJob(teatQuartzDetail1())
                .withIdentity("myTrigger1", "myTriggerGroup1")
                //默認當前時間啟動
                .startAt(statTime)
                .withSchedule(cronScheduleBuilder)
                //.withSchedule(scheduleBuilder)
                .build();

    }

    /**
     * 創建任務2的 Trigger2
     *
     * @return
     */
    @Bean
    public Trigger testQuartzTrigger2() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //設置時間周期單位秒
                .withIntervalInSeconds(10)
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(teatQuartzDetail2())
                .withIdentity("myTrigger2", "myTriggerGroup2")
                .withSchedule(scheduleBuilder)
                .build();
    }

} 

以上就是淺析java中常用的定時任務框架-單體的詳細內容,更多關於java定時任務框架的資料請關註WalkonNet其它相關文章!

推薦閱讀: