SpringBoot+Quartz实现定时器,
由于本人也是刚学习,不足之处请各位大神指正 .. 

1.pom配置

  1.  
    <dependency>
  2.  
    <groupId>org.springframework</groupId>
  3.  
    <artifactId>spring-context-support</artifactId>
  4.  
    </dependency>
  5.  
    <dependency>
  6.  
    <groupId>org.springframework</groupId>
  7.  
    <artifactId>spring-tx</artifactId>
  8.  
    </dependency>
  9.  
    <dependency>
  10.  
    <groupId>org.quartz-scheduler</groupId>
  11.  
    <artifactId>quartz</artifactId>
  12.  
    <version>2.2.1</version>
  13.  
    </dependency>
  14.  
     
  15.  
     
  16.  
     

2.注册[pring-boot启动完成事件监听,用于启动job任务

  1.  
    @Configuration
  2.  
    public class SchedulerListener implements ApplicationListener<ContextRefreshedEvent> {
  3.  
    @Autowired
  4.  
    public MyScheduler myScheduler;
  5.  
    @Override
  6.  
    public void onApplicationEvent(ContextRefreshedEvent event) {
  7.  
    try {
  8.  
    myScheduler.scheduleJobs();
  9.  
    } catch (SchedulerException e) {
  10.  
    e.printStackTrace();
  11.  
    }
  12.  
    }
  13.  
    @Bean
  14.  
    public SchedulerFactoryBean schedulerFactoryBean(){
  15.  
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
  16.  
    return schedulerFactoryBean;
  17.  
    }
  18.  
    }
  19.  
     
  20.  
     

3、job参数设置

  1.  
    @Component
  2.  
    public class MyScheduler {
  3.  
    @Autowired
  4.  
    SchedulerFactoryBean schedulerFactoryBean;
  5.  
    public void scheduleJobs() throws SchedulerException {
  6.  
    Scheduler scheduler = schedulerFactoryBean.getScheduler();
  7.  
    startJob1(scheduler);
  8.  
    startJob2(scheduler);
  9.  
    }
  10.  
    private void startJob1(Scheduler scheduler) throws SchedulerException{
  11.  
    JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
  12.  
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
  13.  
    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
  14.  
    scheduler.scheduleJob(jobDetail,cronTrigger);
  15.  
    }
  16.  
    private void startJob2(Scheduler scheduler) throws SchedulerException{
  17.  
    JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();
  18.  
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
  19.  
    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();
  20.  
    scheduler.scheduleJob(jobDetail,cronTrigger);
  21.  
    }
  22.  
    }
  23.  
     
  24.  
     

4.实现各个任务job

  1.  
    public class ScheduledJob implements Job{
  2.  
    private SimpleDateFormat dateFormat() {
  3.  
    return new SimpleDateFormat("HH:mm:ss");
  4.  
    }
  5.  
    @Override
  6.  
    public void execute(JobExecutionContext context) throws JobExecutionException {
  7.  
    System.out.println("AAAA: The time is now " + dateFormat().format(new Date()));
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    这样会导致一个问题,就是执行定时器的时候 ,service不能注入 !
  12.  
     
  13.  
    解决方法: 通过外部来创建serivce ,然后在定时器里调用
  14.  
     
  15.  
     
创建ApplicationContextUtil工具类
/**
* 定时器service注入工具类
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() {
return applicationContext;
} public void setApplicationContext(ApplicationContext applicationContext) {
ApplicationContextUtil.applicationContext = applicationContext;
} public static Object getBean(String BeanName){
return applicationContext.getBean(BeanName);
}
} 2. 在定时器里调用工具类来创建service
   BankService bankService=(BankService) ApplicationContextUtil.getBean("BankService");
Double money=bankService.getBalance("62279205947481841"); 这样就完美的解决了service注入空指针异常的问题
转自:https://blog.csdn.net/dreamer_ax/article/details/72282392

SpringBoot集成Quartz实现定时器的更多相关文章

  1. SpringBoot集成Quartz实现定时任务

    1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...

  2. SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)

    使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. Quartz的4个核心概念: 1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法v ...

  3. Springboot集成Quartz

    之前学习过spring的定时任务 :https://www.cnblogs.com/slimshady/p/10112515.html 本文主要学习记录下springboot使用quartz 1.   ...

  4. springBoot集成 quartz动态定时任务

    项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...

  5. Spring Boot笔记(三) springboot 集成 Quartz 定时任务

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1. 在 pom.xml 中 添加 Quartz 所需要 的 依赖 <!--定时器 quartz- ...

  6. springboot集成quartz定时任务课动态执行

    <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...

  7. springboot集成quartz实现任务调度

    quartz 概述 特点 强大的调度功能 灵活的应用方式 分布式和集群能力 用到的设计模式 Builder 模式 factory模式 组件模式 链式写法 体系结构 调度器 任务 触发器 架构图 spr ...

  8. springboot自带定时任务和集成quartz

    1,springboot自带的定时任务  默认是单线程 有这个依赖就可以 <dependency> <groupId>org.springframework.boot</ ...

  9. SpringBoot整合Quartz定时任务(持久化到数据库)

    背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...

随机推荐

  1. static在C/C++中的作用-(转自华山大师兄)

    1.先来介绍它的第一条也是最重要的一条:隐藏.(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.举例来说明.同时编译两个源文件 ...

  2. Linux命令学习—— fdisk -l 查看硬盘及分区信息

    Linux命令学习(3)-- fdisk -l 查看硬盘及分区信息注意:在使用fdisk命令时要加上sudo命令,否则什么也不能输出linux fdisk 命令和df区别是什么? fdisk工具是分区 ...

  3. 5分钟安装docker教程

    Centos安装docker需要操作系统是 CentOS 7 or 8,必须启用centos extras存储库.默认情况下,此存储库处于启用状态,但如果已禁用它,则需要重新启用它. 卸载旧版本 老版 ...

  4. GO语言常用标准库04---flag读取命令行参数

    package main import ( "flag" "fmt" "math" "os" ) /* go build ...

  5. lms框架模块详解

    模块的定义 一般地,开发者如果想要在一个自定义的程序集(包)中注册相关的服务,或者在应用初始化或停止时执行一段自定义的代码,那么您可能需要将该程序集(包)定义为一个模块. lms框架存在两种类型的模块 ...

  6. Processing平台之PVector求角度

    问题:在processing 平台,通过给定三个PVector向量,如何求他们之间的夹角,同时确定是在左侧还是右侧? 如图所示,在processing 平台中,PVector表示点的坐标是以原点为起点 ...

  7. 批量执行异步任务CompletionService

    批量执行异步任务CompletionService 核心思想,就是将异步结果放入到阻塞队列中,然后再消费队列,实现异步任务批量执行 接口方法说明 Future<V> submit(Call ...

  8. CVPR2020:点云分析中三维图形卷积网络中可变形核的学习

    CVPR2020:点云分析中三维图形卷积网络中可变形核的学习 Convolution in the Cloud: Learning Deformable Kernels in 3D Graph Con ...

  9. Spring Cloud系列(七):消息总线

    在上一篇中,当一个配置中心的客户端启动之后,它所引用的值就无法改变了,但是Spring Cloud 提供了一种手段去解决了这个问题--Spring Cloud Bus. 一.Spring Cloud ...

  10. Spring Security 快速上手

    Spring Security 框架简介 Spring Security 说明 Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案 关于安全方面的两 ...