`
esffor
  • 浏览: 1379103 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring使用TimerTask配置调度事务

阅读更多

首先我们编写调度服务,继承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());        
    }

}

配置文件:

 

<?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>

 

测试代码:

 

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 in org.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

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



分享到:
评论

相关推荐

    Spring.3.x企业应用开发实战(完整版).part2

    4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...

    spring中如何配置定时器.doc

    在Spring框架中,配置定时器是为了实现周期性或定时的任务调度,这对于执行如数据清理、报告生成等后台操作非常有用。在提供的文档中,我们看到的是使用Spring的Timer模块来配置定时任务的方法。下面将详细介绍如何...

    Spring3.x企业应用开发实战(完整版) part1

    4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...

    spring几种定时执行任务方法 TimeTask Quartz

    本文将深入探讨Spring框架下实现定时任务的几种主流方法,包括使用`java.util.Timer`(即Java定时器)、Spring定时器以及Quartz定时器,旨在帮助开发者根据具体业务场景选择最合适的技术方案。 ### 1. Java定时器...

    java中spring与Quartz 整合定时任务.docx

    - 在Spring配置文件中,使用`MethodInvokingJobDetailFactoryBean`来定义Job。这个Bean将调用指定对象的某个方法作为定时任务的执行逻辑。例如: ```xml &lt;bean id="methodInvokingJobDetail" class="org.spring...

    java Timer定时器实例 Spring实例.doc

    通过使用Spring的定时器,我们可以将定时任务集成到Spring应用中,实现更加灵活和强大的任务调度。例如,我们可以利用 `@Scheduled` 注解来定义一个定时任务: ```java import org.springframework.scheduling....

    time-task-demo.zip

    它支持 cron 表达式,可以实现复杂的定时规则,并且与Spring的完整功能集(如AOP、事务管理等)无缝集成。`@Scheduled`任务是线程安全的,非常适合在Spring Boot应用中使用。 4. **Quartz** Quartz是开源的作业...

    timer_job_test.zip

    它可以通过配置或者注解的方式进行任务调度,且可以方便地与其他Spring功能集成。 文件名`canteen_transaction`可能表示该系统包含了食堂交易的处理部分。在Java中,处理交易数据可能涉及到数据库操作,例如使用...

    Java定时器在Web中的应用.doc

    Spring定时器允许更灵活的配置,如基于cron表达式的时间触发,且能更好地集成到Spring管理的bean中,便于依赖注入和事务控制。 总结,Java定时器在Web中的应用通过`java.util.Timer`和`ServletContextListener`可以...

    Java定时操作数据库测试Demo

    在实际项目中,可能还需要考虑异常处理、连接池管理(如C3P0或HikariCP)、数据库事务控制以及定时任务的灵活性和可扩展性,比如使用Spring的`@Scheduled`注解或Quartz等更强大的定时任务框架。 至于压缩包中的...

    最新JAVA华为面试题

    - Java中可以使用`java.util.Timer`和`java.util.TimerTask`来实现定时任务,或者使用Java 5引入的`java.util.concurrent.ScheduledExecutorService`,后者提供了更强大的定时和调度功能。 6. **Java容器**: - ...

    jsp+struts2数据采集系统

    这通常涉及到Java的定时器API,如`java.util.Timer`和`java.util.TimerTask`,或者使用更高级的调度库如Quartz。定时任务的设计和实现,包括任务的触发策略、异常处理和任务间的协调,是理解数据采集系统工作原理的...

Global site tag (gtag.js) - Google Analytics