博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Spring boot框架整合quartz,解决了mapper无法注入的问题
阅读量:6340 次
发布时间:2019-06-22

本文共 5201 字,大约阅读时间需要 17 分钟。

转自

我只贴代码:

1.配置pom.xml

org.quartz-scheduler
quartz
2.2.1
org.springframework
spring-context-support

2.先写一个监听器保证项目启动的时候会调用到,我们的定时任务,代码如下

import org.quartz.SchedulerException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationListener;import org.springframework.context.annotation.Configuration;import org.springframework.context.event.ContextRefreshedEvent;import com.fjaidata.timer.MyScheduler;@Configurationpublic class SchedulerListener implements ApplicationListener
{ @Autowired public MyScheduler myScheduler; @Override public void onApplicationEvent(ContextRefreshedEvent event) { try { myScheduler.scheduleJobs(); } catch (SchedulerException e) { e.printStackTrace(); } }}

3.写我们自己的定时器,用来触发我们的任务

import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.TriggerBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.quartz.SchedulerFactoryBean;import org.springframework.stereotype.Component;import com.fjaidata.job.AiArticleJob;import com.fjaidata.job.SealedBiddingJob;@Componentpublic class MyScheduler {    @Autowired    SchedulerFactoryBean schedulerFactoryBean;    static Scheduler scheduler;    public void scheduleJobs() throws SchedulerException {        scheduler = schedulerFactoryBean.getScheduler();        startJob1();        startJob2();    }        public static void startJob1() throws SchedulerException {        JobDetail jobDetail = JobBuilder.newJob(AiArticleJob.class).withIdentity("job1", "group1").build();        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 8 * * ?");        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")                .withSchedule(scheduleBuilder).build();        scheduler.scheduleJob(jobDetail, cronTrigger);    }        public static void startJob2() throws SchedulerException {        JobDetail jobDetail = JobBuilder.newJob(SealedBiddingJob.class).withIdentity("job2", "group1").build();        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 8 ? * MON,FRI");        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1")                .withSchedule(scheduleBuilder).build();        scheduler.scheduleJob(jobDetail, cronTrigger);    }        }

4.定时器

@Configurationpublic class SealedBiddingJob implements Job{
@Autowired private SealedBiddingMapper biddingMapper; @Override public void execute(JobExecutionContext context) throws JobExecutionException {} }

这是我们会发现总是报空指针的问题

原因:在quartz框架中,Job 是通过反射出来的实例,不受spring的管理。Scheduler现在交给Spring生成,在Spirng-context-support  jar包下org.springframework.scheduling.quartz包中有个SpringBeanJobFactory的类,job实例通过该类的createJobInstance方法创建。根据Scheduler context、job data map and trigger data map填充其属性。但是创建的job实例并没被spring管理,这就需要我们自定义一个类将创建的job添加到applicationContext中,该类需要继承SpringBeanJobFactory,并实现ApplicationContextAware接口。

    ApplicationContextAware接口的作用:Spring容器会检测容器中的所有Bean,如果发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContextAware()方法,调用该方法时,会将容器本身作为参数传给该方法——该方法中的实现部分将Spring传入的参数(容器本身)赋给该类对象的applicationContext实例变量,因此接下来可以通过该applicationContext实例变量来访问容器本身。

import org.quartz.spi.TriggerFiredBundle;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.scheduling.quartz.SpringBeanJobFactory;public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements        ApplicationContextAware {    private transient AutowireCapableBeanFactory autowireCapablebeanFactory;    @Override    public void setApplicationContext(final ApplicationContext context) {        autowireCapablebeanFactory = context.getAutowireCapableBeanFactory();    }    @Override    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {        final Object job = super.createJobInstance(bundle);        autowireCapablebeanFactory.autowireBean(job);        return job;    }}
@Configurationpublic class SchedulerConfig {        @Bean    public JobFactory jobFactory(ApplicationContext applicationContext)    {        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();        jobFactory.setApplicationContext(applicationContext);        return jobFactory;    }    @Bean    public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws Exception {        SchedulerFactoryBean factory = new SchedulerFactoryBean();        factory.setOverwriteExistingJobs(true);        factory.setJobFactory(jobFactory);        factory.afterPropertiesSet();        return factory;    }}

 

转载于:https://www.cnblogs.com/person008/p/10826929.html

你可能感兴趣的文章
U盘病毒原理分析和解决方法
查看>>
谈谈系统架构这个东西
查看>>
thunderbird 导入通讯录乱码问题解决
查看>>
使用LVS+Keepalived实现Mysql的 负载均衡Web集群系统
查看>>
[.Net线程处理系列]专题四:线程同步
查看>>
lync Topology内Replication显示红X
查看>>
yum安装crontab
查看>>
取消列隐藏
查看>>
JVM初探- 内存分配、GC原理与垃圾收集器
查看>>
Ruby和SHELL中如何遍历指定目录的文件
查看>>
LevelDB
查看>>
CentOS7下安装mysql5.6修改字符集为utf8并开放端口允许远程访问
查看>>
初尝Mcafee之在ePO中进行策略和客户端任务设置【06】
查看>>
通过案例学调优之--和 BUFFER CACHE 相关的主要 Latch
查看>>
网络开发工具应用客户端下载Dreamweaver8在Windows XP中安装详解与步骤
查看>>
事务回滚后,自增ID仍然增加。
查看>>
sql2000数据库维护计划器
查看>>
IO Foundation 3 -文件解析器 FileParser
查看>>
查询MY SQL数据库IO,查询等各种指标
查看>>
【阿里云ACE】上海新春活动,辞旧迎新嗨起来!
查看>>