Spring 定时器

 

定时器

1. 继承QuartzJobBean,重写executeInternal方法

package com.timer;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class TimerJob extends QuartzJobBean {  

  @Override  
  protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {  
       System.out.println("hello");
  }  

}

 2. 配置XML

 

<bean id="timerJob" class="org.springframework.scheduling.quartz.JobDetailBean">  
        <property name="timerjob" value="com.timer.TimerJob"/> 
 </bean> 

 <!-- 每天11点零6分执行-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="triggerjob">  
            <ref bean="timerJob"/>  
        </property>  
        <property name="cron">  
            <value>0 6 11 * * ?</value>  
        </property>  
    </bean>  

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"  autowire="no"
>  
        <property name="triggers">  
            <list>  
                <ref bean="complexTellTheTimeTrigger"/>  
                <!-- other triggers -->  
            </list>  
        </property>  
    </bean>   
</beans> 

3. 红色部分要注意,否则可能报异常:不存在表或者视图,原因是datasource

你可能感兴趣的:(Spring 定时器)