zoukankan      html  css  js  c++  java
  • spring-quartz定时任务初探

    最近有关定时任务的需求还蛮多的,我这里呢用的是最简单的用法,后续了解更深层次的用法来优化目前的代码。

    首先就是引入相关jar    quartz-1.6.4.jar  spring的jar就不说了

    接下来看看配置文件 applicationContext-quartz.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

          <!-- 实例化bean -->  
        <bean id= "flowPrizeJob" class ="com.yundao.job.FlowPrizeJob"/>  //执行任务的action
          
        <!-- 配置MethodInvokingJobDetailFactoryBean -->  
        <bean id= "flowPrize"  
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
               <property name="targetObject" ref="flowPrizeJob"/>  
               <property name="targetMethod" value="startJob"/>  
               <property name="concurrent" value="false"/>  
        </bean>  
          
        <!-- 配置定时表达式 -->  
        <bean id= "startFlowPrize" class="org.springframework.scheduling.quartz.CronTriggerBean" >  
               <property name="jobDetail" ref="flowPrize" />
              <!-- 每一分钟执行一次 -->   
              <property name="cronExpression" value="0 0/1 * * * ?" />  
        </bean>  
          
        <!-- 配置调度工厂 -->  
        <bean id= "testSchedulerFactoryBean"  
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">  
               <property name="triggers">  
                     <list>  
                           <ref bean="startFlowPrize" />  
                     </list>  
               </property>  
        </bean>  
    </beans>

    这个是我的业务实现类FlowPrizeJob

    @Service("flowPrizeJob")
    public class FlowPrizeJob {

        private static final Logger     logger = LoggerFactory.getLogger(FlowPrizeJob.class);

        @Autowired
        private IFlowPrizeService       iFlowPrizeService;

        /**
         * 执行任务
         */
        public void startJob() {
            try {
                //具体实现
            } catch (Exception e) {
                logger.error(报错", e);
            }
        }

  • 相关阅读:
    正则表达式 \n和\r
    【转】单循环赛赛程安排算法研究
    Iterator效率
    Map获取键值
    PL/SQL语法详解(pdf)
    Iterator模式
    测试js函数的静态页面
    【转】java的一些基本概念
    Oracle 11g用户解锁
    oracle官方文档
  • 原文地址:https://www.cnblogs.com/boboxing/p/7058386.html
Copyright © 2011-2022 走看看