spring集成quartz

Posted 蝈蝈大王

tags:

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

首先需要的jar包,最好是1.8.5的,其他的版本spring如果版本不高的话,会报错

<dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz</artifactId>  
            <version>1.8.5</version>  
</dependency>  
<?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:aop="http://www.springframework.org/schema/aop" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop.xsd   
      http://www.springframework.org/schema/context    
      http://www.springframework.org/schema/context/spring-context.xsd       
      http://www.springframework.org/schema/tx        
      http://www.springframework.org/schema/tx/spring-tx.xsd">
                          
        <!-- 线程执行器配置,用于任务注册 -->
        <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
         <property name="corePoolSize" value="10" />
         <property name="maxPoolSize" value="100" />
         <property name="queueCapacity" value="500" />
        </bean>
        
        <!-- 业务对象,自己定义的操作业务的类 -->
        <bean id="bizObject" class="com.um.framework.baseware.webadmin.modules.controller.core.ExpireJobTask" />
        
        <!-- 调度业务 -->
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
         <property name="targetObject" ref="bizObject" />
         <property name="targetMethod" value="doBiz" />
        </bean>
        
        <!-- 调度触发器、按照时间规则触发 -->
        <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
         <property name="jobDetail" ref="jobDetail" />
         <property name="cronExpression" value="* * * * * ?" />
        </bean>
        <!-- 调度触发器、按时间间隔 -->
        <bean id="taskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
         <property name="jobDetail" ref="jobDetail" />
         <property name="startDelay" value="100" />
         <property name="repeatInterval" value="1000" />
         </bean>
         
         <!-- 设置调度 -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         <property name="triggers">
          <list>
           <ref bean="taskTrigger" />
           <ref bean="cronTrigger" />
          </list>
         </property>
         <property name="taskExecutor" ref="executor" />
        </bean>

</beans>

然后将该配置文件引入spring的配置文件中。下面路径根据自己实际路径

<import resource="classpath:config/spring-context-quartzJob.xml"></import>

 

java类

package com.um.framework.baseware.webadmin.modules.controller.core;

public class ExpireJobTask {
    /** Logger */
    //private static final Logger logger = LoggerFactory.getLogger(ExpireJobTask.class);
 
    /**
     * 业务逻辑处理
     */
    public void doBiz() {
        System.out.println("定时任务开启");
    }
    
}

 

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

Quartz总结:Quartz集成Spring的2个方法

Spring Boot集成持久化Quartz定时任务管理和界面展示

spring集成Quartz时区问题造成任务晚执行八小时

Spring Boot 入门:集成Quartz定时任务

59Quartz+Spring框架详解

Quartz与Spring集成