quartz的maven依赖_Spring整合Quartz框架实现定时任务跑批(Maven完整版)

Quartz 介绍

Quartz is a full-featured, open source job scheduling service that can be integrated with, or

used along side virtually any Java application - from the smallest stand-alone application to

the largest e-commerce system. Quartz can be used to create simple or complex schedules for

executing tens, hundreds, or even tens-of-thousands of jobs;

Quartz框架是一个全功能、开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型

的电子商务系统。Quartz可以执行上千上万的任务调度。

Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节

Spring框架提供了对Quartz框架的支持,这对我们来说,方便了很多。我们还是通过Maven工程来演示。1,pom.xml配置文件内容如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.yangcq

SpringQuartzTest

1.0.0

jar

SpringQuartzTest

4.0.6.RELEASE

2.2.1

org.springframework

spring-core

${springframework.version}

org.springframework

spring-context-support

${springframework.version}

org.springframework

spring-tx

${springframework.version}

org.quartz-scheduler

quartz

${quartz.version}

2,定义我们自己的Job实现类。

Spring框架整合Quartz时,不是直接继承Job类,而是继承QuartzJobBean,

我们这里的一个实现如下:

package com.yangcq.quartz;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

/**

*

* @author yangcq

* @description 由于Spring提供对Quartz的支持,所以直接使用Spring提供的API

* @description 继承 org.springframework.scheduling.quartz.QuartzJobBean

*

*/

public class EBankJob extends QuartzJobBean {

/**

*

*/

private EBankJobBean eBankJobBean;

@Override

protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {

eBankJobBean.printAnotherMessage();

}

public void setEBankJobBean(EBankJobBean eBankJobBean) {

this.eBankJobBean = eBankJobBean;

}

}

这部分代码就是一个典型的spring注入,实际上调用了eBankJobBean的方法。具体job的执行是在executeInternal

方法里。

问题一:目前这种方法没有测试通过,分析一下什么原因?

问题二:MethodInvokingJobDetailFactoryBean 与 JobDetailBean 的区别,研究Spring源码

答:

1,QuartzJobBean是Spring框架下的一个抽象类,这个类实现了Quartz的Job接口。我们可以理解为,Spring对Job的

进一步封装,

public abstract class QuartzJobBean implements Job

2,JobDetailBean也是继承了Quartz下面的JobDetail接口,这里为什么是继承,本人也感到费解,按说JobDetail是

一个抽象接口,

应该用implements关键字实现这个接口,具体代码如下:

public class JobDetailBean extends JobDetail

3,MethodInvokingJobDetailFactoryBean,这个类为我们设置定时任务,提供了丰富的支持,如下:

private String name;

private String group;

private boolean concurrent;

private String targetBeanName;

private String[] jobListenerNames;

private String beanName;

private ClassLoader beanClassLoader;

private BeanFactory beanFactory;

private JobDetail jobDetail;

package com.yangcq.quartz;

/**

*

* @author yangcq

*

*/

public class EBankJobBean {

public void printAnotherMessage(){

System.out.println("CronTriggerBean 调用的定时任务...");

}

}

因为是要将这些任务通过spring的配置文件来拼接到一起,我们来看看具体的配置文件该怎么设置。

在spring里,如果我们要执行一个计划任务,需要定义一个JobDetail,用它来封装我们具体执行的任务。结合前面纯quartz的示例,

我们发现它们其实本质上是一样的。这里的定义如下:

spring默认提供了一个叫MethodInvokingJobDetailFactoryBean,我们需要将定义好的对象和需要调用的方法传给它。

这里对应的是一种类型的jobDetail定义。对应的myBean定义如下:

package com.yangcq.quartz;

/**

*

* @author yangcq

* @description SimpleTriggerFactoryBean 调用的定时任务

*/

public class MyJobBean {

public void printMessage() {

System.out.println("SimpleTriggerFactoryBean 调用的定时任务...");

}

}

OK,下面写一个测试类,进行测试

package com.yangcq.quartz;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

*

* @author yangcq

* @description 启动定时任务main方法

*

*/

public class StartUpQuartzJobs {

static final Log log = LogFactory.getLog(StartUpQuartzJobs.class); // 日志

public static void main(String args[]) throws Exception {

log.info("开始启动定时任务 ...");

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

log.info("定时任务启动成功...");

}

}

接下来就是关键部分了,Spring核心配置文件的的配置,配置文件applicationContext.xml配置如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

启动程序以后,控制台输出如下:

log4j:WARN No appenders could be found for logger (com.yangcq.quartz.StartUpQuartzJobs).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

SimpleTriggerFactoryBean 调用的定时任务...

CronTriggerBean 调用的定时任务...

SimpleTriggerFactoryBean 调用的定时任务...

CronTriggerBean 调用的定时任务...

SimpleTriggerFactoryBean 调用的定时任务...

总结

在这两个示例里,我们首先通过一个纯手工的过程来完成一个任务调度的示例。它的主要步骤为

1.定义job

定义trigger

定义scheduler来拼接。

在后续使用spring的示例里,其实也是这么一个步骤,只不过spring提供了一些实现的支持,需要在配置文件里指定

不同的jobDetail类型和trigger类型。

欢迎工作一到五年的Java工程师朋友们加入Java架构开发:855801563

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值