• spring的quartz定时任务


    一、版本:

      1.spring:4.1.7;

         2.quartz:2.2.1;

    二、基于ssm项目:

    1.引入jar包:quartz-2.2.1.jar;spring所需包。

    2.说明:quartz版本和spring版本要兼容,低版本的quartz集成在高版本的spring中会报错:java.lang.ClassNotFoundException: org.quartz.impl.JobDetailImpl。

      其实在spring4.1.x的源码中已经有对应的说明了,原文如下:

    Compatible with Quartz 2.1.4 and higher, as of Spring 4.1. 

    3.常见异常原因ClassNotFoundException: org.springframework.scheduling.quartz.CronTriggerBean:

    在quartz 1.8.6及以前版本的时候 调度触发器 依赖的类是 org.springframework.scheduling.quartz.CronTriggerBean

    在2.xx版本之后就改为了org.springframework.scheduling.quartz.CronTriggerFactoryBean

    因此当你依赖2.x.x版本之后只需将调度触发器的依赖类改为 org.springframework.scheduling.quartz.CronTriggerFactoryBean即可

    三、代码:

    quartz文件配置applicationContext-quartz.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:cache="http://www.springframework.org/schema/cache"
           xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">
    
        <!-- quartz定时任务 -->
        <!-- <bean id="marketTest" class="com.test.www.web.timeTask.MarketMaintainEmailRemind">
            
        </bean> -->
          <!--第四步 把定义好的任务放到调度(Scheduler)工厂里面,注意这里的ref bean -->  
        <bean id="schedulerFactoryBean3"  
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
            <property name="applicationContextSchedulerContextKey" value="applicationContext"/>  
            <property name="triggers">  
                <list>  
                    <ref bean="printTimerTrigger10" />  
                </list>  
            </property>  
        </bean>
        <!-- CronTriggerBean -->
        <bean id="printTimerJob10"  
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
            <property name="targetObject" ref="marketMaintainEmailRemind" />  <!-- marketMaintainEmailRemind marketTest -->
            <property name="targetMethod" value="run" />  
            <property name="concurrent" value="false"/>  
        </bean>  
        <bean id="printTimerTrigger10" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
            <property name="jobDetail" ref="printTimerJob10" />  
            <property name="cronExpression" value="0 54 12 * * ?" /> 
        </bean>
        
        
    </beans>

    在spring的配置文件applicationContext.xml中引入配置文件applicationContext-quartz.xml:

    <import resource="applicationContext-quartz.xml" />

    定时类:

    package com.test.www.web.timeTask;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Component;
    
    import com.test.www.util.PrimaryKeyGenerator;
    import com.test.www.web.entity.user.User;
    import com.test.www.web.service.user.UserService;
    
    //注:MarketMaintainEmailRemind类注册bean默认名称为类名且首字母小写,可通过注解方式自定义名称修改bean名称
    @Component //@Service("marketTest")  @Component("marketTest")
    public class MarketMaintainEmailRemind {
        @Resource
        private UserService userService;
        
        public void run() {
            
            try {
                System.out.println("aa");
                User user = new User();
                user.setId(PrimaryKeyGenerator.getLongKey());
                user.setUsername("定时任务Test");
                user.setPassword("dingshirenwuTest");
                userService.insertUser(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    完成后,设定quartz中的时间,等待触发。时间一到就会执行MarketMaintainEmailRemind中的run方法。执行结果如下图所示:

                                 

  • 相关阅读:
    最佳路径搜索算法1
    积分方程的程序化解决方案
    lcov
    nvidia driver
    dependency
    scp ssh-server
    boost 安装 latest
    ubuntu 快捷键
    nvidia drive
    查询优化器 postgres
  • 原文地址:https://www.cnblogs.com/super-chao/p/7884426.html
Copyright © 2020-2023  润新知