zoukankan      html  css  js  c++  java
  • spring配置quartz 多任务

          <!-- 要调用的工作类 -->
             <bean id="quartzJob" class="com.yjdgis.quartz.QuartzJob"></bean>
             <bean id="quartzJob2" class="com.yjdgis.quartz.QuartzExceptionJob"></bean>
             <!-- 定义调用对象和调用对象的方法 -->
             <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                 <!-- 调用的类 -->
                 <property name="targetObject" ref="quartzJob"/>
                 <!-- 调用类中的方法 -->
                 <property name="targetMethod" value="work" />
             </bean>
             <bean id="jobtask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                 <property name="targetObject" ref="quartzJob2"/>
                 <property name="targetMethod" value="run" />
             </bean>
             <!-- 定义触发时间 -->
             <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
                 <property name="jobDetail" ref="jobtask" />
                 <!-- cron表达式 , 一个cronExpression表达式有至少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) -->
                 <property name="cronExpression" value="5/5 * * * * ?" />
             </bean>
             <bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
                 <property name="" ref="jobtask2"/>
                 <property name="cronExpression" value="3/3 * * * * ?" />
             </bean>
             <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
             <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                 <property name="triggers">
                     <list>
                         <ref bean="doTime"/>
                         <ref bean="doTime2"/>
                     </list>
                 </property>
             </bean>

    应用环境:spring 3.2     quarz 2.2    版本一样,引入的bean有点区别

    cronExpression说明:

    spring定时任务时间格式cronExpression设置 org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpression属性。 一个cronExpression表达式有至少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) 0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 0 0 8-5 ? * MON-FRI 每个工作日的工作时间

    各个时间可用值如下: 秒 0-59 , – * / 分 0-59 , – * / 小时 0-23 , – * / 日 1-31 , – * ? / L W C 月 1-12 or JAN-DEC , – * / 周几 1-7 or SUN-SAT , – * ? / L C # 年 (可选字段) empty, 1970-2099 , – * /

    可用值详细分析如下:

    “*”——字符可以用于所有字段,在“分”字段中设为”*”表示”每一分钟”的含义。

    “?”——字符可以用在“日”和“周几”字段. 它用来指定 ‘不明确的值’. 这在你需要指定这两个字段中的某一个值而不是另外一个的时候会被用到。在后面的例子中可以看到其含义。

    “-”——字符被用来指定一个值的范围,比如在“小时”字段中设为”10-12″表示”10点到12点”。

    “,”——字符指定数个值。比如在“周几”字段中设为”MON,WED,FRI”表示”the days Monday, Wednesday, and Friday”。

    “/”——字符用来指定一个值的的增加幅度. 比如在“秒”字段中设置为”0/15″表示”第0, 15, 30, 和 45秒”。而 “5/15″则表示”第5, 20, 35, 和 50″. 在’/'前加”*”字符相当于指定从0秒开始. 每个字段都有一系列可以开始或结束的数值。对于“秒”和“分”字段来说,其数值范围为0到59,对于“小时”字段来说其为0到23, 对于“日”字段来说为0到31, 而对于“月”字段来说为1到12。”/”字段仅仅只是帮助你在允许的数值范围内从开始”第n”的值。

    “L”——字符可用在“日”和“周几”这两个字段。它是”last”的缩写, 但是在这两个字段中有不同的含义。例如,“日”字段中的”L”表示”一个月中的最后一天” —— 对于一月就是31号对于二月来说就是28号(非闰年)。而在“周几”字段中, 它简单的表示”7″ or “SAT”,但是如果在“周几”字段中使用时跟在某个数字之后, 它表示”该月最后一个星期×” —— 比如”6L”表示”该月最后一个周五”。当使用’L'选项时,指定确定的列表或者范围非常重要,否则你会被结果搞糊涂的。

    “W”——可用于“日”字段。用来指定历给定日期最近的工作日(周一到周五) 。比如你将“日”字段设为”15W”,意为: “离该月15号最近的工作日”。因此如果15号为周六,触发器会在14号即周五调用。如果15号为周日, 触发器会在16号也就是周一触发。如果15号为周二,那么当天就会触发。然而如果你将“日”字段设为”1W”, 而一号又是周六, 触发器会于下周一也就是当月的3号触发,因为它不会越过当月的值的范围边界。’W'字符只能用于“日”字段的值为单独的一天而不是一系列值的时候。

    “L”和“W”可以组合用于“日”字段表示为’LW’,意为”该月最后一个工作日”。

    “#”—— 字符可用于“周几”字段。该字符表示“该月第几个周×”,比如”6#3″表示该月第三个周五( 6表示周五而”#3″该月第三个)。再比如: “2#1″ = 表示该月第一个周一而 “4#5″ = 该月第五个周三。注意如果你指定”#5″该月没有第五个“周×”,该月是不会触发的。

    “C”—— 字符可用于“日”和“周几”字段,它是”calendar”的缩写。 它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日历, 那它等同于包含全部日历。“日”字段值为”5C”表示”日历中的第一天或者5号以后”,“周几”字段值为”1C”则表示”日历中的第一天或者周日以后”。

    对于“月份”字段和“周几”字段来说合法的字符都不是大小写敏感的。

    一些例子: “0 0 12 * * ?” 每天中午十二点触发 “0 15 10 ? * *” 每天早上10:15触发 “0 15 10 * * ?” 每天早上10:15触发 “0 15 10 * * ? *” 每天早上10:15触发 “0 15 10 * * ? 2005″ 2005年的每天早上10:15触发 “0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发 “0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发 “0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 “0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发 “0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发 “0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发 “0 15 10 15 * ?” 每月15号的10:15触发 “0 15 10 L * ?” 每月的最后一天的10:15触发 “0 15 10 ? * 6L” 每月最后一个周五的10:15

    转自:

    下面有一些关于时间配置的说明:

    字段顺序

    允许值

    允许的特殊字符

    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.

    "/"字符用来指定渐增的值。例如0/15出现在秒字段的时候意味着“在第0,15,30和45秒”(的时候被触发)。而5/15出现在秒字段的时候意味着“在第5,20,35和第50秒”(的时候被触发)。"*/"和"0/"的指定是等价的。 注:当分子+分母的值大于该字段的最大值,如在秒钟字段出现45/20的时候,表示在第45秒以后的每20会触发一次,但是20秒以后又算做另外的时间段以内了,所以该字段的/20即失效,如改为45/10 则只有第45 和55秒会执行一次。

    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.

    L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of-month域中表示一个月的最后一天, 如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示一个月的最后几天,例如‘6L’就表示一个月的最后一个 星期五,

    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". 关于cronExpression的介绍:   字段允许值允许的特殊字符 秒0-59 , - * / 分0-59 , - * / 小时0-23 , - * / 日期1-31 , - * ? / L W C 月份1-12 或者JAN-DEC , - * / 星期1-7 或者SUN-SAT , - * ? / L C # 年(可选)留空, 1970-2099 , - * /

  • 相关阅读:
    订餐系统
    throw和throws
    CF999E Solution
    CF1142B Solution
    CF965C Solution
    CF963B Solution
    CF999F Solution
    CF975D Solution
    CF997B Solution
    hdu 2553 N皇后
  • 原文地址:https://www.cnblogs.com/hwj2wj/p/3229258.html
Copyright © 2011-2022 走看看