Oracle神谕

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  284 随笔 :: 9 文章 :: 106 评论 :: 0 Trackbacks
 定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行财务挂账,数据转存,新闻联播等等操作。

    而在Spring里,已经很好的集成了Quartz,简单到像配cron一样,在xml文件里面配一下时间就可以自动执行,不需要写一行代码。Spring对Quartz大刀阔斧的简化堪称范例,Quartz项目组也许可以学习一下。

    <bean id="methodInvokingJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject"><ref bean="financeDAO"/></property>
        <property name="targetMethod"><value>confirmOrder</value></property>
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="methodInvokingJobDetail"/>
        </property>
        <property name="cronExpression">
            <value>0 0 6,12,20 * * ?</value>
        </property>
    </bean>
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list><ref local="cronTrigger"/></list>
        </property>
    </bean>

上面这段配置文件规定了在早上6点和晚上8点执行financeDAO对象的confirmOrder()方法.


附:cronExpression配置说明

字段   允许值   允许的特殊字符
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

posted on 2005-07-20 21:37 java世界畅谈 阅读(15582) 评论(42)  编辑  收藏 所属分类: Spring

评论

# re: Spring--简单使用quartz实现定时作业 2005-11-06 18:41 else
这么多人看了这篇日志,竟然没人回复,太不厚道了,看来都是google过来的
花了不短的时间仔细研究了quartz的内部代码
才发现spring都已经把事情做完了,sigh  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-11-08 11:57 wtigter
不错,允许的特殊字符,包括那些东西。  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-11-08 11:58 wtigter
能解释下后面的字符的用法吗?
wtigter@163.com  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-11-08 12:04 wtigter
<property name="cronExpression">
<value>0 0 6,12,20 * * ?</value>
</property>

这里是6,12,20,怎么你说是定时在
在早上6点和晚上8点执行

那12点不执行吗  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-11-08 14:12 wtigter
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".
  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-11-19 10:30 kcb
请问,如果所执行的方法需要用户提供参数应该怎么做?谢谢!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-12-14 10:42 丫梨
嗨,那你能不能告诉我,如何在服务器启动的时候就调用某个类中的一个方法呢?我想这个方法只在服务器启动的时候才调用一次。如果你知道的话,请告诉我哦,我的邮箱是wjljava@163.com,真的是非常感谢!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-12-14 23:37 jon
我也用这个,配置的是<property name="cronExpression">
<value>0 0 3 * * ?</value>
</property>早上3点运行,在TOMCAT下能很好启动一次任务,而在SUNONE服务器下,会同时启动两个定时任务,真奇怪,搞得插入记录总是重复,且重复生成的时间只相差几秒而已,有哪位知道呀?  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2005-12-14 23:38 jon
我的EMAIL:jon.xiong@163.com  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-03-08 15:19 klt8848
好东东!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-04-03 10:14 avgi
@丫梨
可以在spring初始化bean时做点手脚。  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-05-18 10:32 对冲
@kcb
@wtigter
应该执行吧,估计是少写了  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-06-21 14:59 frankie
好东西!为了避免被说不厚道,嘿嘿
  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-07-31 17:58 长征路上的妖怪
有人用quartz吗?CronTrigger中的若干个构造方法中的String类型是什么用处呢?  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-08-03 17:11 flyhawk1010
好文!正好项目要用到!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-08-04 10:28 潘晓欢
financeDAO对象怎么建立,能不能给完整点
÷  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-08-04 12:51 terry0431
怎样实现动态设置任务?不重起服务  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-08-10 14:04 kevin.guo
quartz的高级特性不仅如此
1 数据库存储
2 集群支持
3 数据库持久化任务,trigger
4 trigger 的停止,运行
5 任务的任意添加
6 比corntrigger 更详尽的任务安排
7 线程的内部数据交换  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-09-11 15:14 gLaiVe
@kevin.guo

你说的trigger的停止,能不能动态设置的呢,比如我想在任何时刻停止当前正在跑得task,我现在的quartz是配置在spring里面了,怎么用spring来控制  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-09-16 18:05 xiaqingfeng
为什么我照你的写会出一个异常
Failure obtaining db row lock: Table 'onhave.qrtz_locks' doesn't exist [See nested exception: java.sql.SQLException: Table 'onhave.qrtz_locks' doesn't exist]  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-09-18 22:39 xiaqingfeng
请高手指点一下啊,为什么会报异常?难道一定要建表吗?可我不想建表  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2006-11-03 18:25 kevin.guo
你说的trigger的停止,能不能动态设置的呢,

我没用spring集成,做法是job里调用trigger的停方法,这样trigger驱动的job都不会被执行了  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2007-03-21 16:29 helloint
cronExpression配置说明

这个说的太简单拉..  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2007-04-10 11:08 houhy
一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素。从左至右,这些元素的定义如下:

1.秒(0–59)

2.分钟(0–59)

3.小时(0–23)

4.月份中的日期(1–31)

5.月份(1–12或JAN–DEC)

6.星期中的日期(1–7或SUN–SAT)

7.年份(1970–2099)  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2007-11-06 18:14 fenix
好  回复  更多评论
  

# re:sss 2007-11-26 10:13 dfsf
Good java spring   回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2007-11-28 14:32 meizi
真棒!!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-01-02 23:01 linuxboy
VERY good!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-01-09 15:54 quarts
多谢楼主,不错的文章。我通过你的文章配置了Quarts的应用,但在tomcat上使用时,出现过只能采集一次,之后那线程一直阻塞状态没法定时下去,该怎么办?  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 [未登录] 2008-01-21 12:28 leo
up 。。。。。。。。。  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-01-28 10:07 df
good  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 [未登录] 2008-03-30 09:29 aa
good
  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-04-11 13:34 初级
<bean id="fileWriteTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="fileWriteDetail"/>
</property>
<property name="cronExpression">
<value>0 27 10 ? * *</value>
</property>
</bean>
这个job执行了一次,我修改为“<value>0 27 13 ? * *</value>”就不再自动执行,再改为其他时间,也不再自动执行。我重启服务也没用。谁遇见到这个问题啊?  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-05-19 09:30 asd
不错,学习了。  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-07-18 16:37 zlj
学习中。。。  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-07-30 18:03 飞越海面
我想请问一下对于现在的spring中集成的还是这样的配置方式吗?  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-07-31 15:26 black_boy
太好了,解了我燃眉之急  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 [未登录] 2008-08-24 13:51 wz
正要用这个,好啊,谢谢!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2008-10-27 16:00 9527
为什么我跟你写的一样 但是我的TOMCAT启动之后 没有反应呢?请高手指点 多谢啦  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2009-01-13 22:14 odin
真庆幸可以看到你的文章,对我太有帮助了!  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2009-10-19 14:34 coding
不错 很有帮助 学习了  回复  更多评论
  

# re: Spring--简单使用quartz实现定时作业 2009-12-04 19:35 baizhiguo
我的配置文件和你的一样但是就是报错:错误内容是找不到:org.springframework.scheduling.quartz.CronTriggerBean,那个包也有了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: