实施定时业务one
执行定时业务one
spring执行定时任务
定义一个任务是很简单的实现TimerTask的run方法就可以了.
如下:SayHelloTask.java
spring执行定时任务
定义一个任务是很简单的实现TimerTask的run方法就可以了.
如下:SayHelloTask.java
1package test.timerTask; 2 3import java.util.TimerTask; 4 5public class Task extends TimerTask { 6 7 @Override 8 public void run() { 9 // TODO Auto-generated method stub 10 System.out.println("测试TimerTask : Hello !!"); 11 } 12 13} 1package test.springTimer; 2 3import java.util.TimerTask; 4 5public class Task2 extends TimerTask{ 6 public void run(){ 7 8 System.out.println("task2 is running"); 9 } 10 11 12} 13 然后是配置文件: <?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="sayHelloTask" class="test.springTimer.Task"></bean> <bean id="HelloTask" class="test.springTimer.Task2"></bean> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="sayHelloTask"/> </property> <!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档--> <property name="period"> <value>2000</value> </property> <!-- 延时1m 执行任务 --> <property name="delay"> <value>1000</value> </property> </bean> <bean id="scheduledTask2" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="HelloTask"/> </property> <!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档--> <property name="period"> <value>2000</value> </property> <!-- 延时1m 执行任务 --> <property name="delay"> <value>1000</value> </property> </bean> <!-- 启动定时器 --> <bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTask"/> <ref bean="scheduledTask2"/> </list> </property> </bean> </beans> 测试类如下:TestApp.java package test.timerTask; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml"); // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml"); } // 只要加载配置文件就可以了, } 运行结果: task2 is running task2 is running haha,task is running haha,task is running task2 is running 使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务. 1package test.timerTask; 2 3import org.quartz.JobExecutionContext; 4import org.quartz.JobExecutionException; 5import org.springframework.scheduling.quartz.QuartzJobBean; 6 7public class SayHelloTaskUsingQuartz extends QuartzJobBean { 8 9 @Override 10 protected void executeInternal(JobExecutionContext context) 11 throws JobExecutionException { 12 // TODO Auto-generated method stub 13 System.out.println("使用Quartz 认为调度: Hello!!"); 14 } 15 16} 17 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>test.timerTask.SayHelloTaskUsingQuartz</value> </property> </bean> <!-- 关键在如下两个触发器的配置 --> <!-- 类似于Java的简单触发器 --> <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="startDelay"> <value>1000</value> </property> <property name="repeatInterval"> <value>3000</value> </property> </bean> <!-- 复杂触发器 --> <bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="sayHelloJob"/> </property> <property name="cronExpression"> <!-- 关键在配置此表达式 --> <value>0 49 15 * * </value> </property> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="helloCronTrigger"/> </property> </bean> </beans>