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. 11.2 uptime:显示系统的运行时间及负载

    uptime命令可以输出当前系统时间.系统开机到现在的运行时间.目前有多少用户在线和系统平均负载等信息. [root@cs6 ~]# uptime   17:02:25 up 1:48, 3 user ...

  2. 【转】Spring_IOC学习

    原文地址:http://github.thinkingbar.com/spring/ 一.XML文件语法的知识点 对于XML没有提示的话,在Eclipse中搜索XML catalog设置.对于XML文 ...

  3. (xxx) is not defined at HTMLInputElement.onblur(Day_27)

    错误: 这个报错我当时是卡了很久,方法是肯定没有问题的,但js所有的事件都失效了. 解决方案: 1.检查js命名是否有误,若外部引用js文件,尽量使用全小写命名,遵守js命名规范. 2.若还不行,请将 ...

  4. Question&&Answer

    1.使用Navicat连接Ubuntu上面的MySql数据库失败 解决办法:Navicat版本的问题,尝试换用更高版本的Navicat解决了问题(当时使用了Navicat Premium_11.2.7 ...

  5. [leetcode] 75. 分类颜色(常数空间且只扫描一次算法)

    75. 分类颜色 我们直接按难度最高的要求做:你能想出一个仅使用常数空间的一趟扫描算法吗? 常数空间 只能扫描一趟.注意,是一趟,而不是O(n) 题中只会出现3个数字:0,1,2.换句话说,0肯定在最 ...

  6. Python+Selenium - Alert弹框

    上面三种弹窗可以在浏览器的控制台做出效果,如下图 上面三种弹窗可以用alert方法处理 示例: #出现弹窗的操作xxxx# 切换al = driver.switch_to.alert# print(a ...

  7. anaconda同时集成Python2 和 Python3

    参考帖子,亲测有效: 利用anaconda同时使用python2和python3的方法 注意:最后一步是再对应的python环境中输入:conda install anaconda

  8. CUDA数学库

    CUDA数学库 高性能数学例程 CUDA数学库是经过行业验证的,高度准确的标准数学函数的集合.只需在源代码中添加" #include math.h",即可用于任何CUDA C或CU ...

  9. TensorRT原理图示

    TensorRT原理图示 NVIDIA的核心 TensorRT是有助于在NVIDIA图形处理单元(GPU)的高性能推理一个C ++库.它旨在与TensorFlow,Caffe,PyTorch,MXNe ...

  10. 编写HSA内核

    编写HSA内核 介绍 HSA提供类似于OpenCL的执行模型.指令由一组硬件线程并行执行.在某种程度上,这类似于 单指令多数据(SIMD)模型,但具有这样的便利:细粒度调度对于程序员而言是隐藏的,而不 ...