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);
        }
    }

原文地址:https://www.cnblogs.com/boboxing/p/7058386.html