spring中集成TimerTask执行定时任务

本文介绍如何使用Spring框架配置定时任务,包括自定义TimerTask、使用ScheduledTimerTask定义触发器信息及利用TimerFactoryBean自动创建Timer实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring对Timer的支持的核心是由ScheduledTimerTask和TimerFactoryBean类组成的。 ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。使用 TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。

1、自定义timerTask,比如:定时输出ServletContext中的信息,本例中输出项目的绝对路径(比如:D:\software\apache-tomcat-6.0.33\webapps\spring\)

Java代码 spring中集成TimerTask执行定时任务 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. publicclassBirthdayReminderTimerTaskextendsTimerTaskimplementsServletContextAware{
  2. /*通过实现ServletContextAware可获得servletContext*/
  3. privateServletContextservletContext;
  4. privatestaticLoggerlogger=Logger.getLogger(BirthdayReminderTimerTask.class);
  5. @Override
  6. publicvoidrun(){
  7. //logger.debug("BirthdayReminderTimerTaskisrunning");
  8. setServletContext(servletContext);
  9. try{
  10. System.out.println(this.servletContext.getRealPath("/"));
  11. }catch(Exceptione){
  12. e.printStackTrace();
  13. }
  14. }
  15. publicvoidsetServletContext(ServletContextservletContext){
  16. this.servletContext=servletContext;
  17. }
  18. }
public class BirthdayReminderTimerTask extends TimerTask implements ServletContextAware{
  /*通过实现ServletContextAware可获得servletContext*/
  private ServletContext servletContext;
  private static Logger logger = Logger.getLogger(BirthdayReminderTimerTask.class);
  @Override
  public void run() {
//    logger.debug("BirthdayReminderTimerTask is running");
    setServletContext(servletContext);
    try {
      System.out.println(this.servletContext.getRealPath("/"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void setServletContext(ServletContext servletContext) {
    this.servletContext = servletContext;    
  }
}

2、在spring的bean配置文件中配置,当系统加载该文件时,配置的定时器将自动启动,同时被spring管理。

Xml代码 spring中集成TimerTask执行定时任务 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. <!--自定义任务-->
  2. <beanid="birthdayReminder"class="com.jep.task.BirthdayReminderTimerTask"></bean>
  3. <!--ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。-->
  4. <beanid="birthdayReminderTimerTask"
  5. class="org.springframework.scheduling.timer.ScheduledTimerTask">
  6. <!--设置启动延迟-->
  7. <propertyname="delay">
  8. <value>3000</value>
  9. </property>
  10. <!--后续延迟-->
  11. <propertyname="period">
  12. <value>5000</value>
  13. </property>
  14. <!--指定触发器信息-->
  15. <propertyname="timerTask">
  16. <reflocal="birthdayReminder"/>
  17. </property>
  18. </bean>
  19. <!--使用TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTaskbean自动创建Timer实例。-->
  20. <beanid="timerFactory"
  21. class="org.springframework.scheduling.timer.TimerFactoryBean">
  22. <propertyname="scheduledTimerTasks">
  23. <list>
  24. <reflocal="birthdayReminderTimerTask"/>
  25. </list>
  26. </property>
  27. </bean>
    <!--自定义任务-->    
        <bean id="birthdayReminder" class="com.jep.task.BirthdayReminderTimerTask"></bean>  
          
        <!-- ScheduledTimerTask类是对TimerTask的包装器实现,这样你就可以为这个任务定义触发器信息。 -->  
        <bean id="birthdayReminderTimerTask"  
            class="org.springframework.scheduling.timer.ScheduledTimerTask">  
            <!-- 设置启动延迟 -->  
            <property name="delay">  
                <value>3000</value>  
            </property>  
            <!-- 后续延迟 -->  
            <property name="period">  
                <value>5000</value>  
            </property>  
            <!-- 指定触发器信息 -->  
            <property name="timerTask">  
                <ref local="birthdayReminder" />  
            </property>  
        </bean>  
          
        <!-- 使用TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。 -->  
        <bean id="timerFactory"  
            class="org.springframework.scheduling.timer.TimerFactoryBean">  
            <property name="scheduledTimerTasks">  
                <list>  
                    <ref local="birthdayReminderTimerTask" />               
                </list>  
            </property>          
        </bean>  

3、对于web项目,需要在web.xml中进行如下配置

Xml代码 spring中集成TimerTask执行定时任务 spring中集成TimerTask执行定时任务spring中集成TimerTask执行定时任务
  1. <!--SpringApplicationContext配置文件的路径此参数用于后面的Spring-Contextloader-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:beans.xml</param-value>
  5. </context-param>
  6. <!--SpringApplicationContext载入-->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值