Schedule(定时任务)

2019-10-13  本文已影响0人  任未然

概述

虽然定时任务可以嵌⼊到web应⽤程序和WAR⽂件中,但下⾯演⽰⼀种更简单的⽅法创建了⼀个独⽴的应⽤程序。您将所有的内容打包在⼀个单⼀的、可执⾏的JAR⽂件中,⽤⼀个传统Java main()⽅法驱动。这也就是springboot的启动类。

一. 入门案例

1.1 主程序入口配置

package com.qianfeng.test.task;

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

@SpringBootApplication
@EnableScheduling //开启定时调度
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

1.2 任务类代码示例

@Component
public class MyScheduled {
    
    /**
     *  fixedRate: 固定时间去执行。
     *  fixedDelay: 固定的延迟。
     *
     *  cron有六个站位符号: 第一个表示秒,第二个是分,第三个小时,第四是日,
     *   第五个是月份,第六个是星期
     *   0/2 * * * * *  每两秒执行一次
     *   0 25 11 * * * 每天11:25执行
     *   0 0/1 11 * * *  每天的11点,每隔5分钟执行一次
     *   0 0 20 * * *  每天晚上8点钟执行
     *   0 0 8,20 * * * 每天早晚8点执行一次
     *   0 0 8-20 * * * 每天早上8点到晚8点,每个小时执行一次
     *   0 0 12 L * * * 每个月的最后一天12钟执行。
     */
    @Scheduled(fixedRate = 5000)
    public void test1() {
        System.out.println("定时任务触发了");
    }
}

⼀张图来说明(任务执⾏⻓度超过周期的情况):

二. 参数介绍

"0 0 * * * *" = 每天每时整点
"*/10 * * * * *" = 每⼗秒(10:20:00, 10:20:10, 10:20:20 ...)触发
"0 0 8-10 * * *" = 每天早上8:00、9:00 和 10:00 触发
"0 0 6,19 * * *" = 每天6:00 和 19:00 触发
"0 0/30 8-10 * * *" = 每天8:00, 8:30, 9:00, 9:30, 10:00 和 10:30 触发
"0 0 9-17 * * MON-FRI" = 朝九晚五(周⼀⾄周五9:00-17:00的整点)触发
"0 0 0 25 12 ?" = 圣诞节(每年的12⽉25⽇00:00)触发
"0 15 10 L * ?" = 每⽉最后⼀⽇的上午10:15触发
"0 15 10 ? * 6L" = 每⽉的最后⼀个星期五上午10:15触发
"0 15 10 ? * 6#3" = 每⽉的第三个星期五上午10:15触发 1234567891011

三. 并⾏执⾏任务

上⾯的方式中,如果我们开启多个定时任务,他们会使⽤同⼀个线程开启任务,可能会因为某个任务阻塞而导致执⾏失败,所以我们可以使⽤多线程并⾏执⾏任务,只需要添加⼀个线程池即可

3.1 并行类

@Component
public class MyScheduleConfig implements SchedulingConfigurer {


    public Executor getExecutor() {
        return Executors.newScheduledThreadPool(3); //开启特定的任务线程,开启3个
    }

    //
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(getExecutor());
    }
}

3.2 任务类

@Component
public class MySchedule {

    @Scheduled(cron = "0/5 * * * * *")
    public void doSomething() {
        System.out.println("每五秒执行的任务线程名:" + Thread.currentThread().getName());
        System.out.println("定时任务开始................");
    }

    @Scheduled(cron = "0/3 * * * * *")
    public void doSomething1() {
        System.out.println("每3秒执行的任务线程名:" + Thread.currentThread().getName());
        System.out.println("定时任务每3秒执行一次次,任务开始执行...");
    }

    @Scheduled(cron = "0/10 * * * * *")
    public void doSomething2() {
        System.out.println("每10秒执行的任务线程名:" + Thread.currentThread().getName());
        System.out.println("定时任务每天早8点到晚8点,每20分钟执行一次,开始");
    }

}

四、定时操作

有时候需要设定制定时间执行方法, 例如30分钟超时操作,可以用一下方法

public class ScheduleTaskTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(3);
        /**
         * 参数一:执行的方法
         * 参数二:多久后执行一次(只执行一次)
         * 参数三:事件单位。
         */
        ScheduledFuture<String> schedule = ses.schedule(() -> {
            System.out.println("5s一次");
            return "hello";
        }, 5, TimeUnit.SECONDS);
        System.out.println(schedule.get());
  }
}

ScheduledExecutorService接口方法说明

/**
 * 创建并执行在给定延迟后启用的一次性操作
 *
 * @param command 要执行的任务 
 * @param delay 从现在开始延迟执行的时间 
 * @param unit 延时参数的时间单位 
 * @return 表示任务等待完成,并且其的ScheduledFuture get()方法将返回 null完成后 
 * @throws RejectedExecutionException 如果任务无法安排执行 
 * @throws NullPointerException 如果命令为空 
 */
public ScheduledFuture<?> schedule(Runnable command,
                                   long delay, TimeUnit unit);

/**
 * 创建并执行在给定延迟后启用的ScheduledFuture。 
 *
 * @param callable 执行的功能 
 * @param delay 从现在开始延迟执行的时间 
 * @param unit 延迟参数的时间单位 
 * @param <V> the 可调用结果的类型 
 * @return一个可用于提取结果或取消的ScheduledFuture 
 * @throws RejectedExecutionException 如果该任务无法安排执行 
 * @throws NullPointerException 如果callable为空 
 */
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                       long delay, TimeUnit unit);

/**
 * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在initialDelay后开始执行,然后在 
 * initialDelay+period后执行,接着在initialDelay + 2 * period后执行,依此类推。 
 * 如果任务的执行遇到异常,则后续的执行被抑制。 否则,任务将仅通过取消或终止执行人终止。
 * 如果任务执行时间比其周期长,则后续执行可能会迟到,但不会同时执行。 
 *
 * @param command 要执行的任务 
 * @param initialDelay 首次执行的延迟时间
 * @param period 连续执行之间的周期
 * @param unit initialDelay和period参数的时间单位 
 * @return 一个ScheduledFuture代表待完成的任务,其 get()方法将在取消时抛出异常 
 * @throws RejectedExecutionException 如果任务无法安排执行 
 * @throws NullPointerException 如果命令为空 
 * @throws IllegalArgumentException 如果period小于或等于零 
 */
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit);

/**
 * 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
*  如果任务的执行遇到异常,则后续的执行被抑制。 否则,任务将仅通过取消或终止执行人终止。 
 *
 * @param command 要执行的任务 
 * @param initialDelay 首次执行的延迟时间
 * @param delay 一次执行终止和下一次执行开始之间的延迟
 * @param unit initialDelay和delay参数的时间单位
 * @return 表示挂起任务完成的ScheduledFuture,并且其get()方法在取消后将抛出异常
 * @throws RejectedExecutionException 如果任务不能安排执行 
 * @throws NullPointerException 如果command为null
 * @throws IllegalArgumentException 如果delay小于等于0
 */
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit);
上一篇 下一篇

猜你喜欢

热点阅读