Spring使用TimerTask配置调度事务
来源:互联网 发布:知乎日报文章搜索不到 编辑:程序博客网 时间:2024/06/04 19:54
首先我们编写调度服务,继承java.util.TimerTask
import java.util.Date;
import java.util.TimerTask;
public class TimerService extends TimerTask ...{
public void run() ...{
System.out.println(new Date().getSeconds());
}
}
配置文件:
<!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>
测试代码:
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使用TimerTask配置调度事务
- Spring使用Quartz配置调度事务
- Spring使用Quartz配置调度事务
- 使用TimerTask进行调度
- Spring整合TimerTask实现定时任务调度
- Spring整合TimerTask实现定时任务调度
- spring事务的配置使用
- spring使用TransactionProxyFactoryBean配置事务
- Spring框架之TimerTask使用
- Spring、Spring事务详解;使用XML配置事务
- Spring使用Quartz配置调度事物
- Spring 任务调度配置及使用
- Spring实现后台的任务调度TimerTask和Quartz
- Spring配置使用编程式事务
- Spring使用TransactionProxyFactoryBean声明式事务配置
- spring 事务一致性使用xml配置
- spring jdbc 事务配置----TransactionTemplate使用
- Spring MVC 事务配置及使用
- 怎么改变jpg图片的缩略图
- 我用一天时间做了一个MTK版本【转】
- 文件上传组件的应用与编写
- 嵌入式RISC CPU硬核LEEF设计启动
- vc中获得命令行参数
- Spring使用TimerTask配置调度事务
- 考研日志-29
- KFC 打工日
- 【vc】软件启动画面之浅入深出
- 区别 String 类 和 StringBuffer 类 -Java 学习笔记 (19)
- IronPython脚本调用C#实现的dll库
- 搞笑视频-石家庄某帅哥视频
- DIV Based Layout with CSS
- 大家帮个忙啊`` 急急急