Spring Quartz任务案例源码实现

时间:2022-05-10
本文章向大家介绍Spring Quartz任务案例源码实现,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

写在开始

上一篇有讲到 springTask任务案例源码实现

此篇,spring整合Quartz实现更强大的定时任务。

任务介绍

Quartz存储job方式就分三种,我们最常用的也是quartz默认的是RAMJobStore,RAMJobStore顾名思义就是把job的相关信息存储在内存里,如果用spring配置quartz的job信息的话,所有信息是配置在xml里,当spirng context启动的时候就把xml里的job信息装入内存。这一性质就决定了一旦JVM挂掉或者容器挂掉,内存中的job信息就随之消失,无法持久化。另外两种方式是JobStoreTX和JobStoreCMT,暂时不讨论这两者的区别,使用这两种JobStore,quartz就会通过jdbc直连或者应用服务器jndi连接数据库,读取配置在数据库里的job初始化信息,并且把job通过java序列化到数据库里,这样就使得每个job信息得到了持久化,即使在jvm或者容器挂掉的情况下,也能通过数据库感知到其他job的状态和信息。

功能实现

这里,我们主要讲一下如何通过spring-4.0.6配置Quartz-2.2.1实现内存任务。 Quartz下载地址:http://www.quartz-scheduler.org/downloads

目前,最新版本为2.2.1,Maven配置如下:

<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> 

编写JobTest.java:

package quartz;
/**
 * 任务测试 科帮网 http://www.52itstyle.com/
 * 创建者        张志朋
 * 创建时间        2017年4月22日
 *
 */
public class JobTest {
        public void work(){
                System.out.println("任务开始执行");
        }
}

编写applicationContext-job.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName" default-lazy-init="true">
            
             <bean id="testJob" class="quartz.JobTest"></bean>     
        <!-- 定义调用对象和调用对象的方法 -->
        <bean id="testTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="testJob"/>
            </property>
            <!-- 调用类中的方法 -->
            <property name="targetMethod">
                <value>work</value>
            </property>
        </bean>  
        <!-- 调度触发器 -->
        <bean id="jibDoTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail">
                <ref bean="testTask"/>
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                           <value>0/5 * * * * ?</value>
            </property>
        </bean> 
        <!-- 调度工厂  如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="startQuertz"  factory-bean="executor" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="jibDoTime"/>
                </list>
            </property>
            <!--必须的设置 QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
            <property name="startupDelay" value="5" />
      </bean>
      <!-- 配置任务并发执行线程池 -->
          <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
                <property name="corePoolSize" value="3" />
                <property name="maxPoolSize" value="5" />
                <property name="queueCapacity" value="10" />
          </bean>
</beans>

web.xml 启动配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>spring_quartz</display-name>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-job.xml</param-value>
    </context-param>
     <!-- 监听器 -->
         <listener>
                <listener-class>
                                org.springframework.web.context.ContextLoaderListener
                        </listener-class>
         </listener>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

运行截图:

下载地址:http://www.52itstyle.com/thread-40040-1-1.html