防身刀什么牌子最好:spring 定时器
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 07:20:08
spring执行定时任务
定义一个任务是很简单的实现TimerTask的run方法就可以了.
如下:SayHelloTask.java
package test.timerTask;
2 import java.util.TimerTask;
3 public class SayHelloTask extends TimerTask {
4 public void run() {
5 // TODO Auto-generated method stub
6 System.out.println("测试TimerTask : Hello !!");
7 }
8 }
然后是配置文件:
xml version="1.0" encoding="UTF-8" ?>
2
3 < beans >
4 < bean id ="sayHelloTask" class ="test.timerTask.SayHelloTask" > bean >
5 < bean id ="scheduledTask" class ="org.springframework.scheduling.timer.ScheduledTimerTask" >
6 < property name ="timerTask" >
7 < ref bean ="sayHelloTask" />
8 property >
9
10 < property name ="period" >
11 < value > 2000 value >
12 property >
13
14 < property name ="delay" >
15 < value > 1000 value >
16 property >
17 bean >
18
19 < bean id ="timerBean" class ="org.springframework.scheduling.timer.TimerFactoryBean" >
20 < property name ="scheduledTimerTasks" >
21 < list >
22 < ref bean ="scheduledTask" />
23 list >
24 property >
25 bean >
26 beans >
27 测试类如下:TestApp.java package test.timerTask;
2 import org.springframework.context.ApplicationContext;
3 import org.springframework.context.support.ClassPathXmlApplicationContext;
4 public class TestApp {
5 /** */ /**
6 * @param args
7 */
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 ApplicationContext context = new ClassPathXmlApplicationContext( " test/timerTask/javaTimer.xml " );
11 // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
12 }
13 // 只要加载配置文件就可以了,
14 }
15 使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务.
首先制定一个任务, 实现QuartzJobBean中的方法. package test.timerTask;
2 import org.quartz.JobExecutionContext;
3 import org.quartz.JobExecutionException;
4 import org.springframework.scheduling.quartz.QuartzJobBean;
5 public class SayHelloTaskUsingQuartz extends QuartzJobBean {
6 protected void executeInternal(JobExecutionContext context)
7 throws JobExecutionException {
8 // TODO Auto-generated method stub
9 System.out.println("使用Quartz 认为调度: Hello!!");
10 }
11 }
12 配置代码如下:quartzTimer.xml
2
3
4
5
6test.timerTask.SayHelloTaskUsingQuartz
7
8
9
10
11
12
13
14
15
161000
17
18
193000
20
21
22
23
24
25
26
27
28
290 49 15 * * ?
30
31
32
33
34
35
36
37
38 关于简单触发器和复杂触发器,查考下面的解释:
Quartz设计者做了一个设计选择来从调度分离开作业。Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。典型地,如果你需要在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。另一方面,如果你有许多复杂的作业调度,那么或许需要CronTrigger。
CronTrigger是基于Calendar-like调度的。当你需要在除星期六和星期天外的每天上午10点半执行作业时,那么应该使用CronTrigger。正如它的名字所暗示的那样,CronTrigger是基于Unix克隆表达式的。
作为一个例子,下面的Quartz克隆表达式将在星期一到星期五的每天上午10点15分执行一个作业。
0 15 10 ? * MON-FRI
下面的表达式
0 15 10 ? * 6L 2002-2005
将在2002年到2005年的每个月的最后一个星期五上午10点15分执行作业。
你不可能用SimpleTrigger来做这些事情。你可以用两者之中的任何一个,但哪个跟合适则取决于你的调度需要
定义一个任务是很简单的实现TimerTask的run方法就可以了.
如下:SayHelloTask.java
package test.timerTask;
2 import java.util.TimerTask;
3 public class SayHelloTask extends TimerTask {
4 public void run() {
5 // TODO Auto-generated method stub
6 System.out.println("测试TimerTask : Hello !!");
7 }
8 }
然后是配置文件:
xml version="1.0" encoding="UTF-8" ?>
2
3 < beans >
4 < bean id ="sayHelloTask" class ="test.timerTask.SayHelloTask" > bean >
5 < bean id ="scheduledTask" class ="org.springframework.scheduling.timer.ScheduledTimerTask" >
6 < property name ="timerTask" >
7 < ref bean ="sayHelloTask" />
8 property >
9
10 < property name ="period" >
11 < value > 2000 value >
12 property >
13
14 < property name ="delay" >
15 < value > 1000 value >
16 property >
17 bean >
18
19 < bean id ="timerBean" class ="org.springframework.scheduling.timer.TimerFactoryBean" >
20 < property name ="scheduledTimerTasks" >
21 < list >
22 < ref bean ="scheduledTask" />
23 list >
24 property >
25 bean >
26 beans >
27 测试类如下:TestApp.java package test.timerTask;
2 import org.springframework.context.ApplicationContext;
3 import org.springframework.context.support.ClassPathXmlApplicationContext;
4 public class TestApp {
5 /** */ /**
6 * @param args
7 */
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 ApplicationContext context = new ClassPathXmlApplicationContext( " test/timerTask/javaTimer.xml " );
11 // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
12 }
13 // 只要加载配置文件就可以了,
14 }
15 使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务.
首先制定一个任务, 实现QuartzJobBean中的方法. package test.timerTask;
2 import org.quartz.JobExecutionContext;
3 import org.quartz.JobExecutionException;
4 import org.springframework.scheduling.quartz.QuartzJobBean;
5 public class SayHelloTaskUsingQuartz extends QuartzJobBean {
6 protected void executeInternal(JobExecutionContext context)
7 throws JobExecutionException {
8 // TODO Auto-generated method stub
9 System.out.println("使用Quartz 认为调度: Hello!!");
10 }
11 }
12 配置代码如下:quartzTimer.xml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 关于简单触发器和复杂触发器,查考下面的解释:
Quartz设计者做了一个设计选择来从调度分离开作业。Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。典型地,如果你需要在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。另一方面,如果你有许多复杂的作业调度,那么或许需要CronTrigger。
CronTrigger是基于Calendar-like调度的。当你需要在除星期六和星期天外的每天上午10点半执行作业时,那么应该使用CronTrigger。正如它的名字所暗示的那样,CronTrigger是基于Unix克隆表达式的。
作为一个例子,下面的Quartz克隆表达式将在星期一到星期五的每天上午10点15分执行一个作业。
0 15 10 ? * MON-FRI
下面的表达式
0 15 10 ? * 6L 2002-2005
将在2002年到2005年的每个月的最后一个星期五上午10点15分执行作业。
你不可能用SimpleTrigger来做这些事情。你可以用两者之中的任何一个,但哪个跟合适则取决于你的调度需要
spring 定时器
Spring定时器
Spring定时器1
Spring定时器111
Spring定时器a
Spring定时器B
Spring 中的定时器TimerTask 与 quartz的使用
stm32 定时器
洗衣机电子定时器
定时器和时间管理
PLC定时器工作原理
H8/3048定时器设置
VC定时器SetTimer函数
标准C语言定时器
Linux定时器的使用
555定时器的应用
spring festival
Spring colors
【引用】Spring
Spring入门教程
Spring 入门教程
Spring 系列: Spring 框架简介
【转载】技术系列之 定时器
在Excel里使用定时器