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. 问题解决: PythonStudy 环境搭建

    环境搭建的时候遇到问题 参见帖子: http://www.xitongcheng.com/jiaocheng/dnrj_article_24923.html 虚拟机运行的时候会遇到 最近有用户发现在电 ...

  2. MyBatis 缓存机制(十三)

    什么是缓存 缓存就是内存中的一个对象,用于对数据库查询结果的保存,用于减少与数据库的交互次数从而降低数据库的压力,进而提高响应速度. MyBatis 缓存机制原理 Mybatis 缓存机制原理是将第一 ...

  3. make clean 和 make distclean区别-(转自秋水Leo)

    make clean仅仅是清除之前编译的可执行文件及配置文件. 而make distclean要清除所有生成的文件. Makefile 在符合GNU Makefiel惯例的Makefile中,包含了一 ...

  4. 火币HBAI量化币圈唯一免费量化炒币机器人

    量化交易是一种投资方法.以先进的数学模型替代人为的主观判断,利用计算机技术从庞大的历史数据中海选能带来超额收益的多种"大概率"事件以制定策略,极大地减少了投资者情绪波动的影响,避免 ...

  5. GO学习-(29) Go语言操作etcd

    Go语言操作etcd etcd是近几年比较火热的一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现,本文主要介绍etcd的安装和使用. etcd etcd介绍 etcd是使用Go语 ...

  6. Python+Selenium - windows安全中心的弹窗(账号登录)

    当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...

  7. Pass算子python 函数

    Pass算子python 函数 函数 函数是代码的一种组织形式 函数应该能完成一项特定的工作,而且一般一个函数只完成一项工作 有些语言,分函数和过程两个概念,通俗解释是,有返回结果的是函数,无返回结果 ...

  8. 基于NVIDIA GPUs的深度学习训练新优化

    基于NVIDIA GPUs的深度学习训练新优化 New Optimizations To Accelerate Deep Learning Training on NVIDIA GPUs 不同行业采用 ...

  9. Postman之newman的安装

    一.newman简介:newman是为Postman而生,专门用来运行Postman编写好的脚本:使用newman,你可以很方便的用命令行来执行postman collections. 二.newma ...

  10. P4779 【模板】单源最短路径(标准版)单源最短路Dijkstra

    题目描述 给定一个$n$个点,$m$条有向边的带非负权图,请你计算从$s$出发,到每个点的距离. 数据保证你能从$s$出发到任意点. 输入格式 第一行为三个正整数$n,m,s$. 第二行起$m$行,每 ...