码迷,mamicode.com
首页 > 编程语言 > 详细

Java Timer触发定时器

时间:2016-06-13 21:46:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

XML:

<!-- Java Timer定时  -->
    <!-- <bean id="shortUrlTask" class=" com.spring.common.ShortUrlTask">
    </bean>  
    <bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
        <property name="timerTask" ref="shortUrlTask" />
        <property name="period">
            <value>500</value>
        </property>  
    </bean>
    
    <bean class="org.springframework.scheduling.timer.TimerFactoryBean">  
        <property name="scheduledTimerTasks">  
           <list>
               <ref bean="scheduleReportTask"/>
           </list>  
        </property> 
    </bean> -->
    
    <!-- <bean id="SchedulerTask" class="org.springframework.scheduling.quartz.JobDetailBean">  
        <property name="jobClass">  
            <value>com.spring.common.QuatzTime</value>  
        </property>
    </bean> 
    
    <bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail" ref="SchedulerTask" />
      <property name="cronExpression">
       <value>0/10 * * * * ?</value>
      </property>
     </bean> -->
     
      <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <!-- durability 表示任务完成之后是否依然保留到数据库,默认false   -->
        <property name="durability" value="true" />  
        <!--     目标类  /wmuitp/src/test/SpringQuartz.java-->
        <property name="jobClass" value="com.spring.common.QuatzTime"></property>
       </bean>
       
       <!-- 另一种触发器是CornTrigger -->
     <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
       <property name="jobDetail" ref="jobDetail"/>
       <!-- 每个10秒触发 -->
       <property name="cronExpression" value="0/10 27 10 * * ?"/>
    </bean> 
    
    <!-- 定义核心调度器 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <ref bean="cornTrigger"/>
      </property>
    </bean>

java类中:

public class QuatzTime extends QuartzJobBean
{

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println("现在的时间是:" + nowTime());
    }

    public String nowTime()
    {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sf.format(new Date());
        return time;
    }

}

 

Java Timer触发定时器

标签:

原文地址:http://www.cnblogs.com/dragkiss/p/3906814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!