首页 > 代码库 > spring集成quartz

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