《pro Spring》学习笔记之使用JDK Timer调度任意任务
2007-09-10 23:52
513 查看
Spring对Timer的支持核心是由ScheduledTimerTask和TimerFactoryBean类组成,前者扮演后者一个wrapper的角色,通过 TimerFactoryBean,你能够让Spring使用配置创建触发器,为一指定的ScheduledTimerTask bean自动创建Timer实例
我们一般可以编写TimerTask的类,或者ScheduledTimerTas类的子类,实现其run或者getDelay方法来完成具体的定时任务,但是,基于POJ的设计原则,我们希望对一个普通的java中的方法,进行调度,该如何处理呢,请看以下代码
POJO:
package ch14.AnyTimerTask;
import java.util.TimerTask;
import java.util.TimerTask;
public class HelloWorldTask ...{
public void someJob(String message) ...{
System.out.println(message);
}
}
配置文件
核心是MethodInvokingTimerTaskFactoryBean
<?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.0.xsd">
<bean id="target" class="ch14.AnyTimerTask.HelloWorldTask">
</bean>
<bean id="task" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject">
<ref bean="target"/>
</property>
<property name="targetMethod">
<value>someJob</value>
</property>
<property name="arguments">
<value>hello world 3q!</value>
</property>
</bean>
<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay">
<value>1000</value>
</property>
<property name="period">
<value>2000</value>
</property>
<property name="timerTask">
<ref bean="task"/>
</property>
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<ref bean="timerTask"/>
</property>
</bean>
</beans>
测试代码:
package ch14.AnyTimerTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test ...{
/** *//**
* @param args
*/
public static void main(String[] args) throws Exception...{
ApplicationContext context=new ClassPathXmlApplicationContext("ch14/AnyTimerTask/applicationContext.xml");
System.in.read();
}
}
运后可以看到,延迟1000毫秒后,每个2000毫秒便运行一次somJob方法,其中我们用arguments参数定义了方法的参数
我们一般可以编写TimerTask的类,或者ScheduledTimerTas类的子类,实现其run或者getDelay方法来完成具体的定时任务,但是,基于POJ的设计原则,我们希望对一个普通的java中的方法,进行调度,该如何处理呢,请看以下代码
POJO:
package ch14.AnyTimerTask;
import java.util.TimerTask;
import java.util.TimerTask;
public class HelloWorldTask ...{
public void someJob(String message) ...{
System.out.println(message);
}
}
配置文件
核心是MethodInvokingTimerTaskFactoryBean
<?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.0.xsd">
<bean id="target" class="ch14.AnyTimerTask.HelloWorldTask">
</bean>
<bean id="task" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject">
<ref bean="target"/>
</property>
<property name="targetMethod">
<value>someJob</value>
</property>
<property name="arguments">
<value>hello world 3q!</value>
</property>
</bean>
<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay">
<value>1000</value>
</property>
<property name="period">
<value>2000</value>
</property>
<property name="timerTask">
<ref bean="task"/>
</property>
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<ref bean="timerTask"/>
</property>
</bean>
</beans>
测试代码:
package ch14.AnyTimerTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test ...{
/** *//**
* @param args
*/
public static void main(String[] args) throws Exception...{
ApplicationContext context=new ClassPathXmlApplicationContext("ch14/AnyTimerTask/applicationContext.xml");
System.in.read();
}
}
运后可以看到,延迟1000毫秒后,每个2000毫秒便运行一次somJob方法,其中我们用arguments参数定义了方法的参数
相关文章推荐
- 任务调度(一)——jdk自带的Timer
- 任务调度(二)——jdk自带的Timer 动态修改任务执行计划
- 使用 Timer 实现任务调度(定时任务)
- 任务调度--使用java.util.Timer实现
- 任务调度--使用java.util.Timer实现
- 任务调度(一)——jdk自带的Timer
- spring任务调度方法之JDK Timer
- 在Spring中使用 Java Timer 调度任务
- 任务调度(一)——jdk自带的Timer
- 定时任务调度之Timer---综合使用小案例
- JDK TimerTask任务使用
- 使用java.util.Timer实现任务调度
- 任务调度(二)——jdk自带的Timer 动态修改任务执行计划
- 使用Timer+servletListener实现web服务的任务调度
- 多线程应用:用TimerTask于Timer任意时间调度任务
- 使用Timer类去调度任务——java
- 在Spring中使用JDK定时器实现调度任务
- 简单的使用java Timer调度任务
- 在Spring中使用JDK定时器实现调度任务
- 使用 Timer 进行任务调度