• spring配置定时器的两种方式quartz和TimerTask(示例转)


    转自:http://blog.csdn.net/daryl715/article/details/1639876

    首先我们编写调度服务,继承java.util.TimerTask

    package TimerTest;

    import java.util.Date;
    import java.util.TimerTask;

    public class TimerService extends TimerTask {    
        
    public void run() {
            System.out.println(
    new Date().getSeconds());        
        }

    }

    配置文件:以下是两种定时器的配置,使用任意一种即可

    方式一: TimerTask定时器配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
    <beans>
      
    <!-- 配置调度方法 -->
      
    <bean id="reportTask" class="TimerTest.TimerService"/>
      
    <!-- 配置定时器任务 -->
      
    <bean id="scheduledReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
        
    <property name="timerTask">
          
    <ref bean="reportTask"/>
        
    </property>
         
    <property name="period">
          
    <value>1000</value>
        
    </property>

      
    </bean>
      
    <!-- 启动定时器 -->
      
    <bean id="start" class="org.springframework.scheduling.timer.TimerFactoryBean">
        
    <property name="scheduledTimerTasks">
          
    <list>
          
    <ref bean="scheduledReportTask"/>
        
    </list>
        
    </property>
      
    </bean>
    </beans>

    方式二:Quartz定时器配置: 该定时器,需要导入quatz-all1.6.jar 和common-collections.jar包
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
         xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context 
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
               >
               
      <!-- 任务类 -->
     <bean id="myTask" class="myspring.mvc.web.TimerSerivce"/>
     
     <!-- 代理指定运行的任务方法  -->
     <bean id="myCron-over-timer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
         <property name="targetObject">
             <ref bean="myTask"/>
         </property>
         <property name="targetMethod">
             <value>run</value>
         </property>
     </bean>
               
     <!-- 设置任务的运行周期 -->
    <bean id="myCron-trigger-timer" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                  <ref bean="myCron-over-timer"/>
            </property>
            <property name="cronExpression">
                <value>*/1 * * * * ?</value>
            </property>
    </bean>
    
    <!-- 启动定时器 -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                <property name="triggers">
                        <list>
                                <ref bean="myCron-trigger-timer"/>
                        </list>
                </property>
        </bean>
    
    
    </beans>

    测试代码:

    package TimerTest;

    import java.io.File;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;




    public class TestTimer {

        
    public static void main(String[] args) {

            String filePath
    =System.getProperty("user.dir")+File.separator+"TimerTest"+File.separator+"hello.xml";
            
            ApplicationContext  context
    =new FileSystemXmlApplicationContext(filePath);
            
            
    //如果使用BeanFactory,则必须调用factory.getBean("start"),才能启动调度任务
            
            
        //或者直接把项目布署到服务器,启动服务器,则可看到控制台会打印当前秒数

        }

    }

    运行结果:

    2007-6-5 23:16:24 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@109a4c: display name [org.springframework.context.support.FileSystemXmlApplicationContext@109a4c]; startup date [Tue Jun 05 23:16:24 CST 2007]; root of context hierarchy
    2007-6-5 23:16:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from file [E:/项目/SpringInActionStudy/TimerTest/hello.xml]
    2007-6-5 23:16:24 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    信息: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@109a4c]:org.springframework.beans.factory.support.DefaultListableBeanFactory@cd2c3c
    2007-6-5 23:16:24 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    信息: Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@cd2c3c: defining beans [reportTask,scheduledReportTask,start]; root of factory hierarchy
    2007-6-5 23:16:24 org.springframework.scheduling.timer.TimerFactoryBean afterPropertiesSet
    信息: Initializing Timer
    25
    26
    27

    可以看到,每隔一秒就打印当前时间的秒数

    示例下载:springmvc-timer.rar

  • 相关阅读:
    聊聊 print 的前世今生
    在树莓派里搭建 Lighttpd 服务器
    如何重复执行一条命令直至运行成功?
    手把手教你Windows Linux双系统的安装与卸载
    你以为只有马云会灌鸡汤?Linux 命令行也会!
    Linux 下三种提高工作效率的文件处理技巧
    太高效了!玩了这么久的Linux,居然不知道这7个终端快捷键!
    Linux下分析bin文件的10种方法
    Linux下几个与磁盘空间和文件尺寸相关的命令
    如何让你的脚本可以在任意地方都可执行?
  • 原文地址:https://www.cnblogs.com/summer520/p/3300424.html
Copyright © 2020-2023  润新知