Oracle神諭

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            284 隨筆 :: 9 文章 :: 106 評論 :: 0 Trackbacks
           定時批處理作業(yè)是J2EE企業(yè)應(yīng)用里很重要的一環(huán),用來在晚間進行財務(wù)掛賬,數(shù)據(jù)轉(zhuǎn)存,新聞聯(lián)播等等操作。

              而在Spring里,已經(jīng)很好的集成了Quartz,簡單到像配cron一樣,在xml文件里面配一下時間就可以自動執(zhí)行,不需要寫一行代碼。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>

          上面這段配置文件規(guī)定了在早上6點和晚上8點執(zhí)行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世界暢談 閱讀(15606) 評論(42)  編輯  收藏 所屬分類: Spring

          評論

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-11-06 18:41 else
          這么多人看了這篇日志,竟然沒人回復(fù),太不厚道了,看來都是google過來的
          花了不短的時間仔細研究了quartz的內(nèi)部代碼
          才發(fā)現(xiàn)spring都已經(jīng)把事情做完了,sigh  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-11-08 11:57 wtigter
          不錯,允許的特殊字符,包括那些東西。  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-11-08 11:58 wtigter
          能解釋下后面的字符的用法嗎?
          wtigter@163.com  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-11-08 12:04 wtigter
          <property name="cronExpression">
          <value>0 0 6,12,20 * * ?</value>
          </property>

          這里是6,12,20,怎么你說是定時在
          在早上6點和晚上8點執(zhí)行

          那12點不執(zhí)行嗎  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 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".
            回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-11-19 10:30 kcb
          請問,如果所執(zhí)行的方法需要用戶提供參數(shù)應(yīng)該怎么做?謝謝!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-12-14 10:42 丫梨
          嗨,那你能不能告訴我,如何在服務(wù)器啟動的時候就調(diào)用某個類中的一個方法呢?我想這個方法只在服務(wù)器啟動的時候才調(diào)用一次。如果你知道的話,請告訴我哦,我的郵箱是wjljava@163.com,真的是非常感謝!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-12-14 23:37 jon
          我也用這個,配置的是<property name="cronExpression">
          <value>0 0 3 * * ?</value>
          </property>早上3點運行,在TOMCAT下能很好啟動一次任務(wù),而在SUNONE服務(wù)器下,會同時啟動兩個定時任務(wù),真奇怪,搞得插入記錄總是重復(fù),且重復(fù)生成的時間只相差幾秒而已,有哪位知道呀?  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2005-12-14 23:38 jon
          我的EMAIL:jon.xiong@163.com  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-03-08 15:19 klt8848
          好東東!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-04-03 10:14 avgi
          @丫梨
          可以在spring初始化bean時做點手腳。  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-05-18 10:32 對沖
          @kcb
          @wtigter
          應(yīng)該執(zhí)行吧,估計是少寫了  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-06-21 14:59 frankie
          好東西!為了避免被說不厚道,嘿嘿
            回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-07-31 17:58 長征路上的妖怪
          有人用quartz嗎?CronTrigger中的若干個構(gòu)造方法中的String類型是什么用處呢?  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-08-03 17:11 flyhawk1010
          好文!正好項目要用到!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-08-04 10:28 潘曉歡
          financeDAO對象怎么建立,能不能給完整點
          ÷  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-08-04 12:51 terry0431
          怎樣實現(xiàn)動態(tài)設(shè)置任務(wù)?不重起服務(wù)  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-08-10 14:04 kevin.guo
          quartz的高級特性不僅如此
          1 數(shù)據(jù)庫存儲
          2 集群支持
          3 數(shù)據(jù)庫持久化任務(wù),trigger
          4 trigger 的停止,運行
          5 任務(wù)的任意添加
          6 比corntrigger 更詳盡的任務(wù)安排
          7 線程的內(nèi)部數(shù)據(jù)交換  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-09-11 15:14 gLaiVe
          @kevin.guo

          你說的trigger的停止,能不能動態(tài)設(shè)置的呢,比如我想在任何時刻停止當前正在跑得task,我現(xiàn)在的quartz是配置在spring里面了,怎么用spring來控制  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 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]  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-09-18 22:39 xiaqingfeng
          請高手指點一下啊,為什么會報異常?難道一定要建表嗎?可我不想建表  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2006-11-03 18:25 kevin.guo
          你說的trigger的停止,能不能動態(tài)設(shè)置的呢,

          我沒用spring集成,做法是job里調(diào)用trigger的停方法,這樣trigger驅(qū)動的job都不會被執(zhí)行了  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2007-03-21 16:29 helloint
          cronExpression配置說明

          這個說的太簡單拉..  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 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)  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2007-11-06 18:14 fenix
          好  回復(fù)  更多評論
            

          # re:sss 2007-11-26 10:13 dfsf
          Good java spring   回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2007-11-28 14:32 meizi
          真棒?。?nbsp; 回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-01-02 23:01 linuxboy
          VERY good!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-01-09 15:54 quarts
          多謝樓主,不錯的文章。我通過你的文章配置了Quarts的應(yīng)用,但在tomcat上使用時,出現(xiàn)過只能采集一次,之后那線程一直阻塞狀態(tài)沒法定時下去,該怎么辦?  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) [未登錄] 2008-01-21 12:28 leo
          up 。。。。。。。。。  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-01-28 10:07 df
          good  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) [未登錄] 2008-03-30 09:29 aa
          good
            回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 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執(zhí)行了一次,我修改為“<value>0 27 13 ? * *</value>”就不再自動執(zhí)行,再改為其他時間,也不再自動執(zhí)行。我重啟服務(wù)也沒用。誰遇見到這個問題?。?nbsp; 回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-05-19 09:30 asd
          不錯,學習了。  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-07-18 16:37 zlj
          學習中。。。  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-07-30 18:03 飛越海面
          我想請問一下對于現(xiàn)在的spring中集成的還是這樣的配置方式嗎?  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-07-31 15:26 black_boy
          太好了,解了我燃眉之急  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) [未登錄] 2008-08-24 13:51 wz
          正要用這個,好啊,謝謝!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2008-10-27 16:00 9527
          為什么我跟你寫的一樣 但是我的TOMCAT啟動之后 沒有反應(yīng)呢?請高手指點 多謝啦  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2009-01-13 22:14 odin
          真慶幸可以看到你的文章,對我太有幫助了!  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2009-10-19 14:34 coding
          不錯 很有幫助 學習了  回復(fù)  更多評論
            

          # re: Spring--簡單使用quartz實現(xiàn)定時作業(yè) 2009-12-04 19:35 baizhiguo
          我的配置文件和你的一樣但是就是報錯:錯誤內(nèi)容是找不到:org.springframework.scheduling.quartz.CronTriggerBean,那個包也有了  回復(fù)  更多評論
            

          主站蜘蛛池模板: 龙山县| 凉城县| 南和县| 江阴市| 洞口县| 吉木萨尔县| 昌邑市| 申扎县| 隆安县| 喜德县| 达孜县| 扶沟县| 福建省| 饶阳县| 栾城县| 永顺县| 兰溪市| 双柏县| 乐山市| 九台市| 郴州市| 凤山市| 桐庐县| 师宗县| 曲沃县| 钟山县| 镇坪县| 突泉县| 进贤县| 广东省| 衡南县| 仪陇县| 宜宾县| 都江堰市| 古浪县| 罗江县| 湖南省| 娄底市| 湖口县| 娱乐| 临桂县|