论坛首页 Java企业应用论坛

spring对定时任务调度集成的总结

浏览 10019 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-03-09  
1.TimerTask
>>配置文件default-lazy-init="false"如果为true则不会启动Timer

>>INFO [org.springframework.scheduling.timer.TimerFactoryBean] - Initializing Timer
表示启动了定时.

>>但是用AbstractDependencyInjectionSpringContextTests这个类来加载spring的配置文件后虽然启动了定时任务,但是马上执行以下步骤:
Closing application context
Destroying singletons in...
Cancelling Timer
所以定时任务不能执行.
>>如果使用new ClassPathXmlApplicationContext(String[])来加载配置文件,则在Initializing Timer后就程序就退出了,真是怪事.

>>用org.springframework.web.context.ContextLoaderListener 在web应用环境下加载配置文件可以正确执行定时任务.
spring bean配置如下:
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
	<property name="delay" value="1000" />
	 <property name="period" value="50000" />
	 <property name="timerTask" ref="scoringService" />
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
	  <property name="scheduledTimerTasks">
	        <list>
	            <ref bean="scheduledTask" />
	        </list>
	 </property>
	 <property name="daemon" value="true"/>
</bean>

2.quartz
>>使用autowire by name的禁忌:不要有叫dataSource的bean存在.
见http://eyejava.iteye.com/blog/58281

>>cronExpression和unix cronTab文件的区别
cronTab没有秒 和 年这两个栏位,cronTab 的星期天用0表示,cronExpression用7表示

>>cronExpression:
引用

字段     允许值     允许的特殊字符
秒   0-59   , - * /
分   0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.
The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.
The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.
The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.
The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".
The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.
The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".


   发表时间:2007-03-09  
使用在web容器中使用quartz要小心了,会造成问题,robbin以前说过这个问题,我在项目中也碰到过服务器内存无法被释放的问题,后来把quartz去掉就好了
0 请登录后投票
   发表时间:2007-03-09  
后面乱七八糟的,需要整理下。文档里的内容就不要再复制出来了,参考就行了。
0 请登录后投票
   发表时间:2007-03-09  
Ivan Li 写道
使用在web容器中使用quartz要小心了,会造成问题,robbin以前说过这个问题,我在项目中也碰到过服务器内存无法被释放的问题,后来把quartz去掉就好了

有具体点的原因吗? 不能仅仅因为"小心" 两字就不用了吧.
要调用spring管理的bean,又要达到cron表达式的定时配置,不用spring的集成,还有什么更方便的解决办法?
0 请登录后投票
   发表时间:2007-03-09  
http://www.iteye.com/topic/40970
看看这篇贴子4楼,robbin的话在我的项目中得到了证实,所以我不在web 容器里用quartz了,解决方案robbin说了
0 请登录后投票
   发表时间:2007-03-09  
Ivan Li 写道
使用在web容器中使用quartz要小心了,会造成问题,robbin以前说过这个问题,我在项目中也碰到过服务器内存无法被释放的问题,后来把quartz去掉就好了


我觉得还是要视具体情况而论。也有可能视你的同步程序本身有问题。

我曾经做过一个用于同步的程序,一样在web容器中,使用Spring的封装调用quartz,跑了一年多,一点问题也没有。当然这不是绝对的,要看具体的业务了。
0 请登录后投票
   发表时间:2007-03-09  
文章下半部分实在是太乱了
0 请登录后投票
   发表时间:2007-03-09  
downpour 写道
Ivan Li 写道
使用在web容器中使用quartz要小心了,会造成问题,robbin以前说过这个问题,我在项目中也碰到过服务器内存无法被释放的问题,后来把quartz去掉就好了


我觉得还是要视具体情况而论。也有可能视你的同步程序本身有问题。

我曾经做过一个用于同步的程序,一样在web容器中,使用Spring的封装调用quartz,跑了一年多,一点问题也没有。当然这不是绝对的,要看具体的业务了。

我是用quartz定时发邮件提醒,但是造成了内存泄漏
0 请登录后投票
   发表时间:2007-03-09  
Ivan Li 写道
downpour 写道
Ivan Li 写道
使用在web容器中使用quartz要小心了,会造成问题,robbin以前说过这个问题,我在项目中也碰到过服务器内存无法被释放的问题,后来把quartz去掉就好了


我觉得还是要视具体情况而论。也有可能视你的同步程序本身有问题。

我曾经做过一个用于同步的程序,一样在web容器中,使用Spring的封装调用quartz,跑了一年多,一点问题也没有。当然这不是绝对的,要看具体的业务了。

我是用quartz定时发邮件提醒,但是造成了内存泄漏


不妨把你的程序片断发给我看一下,看看问题在哪里。
0 请登录后投票
   发表时间:2007-03-11  
泄露问题一般是对API是使用或者理解不当造成的。当然不排除程序的BUG,这个概率相对小点
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics