Quartz-Spring集成Quartz通过XML配置的方式

文章目录

  • 概述
  • Maven依赖
  • 步骤
    • 1 创建JobDteail
    • 2 创建Trigger
    • 3 创建Scheduler
  • 示例-MethodInvokingJobDetailFactoryBean
  • 示例-JobDetailFactoryBean
  • 示例源码

在这里插入图片描述

概述

Spring为创建Quartzde Scheduler、Trigger和JobDetail提供了方便的FactoryBean类,以便能够在Spring容器中享受注入的好处。

此外,Spring还通了一些便利的工具类,用于直接将Spring中的Bean包装成合法的任务。

Spring进一步降低了使用Quartz的难度,能够以更加Spring风格的方式使用Quartz,主要体现在如下两点

  • 为Quartz的重要组件提供更具Bean风格的扩展类

  • 提供创建Scheduler的BeanFactory类,方便在Spring环境下创建对应的组件对象,并结合Spring容器生命周期执行启动和停止的动作


Maven依赖

主要的两个依赖如下:

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
</dependency>

<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version> 
</dependency>

spring-context-support包主要是Spring集成了很多有用的第三方库,我们在使用第三方的东西的时候一般都需要引入该依赖。


步骤

Spring通过xml的方式集成Quartz,主要有两种方式

  1. JobDteail通过JobDetailBean实现

  2. JobDteail通过MethodInvokeJobDetailFactoryBean实现(推荐)


1 创建JobDteail

Spring通过扩展JobDetail提供了一个更具Bean风格的JobDetailFactoryBean,此外Spring还提供了了一个 MethodInvokingJobDetailFactoryBean,通过这个FactoryBean可以将Spring容器中的Bean的方法包装成Quartz任务,这样我们就不必为Job创建对应的类。

案例中会分别讲述两种实现方式的区别


2 创建Trigger

Quartz中另外一个重要的组件就是Trigger,Spring按照相似的思路分别为SimpleTrigger和CronTrigger提供了更具Bean风格的CronTriggerFactoryBean和SimpleTriggerFactoryBean扩展类。


3 创建Scheduler

Quartz的SchedulerFactory是标准的工厂类,不太适合在Spring中使用。 此外为了保证Scheduler能够感知Spring容器的生命周期,在Spring容器启动后,Scheduler自动开始工作,而在Spring容器关闭前,自动关闭Scheduler。为此Spring提供了SchedulerFactoryBean.


示例-MethodInvokingJobDetailFactoryBean

Quartz-Spring集成Quartz通过XML配置的方式_第1张图片

因为下面的案例中我们使用MethodInvokingJobDetailFactoryBean,所以下面的这个Job无需继承任何类或者实现任何接口

模拟业务类

配置成一个普通的java类即可

package com.xgj.quartz.quartzWithSpring.xml;


/**
 * 
 * 
 * @ClassName: MyJob
 * 
 * @Description: 不用继承Quartz的Job接口
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年11月10日 下午10:17:26
 */
public class MyJob {

	public void execute() {
		System.out.println("Quartz Spring XML 配置 - MyJob");
	}

}


Spring集成Quartz的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

    <!-- 配置Job类 -->
    <bean id="myJob" class="com.xgj.quartz.quartzWithSpring.xml.MyJob"></bean>

    <!-- 配置JobDetail -->
    <bean id="springQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 执行目标job -->
        <property name="targetObject" ref="myJob"></property>

        <!-- 要执行的方法 -->
        <property name="targetMethod" value="execute"></property>
    </bean>

    <!-- 配置tirgger触发器 -->
    <bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- jobDetail -->
        <property name="jobDetail" ref="springQtzJobMethod"></property>

        <!-- cron表达式,执行时间  每5秒执行一次 -->
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!-- 配置调度工厂 -->
    <bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerFactoryBean"></ref>
            </list>
        </property>

    </bean>
</beans>

除了使用CronTrigger也可以使用SimpleTrigger配置,示例代码如下

<bean id="simpleTrigger"  class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <!--配置具体要执行的jobDetail-->
    <property name="jobDetail" ref="simpleJobDetail" />
    <!--初始延迟时间 1s-->
    <property name="startDelay" value="1000" />
    <!--间隔执行时间每2s执行一次-->
    <property name="repeatInterval" value="2000" />
</bean>

测试类

package com.xgj.quartz.quartzWithSpring.xml;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzXmlTest {

	public static void main(String[] args) {
		// 启动Spring 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
						"classpath:com/xgj/quartz/quartzWithSpring/xml/application.xml");
		System.out.println("initContext successfully");
	}
}

在调度器中,有一个lazy-init参数,如果lazy-init=‘false’,则容器启动时就会执行调度程序;如果lazy-init=‘true’,则需要实例化该bean才能执行调度程序;

<bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true">


运行结果

2017-11-11 02:03:41,399  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@174580e6: startup date [Sat Nov 11 02:03:41 BOT 2017]; root of context hierarchy
2017-11-11 02:03:41,481  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/quartz/quartzWithSpring/xml/application.xml]
INFO  StdSchedulerFactory - Using default implementation for ThreadExecutor
INFO  SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
INFO  QuartzScheduler - Quartz Scheduler v.2.2.3 created.
INFO  RAMJobStore - RAMJobStore initialized.
INFO  QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'springJobSchedulerFactoryBean' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

INFO  StdSchedulerFactory - Quartz scheduler 'springJobSchedulerFactoryBean' initialized from an externally provided properties instance.
INFO  StdSchedulerFactory - Quartz scheduler version: 2.2.3
INFO  QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@7fe0dbb6
2017-11-11 02:03:42,862  INFO [main] (DefaultLifecycleProcessor.java:343) - Starting beans in phase 2147483647
2017-11-11 02:03:42,862  INFO [main] (SchedulerFactoryBean.java:645) - Starting Quartz Scheduler now
INFO  QuartzScheduler - Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED started.
initContext successfully
Quartz Spring XML 配置 - MyJob
Quartz Spring XML 配置 - MyJob
Quartz Spring XML 配置 - MyJob
Quartz Spring XML 配置 - MyJob
Quartz Spring XML 配置 - MyJob
.......
.......
.......
.......

示例-JobDetailFactoryBean

Quartz-Spring集成Quartz通过XML配置的方式_第2张图片

业务Job模拟类

package com.xgj.quartz.quartzWithSpring.xml2;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 
 * 
 * @ClassName: MyJob2
 * 
 * @Description: 配置文件中使用JobDetailBean,这是业务Job需要继承QuartzJobBean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年11月12日 上午7:54:28
 */
public class MyJob2 extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext ctx)
			throws JobExecutionException {
		System.out.println("Spring 集成 Quartz 的第二种方式");
	}

}

Spring集成Quartz的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

    <!-- 配置Job类 -->
    <bean id="myJob" class="com.xgj.quartz.quartzWithSpring.xml.MyJob"></bean>

    <!-- 配置JobDetail -->
     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.xgj.quartz.quartzWithSpring.xml2.MyJob2"></property>
        <property name="durability" value="true"></property>
    </bean>
    

    <!-- 配置tirgger触发器 -->
    <bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- jobDetail -->
        <property name="jobDetail" ref="jobDetail"></property>

        <!-- cron表达式,执行时间  每5秒执行一次 -->
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!-- 配置调度工厂 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerFactoryBean"></ref>
            </list>
        </property>

    </bean>
</beans>

测试类

package com.xgj.quartz.quartzWithSpring.xml2;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringQuartzXmlTest {

	public static void main(String[] args) {
		// 启动Spring 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:com/xgj/quartz/quartzWithSpring/xml2/spring-quartz.xml");
		System.out.println("initContext successfully");
	}
}

运行结果

2017-11-12 08:14:27,934  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4af6178d: startup date [Sun Nov 12 08:14:27 BOT 2017]; root of context hierarchy
2017-11-12 08:14:28,014  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/quartz/quartzWithSpring/xml2/spring-quartz.xml]
INFO  StdSchedulerFactory - Using default implementation for ThreadExecutor
INFO  SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
INFO  QuartzScheduler - Quartz Scheduler v.2.2.3 created.
INFO  RAMJobStore - RAMJobStore initialized.
INFO  QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'springJobSchedulerFactoryBean' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

INFO  StdSchedulerFactory - Quartz scheduler 'springJobSchedulerFactoryBean' initialized from an externally provided properties instance.
INFO  StdSchedulerFactory - Quartz scheduler version: 2.2.3
INFO  QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@404f4560
2017-11-12 08:14:28,800  INFO [main] (DefaultLifecycleProcessor.java:343) - Starting beans in phase 2147483647
2017-11-12 08:14:28,801  INFO [main] (SchedulerFactoryBean.java:645) - Starting Quartz Scheduler now
INFO  QuartzScheduler - Scheduler springJobSchedulerFactoryBean_$_NON_CLUSTERED started.
initContext successfully
Spring 集成 Quartz 的第二种方式
Spring 集成 Quartz 的第二种方式
Spring 集成 Quartz 的第二种方式
Spring 集成 Quartz 的第二种方式
Spring 集成 Quartz 的第二种方式
.......
.......
.......

示例源码

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

你可能感兴趣的:(【Quartz任务调度】,Quartz手札)