|
定义一个任务是很简单的实现TimerTask的run方法就可以了. 如下: SayHelloTask.java
1
package
test.timerTask;2 ![]() 3
import
java.util.TimerTask;4 ![]() 5
public
class
Task
extends
TimerTask
{6 ![]() 7 @Override8 public void run() {9 // TODO Auto-generated method stub10 System.out.println("测试TimerTask : Hello !!");11 }12 ![]() 13 }
1
package
test.springTimer;2 ![]() 3
import
java.util.TimerTask;4 ![]() 5
public
class
Task2
extends
TimerTask
{6 public void run(){7 8 System.out.println("task2 is running");9 }10 ![]() 11 12 }
13
然后是配置文件:
<beans> <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
测试类如下: 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来执行一个复杂的任务.
1
package
test.timerTask;2 ![]() 3
import
org.quartz.JobExecutionContext;4
import
org.quartz.JobExecutionException;5
import
org.springframework.scheduling.quartz.QuartzJobBean;6 ![]() 7
public
class
SayHelloTaskUsingQuartz
extends
QuartzJobBean
{8 ![]() 9 @Override10 protected void executeInternal(JobExecutionContext context)11 throws JobExecutionException {12 // TODO Auto-generated method stub13 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 > ![]()
|
spring执行定时任务
本文介绍如何使用Spring框架执行定时任务,包括通过TimerTask和Quartz两种方式实现的任务调度,并提供了详细的代码实例及配置说明。


}
}
602

被折叠的 条评论
为什么被折叠?



