Quartz与Spring集成

Posted bky_lzw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz与Spring集成相关的知识,希望对你有一定的参考价值。

一、获取quartz详情jar

<!-- quartz 的jar -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.1</version>
</dependency>

二、相关代码案例

//Main.java启动类
public
class Main { public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml"); } }
//MyJob任务类
public
class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("myJob执行了............" + context.getTrigger().getKey().getName()); ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap() .get("applicationContext"); System.out.println("获取到的Spring容器是" + applicationContext); } }
//applicationContext-scheduler.xml
<?
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
> <!-- 定义任务bean --> <bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定具体的job类 --> <property name="jobClass" value="com.bky_lzw.quartz.MyJob" /> <!-- 指定job的名称 --> <property name="name" value="myJob" /> <!-- 指定job分组 --> <property name="group" value="jobs" /> <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务 --> <property name="durability" value="true" /> <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 --> <property name="applicationContextJobDataKey" value="applicationContext" /> </bean> <!-- 定义触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myJobDetail" /> <!-- 每一分钟执行一次 --> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <!-- 定义调度器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>



以上是关于Quartz与Spring集成的主要内容,如果未能解决你的问题,请参考以下文章

Quartz与Spring集成

Quartz与Spring集成

Quartz与Spring集成——启动调度器

Quartz与Spring集成 Job如何自动注入Spring容器托管的对象

Quartz与Spring Boot集成使用

SpringBoot系列:Spring Boot集成定时任务Quartz