Spring设置定时器:quartz

基础:上一篇搭过的SSH框架中

1.Spring增加配置,16点55执行(自己需要来修改)

<!-- 配置定时任务 -->
    <bean id="SchedulerTask" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass">
       <value>com.ccc.quartz.TestQuartz</value>
      </property>
     </bean>
    <!-- 配置定时时间 -->
    <bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail" ref="SchedulerTask" />
      <property name="cronExpression">
       <value>0 55 16 * * ?</value>
      </property>
     </bean>
    <!-- 开启定时任务 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
       <list>
             <ref bean="SchedulerTaskTrigger" />
       </list>
      </property>
     </bean>

2.POM文件增加引入包

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
               <groupId>org.quartz-scheduler</groupId>
               <artifactId>quartz</artifactId>
               <version>1.8.5</version>
          </dependency>

3.TestQuartz

package com.ccc.quartz;
/** 
* @author ccc
* @datetime 2016年6月15日 下午3:59:06 
*  
*/
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;


public class TestQuartz extends QuartzJobBean{
 @Override
 protected void executeInternal(JobExecutionContext arg0)
   throws JobExecutionException {
  System.out.println("定时任务进入");
 }
}

4.启动应用等到时间为16点55分的时,控制台输出:

posted on 2016-06-15 16:56  laugher_ccc  阅读(293)  评论(0编辑  收藏  举报

导航