0


springboot:定时任务


定时任务在项目中的应用:

  • 每日凌晨对前一日的数据进行汇总
  • 定时清理系统缓存
  • 对每日的数据进行分析和总结
  • 银行月底汇总账单
  • 月底话费账单
  • 订单在30分钟内未支付会自动取消
  • 文章的缓存更新

一、实现定时任务的方法一:基于JDK

方法一:使用JDK自带的Timer类

优点:

  • 使用方便

缺点:

  • 该类是单线程实现,如果任务执行时间太长或者发生异常,会影响其他任务的执行

Timer类有三种重载方法:

  • schedule(TimerTask task,long delay) :延迟delay毫秒再执行任务
  • schedule(TimerTask task,Date time) :在特定的time时间执行任务:
  • schedule(TimerTask task,long delay,long period) :延迟delay毫秒并每隔period毫秒执行一次
package com.study.demo.dingshi;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
    //定义时间格式
    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");

    public static void main(String[] args) {
        Timer timer=new Timer();
        //从当前时刻开始,每1s执行一次,单位为毫秒
        timer.schedule(new MyTask(),0,1000);
    }
    //自定义的、定时要执行的任务
    private static class MyTask extends TimerTask {
        @Override
        public void run() {
            LocalDateTime now=LocalDateTime.now();
            System.out.println("这是定时任务,时间是:"+pattern.format(now));
        }
    }
}

法二:使用ScheduleExecutorsService类

特点:使用线程池技术,可实现线程复用

package com.study.demo.dingshi;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceDemo {
    //定义时间格式
    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");

    public static void main(String[] args) {
        ScheduledExecutorService service= Executors.newScheduledThreadPool(1);
        service.scheduleAtFixedRate(() -> {
            LocalDateTime now =LocalDateTime.now();
            System.out.println("schedule这是定时任务,时间是:"+pattern.format(now));
        },0,1000, TimeUnit.MILLISECONDS);
    }
}

二、基于Spring Task实现定时任务(推荐使用)

  • @EnableScheduling:开启定时任务
  • @Component:将类标记为一个被Spring管理的功能组件
  • @Scheduled(。。。): 自定义定时任务的相关配置,详查http://t.csdn.cn/CnBYy - fixedDelay- cron- fixedRate
package com.study.demo.dingshi;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@EnableScheduling//开启定时任务
@Component
public class SpringTaskDemo {
    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");

    @Scheduled(cron="0/1 * * * * ?")//每秒钟执行一次,以空格分隔
    public void cron(){
        LocalDateTime now=LocalDateTime.now();
        System.out.println("spring task 这是定时任务,时间是:"+pattern.format(now));
    }
}

三、基于Quartz实现定时调度

Quartz的基本介绍:

  • Quartz是一个由java编写的开源任务调度框架
  • 原理:通过触发器设置作业定时运行规则和运行时间
  • 扩展: - 搭建成集群服务:故障切换和负载平衡
  • 作用: - 定时发送信息- 定时生成报表- 。。。
  • 核心组件: 1. 调度器:作业的总指挥2. 触发器:作业的操作者3. 作业:应用的功能模块
  • 常用类: 1. JobDetail:描述Job(任务的核心逻辑)的实现类及其他相关的静态信息,对Job的进一步封装,完成一些属性设置2. Trigger:触发器,定时任务的定时管理工具,一个Trigger只能对应一个定时任务,而一个定时任务可以对应多个触发器3. Scheduler:调度器,定时任务的管理窗口,是Quartz最上层的接口,使所有触发器和定时任务协调工作,一个Scheduler可以注册多个JobDetail和Trigger

思路:

  1. pom.xml文件添加依赖
  2. 自定义定时任务的类需要实现Job接口并重写execute(JobExecutionContext context)方法,并在该方法中实现定时业务逻辑,JobExecutionContext类提供了调度上下文的各种信息,每次执行Job时都需要重新创建一个Job实例
  3. 创建配置类

pom.xml添加依赖的小贴士:

如果在创建springboot项目时勾选了“Quartz Scheduler”,就不用再添加依赖了

否则,要在pom.xml文件里添加依赖

**<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>**
package com.study.demo.dingshi;

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

@Configuration
public class QuartzConfig {

    @Bean
    /**
     * JobDetail对Job的进一步封装,如设置名称和分组、是否持久化、是否可恢复……
     */
    public JobDetail testQuartz1(){
        return JobBuilder.newJob(MyQuartzTask.class)
                .withIdentity("myQuartzTask")//使用给定的触发器名称创建Trigger的唯一标识
                .storeDurably()
                .build();
    }

    @Bean
    /**
     * Trigger用来指定Job的触发规则,如开始时间、频率、优先级……
     */
    public Trigger testQuartz2(){//配置定时任务的执行频率
        SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1)//1秒执行一次
                .repeatForever();//频率
        return TriggerBuilder
                .newTrigger()//调用自己的无参数构造函数
                .forJob(testQuartz1())
                .withIdentity("myQuartzTask")
                .withSchedule(scheduleBuilder)//用于设置ScheduleBuilder,而ScheduleBuilder在负责真正实例化出一个Trigger
                .build();//使用withSchedule()方法设置的ScheduleBuilder实例化一个MutableTrigger
    }
}
package com.study.demo.dingshi;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MyQuartzTask extends QuartzJobBean {//QuartzJobBean是Spring对Job的进一步封装
    private static DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LocalDateTime now=LocalDateTime.now();
        System.out.println("quartz 这是定时任务,时间是:"+pattern.format(now));
    }
}

四、使用分布式定时任务框架:elastic-job

五、分布式任务调度:国产组件XXL-Job

在开发中可以直接使用Timer和ScheduledExecutorService来进行定时任务的测试。

在实际生产环境中,依需选择Spring Task或Quartz。

标签: java 开发语言

本文转载自: https://blog.csdn.net/weixin_72052233/article/details/131330528
版权归原作者 戏拈秃笔 所有, 如有侵权,请联系我们删除。

“springboot:定时任务”的评论:

还没有评论