spring定时器代码解读.docx
- 文档编号:30550108
- 上传时间:2023-08-16
- 格式:DOCX
- 页数:18
- 大小:23.31KB
spring定时器代码解读.docx
《spring定时器代码解读.docx》由会员分享,可在线阅读,更多相关《spring定时器代码解读.docx(18页珍藏版)》请在冰豆网上搜索。
spring定时器代码解读
applicationContext.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2. 3.xmlns="http: //www.springframework.org/schema/beans" 4.xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" 5.xsi: schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 6. 7. 8. 9. lazy-init="default" autowire="default" dependency-check="default"> 10. 11. 12. 13. 14. 15. -- spring定时器 --> 16. -- 方法一 --> 17. -- 第一步 声明一个定时任务,该类extends java.util.TimerTask --> 18. 19. 20. -- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 --> 21. 22. 24. 25. 26. 27. --这里是每隔多长时间就进行一次计时任务,单位ms--> 28. 29. 30. 31. --这里是服务启动后延时多少时间,开始计时任务,单位ms--> 32. 33. 34. 35. -- 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 --> 36. 37. 38. 40. 41. 42. 43. 44. -- 方法二 --> 45. -- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 --> 46. 47. 48. 49. 50. 51. 52. -- 第二步 调度定时任务 --> 53. --这种配置与第一种方法效果一样 54. 55. 57. 58. 59. 60. 这里是服务启动后延时多少时间,开始计时任务,单位ms 61. 62. 63. 64. 这里是每隔多长时间就进行一次计时任务,单位ms 65. 66. 67.--> 68. -- 这种配置可以精确几点执行定时任务 --> 69. 70. 72. 73. 74. --定时在任何月份任何日期(不管星期几)的22点52分0秒 --> 75. -- 一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下: 76. 1.秒(0-59) 77. 2.分钟(0-59) 78. 3.小时(0-23) 79. 4.月份中的是期(1-31) 80. 5.月份(1-12或SUN-DEC) 81. 6.星期中的日期(1-7或SUN-SAT) 82. 7.年份(1970-2099) 83. 例子: 84. 0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 85. 0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 86. 30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 87. 0 0 8-5 ? * MON-FRI 每个工作日的工作时间 88. 89. - 区间 90. * 通配符 91. ? 你不想设置那个字段 92. --> 93. 94. 95. 96. --第三步 启动定时任务,注意这里的ref bean --> 97. 98. 99. 101. 102. 103. 104.
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd"> lazy-init="default"autowire="default"dependency-check="default"> --spring定时器--> --方法一--> --第一步声明一个定时任务,该类extendsjava.util.TimerTask--> --第二步调度定时任务,把声明的定时任务注入进来,并设置定时参数--> --这里是每隔多长时间就进行一次计时任务,单位ms--> --这里是服务启动后延时多少时间,开始计时任务,单位ms--> --启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务--> --方法二--> --第一步声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象--> --第二步调度定时任务--> --这种配置与第一种方法效果一样 这里是服务启动后延时多少时间,开始计时任务,单位ms 这里是每隔多长时间就进行一次计时任务,单位ms --> --这种配置可以精确几点执行定时任务--> --定时在任何月份任何日期(不管星期几)的22点52分0秒--> --一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下: 1.秒(0-59) 2.分钟(0-59) 3.小时(0-23) 4.月份中的是期(1-31) 5.月份(1-12或SUN-DEC) 6.星期中的日期(1-7或SUN-SAT) 7.年份(1970-2099) 例子: 0010,14,16**? 每天上午10点,下午2点和下午4点 00,15,30,45*1-10*? 每月前10天每隔15分钟 300011? 2012在2012年1月1日午夜过30秒时 008-5? *MON-FRI每个工作日的工作时间 -区间 *通配符 ? 你不想设置那个字段 --> --第三步启动定时任务,注意这里的refbean-->
clock.java
Java代码
1.import java.util.TimerTask;
2.
3.public class Clock extends TimerTask{
4.
5.@Override
6.public void run() {
7.
8. System.out.println("clock..!
clock....!
.......");
9.
10.}
11.
12.}
importjava.util.TimerTask;
publicclassClockextendsTimerTask{
@Override
publicvoidrun(){
System.out.println("clock..!
clock....!
.......");
}
}
QuartzClock.java
Java代码
1.import org.quartz.JobExecutionContext;
2.import org.quartz.JobExecutionException;
3.import org.springframework.scheduling.quartz.QuartzJobBean;
4.
5.public class QuartzClock extends QuartzJobBean {
6.
7.@Override
8.protected void executeInternal(JobExecutionContext arg0)
9. throws JobExecutionException {
10.
11. System.out.println("QuartzClock..!
QuartzClock....!
.......");
12.}
13.
14.}
15.
16.
17.SpringTest .java
18.package cn.xg.spring;
19.
20.import org.apache.log4j.Logger;
21.import org.springframework.context.ApplicationContext;
22.import org.springframework.context.support.ClassPathXmlApplicationContext;
23.
24.public class SpringTest {
25.
26./**
27.* @param args
28.*/
29.public static Logger log = Logger.getLogger(SpringTest.class);
30.
31.public static void main(String[] args) {
32.
33.
34.
35. //第一种写法(加载配置文件)
36. ApplicationContext ctx = new
37. ClassPathXmlApplicationContext("applicationContext.xml");
38.
39. //第二种写法
40. //ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
41. //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
42.
43. //加载多个配置文件
44. // ApplicationContext ctx = new ClassPathXmlApplicationContext(
45. //new String[]{"applicationContext.xml","applicationContext2.xml"} );
46.
47.
48.}
49.
50.}
当有任务触发时,spring会去调用quartzJob的execute()方法,在这个方法里我们就可以写一些我们自己的业务操作。
上面只是说了定时和任务出发时的处理,下面再看看如何动态的添加定时任务
packagecom.actmaps.scheduler;
importjava.text.ParseException;
importmons.logging.Log;
importmons.logging.LogFactory;
importorg.quartz.CronTrigger;
importorg.quartz.Scheduler;
importorg.quartz.SchedulerException;
importorg.quartz.SimpleTrigger;
importorg.quartz.impl.StdSchedulerFactory;
/**
*TODO设置定时任务的控制类
*@authorPHeH
*CreatedOn16:
11:
24
*/
publicclassSchedule{
/**
*TODO添加一个新的触发器
*@return
*@authorPHeH
*CreatedOn2006-6-916:
24:
54
*/
publicbooleaninsertTrigger()
{
try{
Schedulerscheduler=(Scheduler)StdSchedulerFactory.getDefaultScheduler();
CronTriggernewCronTrigger=newCronTrigger();
try{
newCronTrigger.setName("newCronTrigger");
newCronTrigger.setCronExpression(this.formatQuartzString());
newCronTrigger.setJobName("quartzJob");
}catch(ParseExceptione){
e.printStackTrace();
log.error("解析触发器字符串格式出错:
"+e.getMessage());
}
scheduler.scheduleJob(newCronTrigger);
}catch(SchedulerExceptione1){
e1.printStackTrace();
log.error("添加新触发器时发生SchedulerException异常:
"+e1.getMessa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring 定时器 代码 解读