spring整合Quartz

TitusGordon 发布于2年前

Quartz入门教程链接

前面用案例介绍过SimpleTrigger和CronTrigger,
下面用spring整合Quartz,分四步:

  1. 导入依赖
  2. 编写Job
  3. 编写spring配置文件
  4. 启动spring容器(启动调度器)

1. 导入maven依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.ouhei.quartz</groupId>
  <artifactId>ouhei-quartz</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>4.0.6.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz</artifactId>
          <version>2.2.1</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.7</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.0.6.RELEASE</version>
      </dependency>
  </dependencies>
</project>

2. 编写Job

package org.ouhei.quartz;

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

/**
 * QuartzJobBean实现了Job接口
 * 
 */
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);
        
    }

}

3. 编写spring配置文件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="org.ouhei.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" />
        <!-- 每5秒执行一次 -->
        <property name="cronExpression" value="0/5 * * * * ?" />
    </bean>
    
    <!-- 定义触发器 -->
    <!-- 演示:一个job可以有多个trigger;一个trigger只能有一个job -->
    <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        <!-- 每一分钟执行一次 -->
        <property name="cronExpression" value="0 */1 * * * ?" />
    </bean>
    
    <!-- 定义调度器 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
                <ref bean="cronTrigger2" />
            </list>
        </property>
    </bean>

</beans>

4. 启动spring容器(启动调度器)

package org.ouhei.quartz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml");
    }

}

5. 添加日志文件

log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

查看原文: spring整合Quartz

  • silverleopard
  • yellowcat
  • orangeswan
  • goldenpanda
  • tinypanda
  • GabrielTony
  • CarrieAntonio
  • AnneTracy
  • lazyrabbit
  • yellowlion
  • DeQuinceySalome