使用Spring来实现任务计划服务一:继承java.util.TimerTask
2011-05-01 15:41
881 查看
1、定义一个DemoTask类,并继承java.util.TimerTask.
package junit.test; import java.util.TimerTask; public class DemoTask extends TimerTask{ public void run(){ System.out.println("Task is excuted:"+System.currentTimeMillis()); } }2.在springConfig文件beans-task.xml中配置DemoTask bean定义.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="demoTask" class="junit.test.DemoTask" /> </beans>3.使用Sping的org.springframework.scheduling.timer.ScheduledTimerTask来定义执行周期。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="demoTask" class="junit.test.DemoTask" /> <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="demoTask"/> </property> <property name="period"> <value>6000</value><!-- 设置间隔时间,毫秒 --> </property> <property name="delay"> <value>2000</value><!-- 设置延迟时间开始执行 --> </property> </bean> </beans>4.使用Spring的org.springframework.scheduling.timer.TimerFactoryBean类来加入所有的任务计划。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="demoTask" class="junit.test.DemoTask" /> <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="demoTask"/> </property> <property name="period"> <value>6000</value><!-- 设置间隔时间,毫秒 --> </property> <property name="delay"> <value>2000</value><!-- 设置延迟时间开始执行 --> </property> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask"/> </list> </property> </bean> </beans>到这里,整个beans-task.xml文件配置完成,程序代码也完成,下面我们写个测试方法来测试测试....
package junit.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DemoTaskTestg { public static void main(String[] args){ new ClassPathXmlApplicationContext("beans-task.xml"); } }
相关文章推荐
- 使用Spring来实现任务计划服务二:不继承java.util.TimerTask
- 任务调度--使用java.util.Timer实现
- 任务调度--使用java.util.Timer实现
- 使用Spring来实现任务计划服务三:集成quartz任务调度框架
- 使用java.util.Timer实现任务调度
- 使用quartz和Spring-Task实现计划任务的四种方式
- 使用jse的TimerTask实现在一天中的某个区间段定时任务
- java计划任务调度框架quartz结合spring实现调度的配置实例代码
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- 使用java.util.Timer来周期性的执行制定的任务
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- 在Spring中使用 Java Timer 调度任务
- 使用JAVA在TOMCAT下实现计划任务监听器
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- 使用java.util.Timer来周期性的执行制定的任务
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Spring整合TimerTask实现定时任务调度
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- 关于Java Web 使用Spring中使用Quartz(定时调用、实现固定时间执行), 触发定时器(执行某些任务)的实例
- JAVA定时器实现之一(通过继承TimerTask)