定时任务

阅读 / 问答 / 标签

mysql如何定时任务

1,需求:每天晚上10点到早上5点,每10分钟定时执行存储过程。2,实现方式:实现方式有两种: 一种是比较常规的,用mysql的event定时任务,先介绍一下mysql中event定时任务的相关命令,查看event是否启用:SELECT @@event_scheduler;SHOW VARIABLES LIKE "event%";开启定时任务:set GLOBAL event_scheduler = 1;SET GLOBAL event_scheduler = ON;建立定时任务:DROP EVENT IF EXISTS JOB_ALARM;CREATE EVENT JOB_ALARM ON SCHEDULE EVERY 10 MINUTEDO BEGIN if(date_format(current_time(),"%H")>22 || date_format(current_time(),"%H")<5) THEN CALL PRO_ALARM(); END IF;END建立存储过程:DROP PROCEDURE IF EXISTS PRO_ALARM;CREATE PROCEDURE PRO_ALARM() BEGIN DECLARE userId VARCHAR(32); #这个用于处理游标到达最后一行的情况 DECLARE s INT DEFAULT 0; #声明游标cursor_name(cursor_name是个多行结果集) DECLARE cursor_data CURSOR FOR SELECT tmp.USER_ID FROM ( SELECT e.USER_ID, MAX(e.TIME_GMT) TIME_GMT FROM EVENTS e GROUP BY e.USER_ID HAVING MAX(e.TIME_GMT) < UNIX_TIMESTAMP() - 60 * 30 AND MAX(e.TIME_GMT) > UNIX_TIMESTAMP() - 60 * 60 * 24) tmp INNER JOIN EVENTS t ON tmp.USER_ID = t.USER_ID AND tmp.TIME_GMT = t.TIME_GMT WHERE TYPE_ID != "34001"; #设置一个终止标记 DECLARE CONTINUE HANDLER FOR SQLSTATE "02000" SET s = 1; OPEN cursor_data; #获取游标当前指针的记录,读取一行数据并传给变量a,b FETCH cursor_data INTO userId; #开始循环,判断是否游标已经到达了最后作为循环条件 WHILE s <> 1 DO INSERT INTO EVENTS_NOTIFICATION VALUES (NULL, SYSDATE(), UNIX_TIMESTAMP(SYSDATE()), UNIX_TIMESTAMP(SYSDATE()), "00000", userId, "1", "0"); #读取下一行的数据 FETCH cursor_data INTO userId; END WHILE; #关闭游标 CLOSE cursor_data; END;在配置定时器时有一些局限性,并且由于博主使用的mysql启用了skip-grant-tables,在设置event开启时,总是报错,所以想到了第二种实现方式,具体报错信息如下:[HY000][1290] The MySQL server is running with the --event-scheduler=DISABLED or --skip-grant-tables option so it cannot execute this statement 第二种实现方式是利用linux的定时任务,linux定时任务基本命令:查看定时任务:crontab -l编辑定时任务:crontab -e */10 22-23,0-5 * * * mysql -u用户名 -p密码 -e "use db_name;CALL PRO_ALARM();" 或者把use db_name;CALL PRO_ALARM();存到sql脚本中,编辑定时任务如下: */10 22-23,0-5 * * * mysql -u用户名 -p密码 < /application/Job_mysql.sql更多相关教程请访问 MySQL视频教程

如何在spring中配置定时任务

一、在web.xml文件中进行如下配置: <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml </param-value> </context-param>那么需要在工程下创建一个以applicationContext- 为开头的xml文件eg:applicationContext-jobconfig.xmlxml的头和结尾部分跟其他spring配置文件相似,就不赘述,正文如下: <bean id="youJobName(类别名)" class="com.******.YourJobClassLocation(类的定位)" /> <bean id="doYourJob(别名)" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="youJobName(类别名)""/> </property> <property name="targetMethod"> <value>runMethodName(定时执行的方法名)</value> </property> </bean> <bean id="youJobNameTrigger(触发器别名)" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="doYourJob(别名)""/> </property> <property name="cronExpression"> <value>0 0/20 * * * ?(定时的时间配置)</value> </property> </bean> <bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="youJobNameTrigger(触发器别名)"/> </list> </property> </bean>这样的配置几本就可以运转了,但是有一个地方可能是你需要根据你的需求来确定的,那就是触发时间。下面有一些关于时间配置的说明: <value>0 0/20 * * * ?</value>按顺序 <value> 秒 分 小时 日期 月份 星期 年 </value>字段顺序允许值允许的特殊字符秒 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 , - * / 表达式意义 "0 0 12 * * ?" 每天中午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期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2: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 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 每天早上6点 0 6 * * * 每两个小时 0 */2 * * * 晚上11点到早上7点之间每两个小时,早上八点 0 23-7/2,8 * * * 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 0 11 4 * 1-3 1月1日早上4点 0 4 1 1 *quartz的高级特性不仅如此 1 数据库存储 2 集群支持 3 数据库持久化任务,trigger 4 trigger 的停止,运行 5 任务的任意添加 6 比corntrigger 更详尽的任务安排 7 线程的内部数据交换

Spring+quartz 做定时任务时,每5分种执行一次,怎么写法

可以使用cron表达式写,* 5 * * * * *。以下为cron表达式详Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minutes Hours DayofMonth Month DayofWeek每一个域可出现的字符如下: Seconds:可出现", - * /"四个字符,有效范围为0-59的整数 Minutes:可出现", - * /"四个字符,有效范围为0-59的整数 Hours:可出现", - * /"四个字符,有效范围为0-23的整数 DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 Year:可出现", - * /"四个字符,有效范围为1970-2099年每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是: (1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。 (3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次 (4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次. (5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。 (6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。 (7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 (8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。 (9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。举几个例子: 0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业 0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT) 年份(1970-2099)其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 12 ? * WED 表示每个星期三中午12点 "0 0 12 * * ?" 每天中午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期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2: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 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发有些子表达式能包含一些范围或列表例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”“*”字符代表所有可能的值因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天“/”字符用来指定数值的增量 例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟 在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值 当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写 但是它在两个子表达式里的含义是不同的。 在天(月)子表达式中,“L”表示一个月的最后一天 在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT如果在“L”前有具体的内容,它就具有其他的含义了例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五 注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * / 星期 1-7 或者 SUN-SAT , - * ? / L C # 年(可选) 留空, 1970-2099 , - * /

Spring+quartz 做定时任务时,每5分种执行一次,怎么写法

可以使用cron表达式写,* 5 * * * * *。以下为cron表达式详Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minutes Hours DayofMonth Month DayofWeek每一个域可出现的字符如下: Seconds:可出现", - * /"四个字符,有效范围为0-59的整数 Minutes:可出现", - * /"四个字符,有效范围为0-59的整数 Hours:可出现", - * /"四个字符,有效范围为0-23的整数 DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 Year:可出现", - * /"四个字符,有效范围为1970-2099年每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是: (1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。 (3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次 (4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次. (5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。 (6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。 (7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 (8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。 (9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。举几个例子: 0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业 0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT) 年份(1970-2099)其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 12 ? * WED 表示每个星期三中午12点 "0 0 12 * * ?" 每天中午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期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2: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 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发有些子表达式能包含一些范围或列表例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”“*”字符代表所有可能的值因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天“/”字符用来指定数值的增量 例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟 在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值 当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写 但是它在两个子表达式里的含义是不同的。 在天(月)子表达式中,“L”表示一个月的最后一天 在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT如果在“L”前有具体的内容,它就具有其他的含义了例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五 注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * / 星期 1-7 或者 SUN-SAT , - * ? / L C # 年(可选) 留空, 1970-2099 , - * /

php如何实现定时任务,php定时任务方法,最佳

使用Linux自带的crontab

如何用Spring实现集群环境下的定时任务

定时任务的实现方式有多种,例如JDK自带的Timer+TimerTask方式,Spring 3.0以后的调度任务(Scheduled Task),Quartz等。Timer+TimerTask是最基本的解决方案,但是比较远古了,这里不再讨论。Spring自带的Scheduled Task是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。Quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。Quartz框架需要10多张表协同,配置繁多,令人望而却步...经过折中考虑,还是选择了Spring的Scheduled Task来实现定时任务。如下:1. Spring配置文件application-context.xml中添加task命名空间和描述。[html] view plain copy<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 2. 添加调度器和线程池声明。[html] view plain copy<task:executor id="taskExecutor" pool-size="10" /> <task:annotation-driven executor="taskExecutor" /> 3. 实现调度方法。基本结构如下:[html] view plain copypackage com.netease.yx.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { @Scheduled(cron = "0 0 5 * * *") public void build() { System.out.println("Scheduled Task"); } } @Scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,Scheduled Task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。如何控制?1. 无非是一个任务互斥访问的问题,声明一把全局的“锁”作为互斥量,哪个应用服务器拿到这把“锁”,就有执行任务的权利,未拿到“锁”的应用服务器不进行任何任务相关的操作。2.这把“锁”最好还能在下次任务执行时间点前失效。在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。完整定时任务类如下:[html] view plain copypackage com.netease.yx.service; import javax.annotation.Resource; import org.apache.commons.lang3.time.DateUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.netease.yx.service.ICacheService; @Service public class ScheduledService { @Resource private ICacheService cache = null; private static String CACHE_LOCK = "cache_lock"; private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000; @Scheduled(cron = "0 0 5 * * *") public void build() { if (cache.get(CACHE_LOCK) == null) { cache.set(CACHE_LOCK, true, EXPIRE_PERIOD); doJob(); } } }

scheduledexecutorservice 定时任务 只执行一次怎么回事

下面是该接口的原型定义java.util.concurrent.ScheduleExecutorService extends ExecutorService extends Executor接口scheduleAtFixedRate原型定义及参数说明[java] view plain copypublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); command:执行线程initialDelay:初始化延时period:两次开始执行最小间隔时间unit:计时单位接口scheduleWithFixedDelay原型定义及参数说明[java] view plain copy

java scheduled 多个定时任务会冲突吗

定时任务本身不会有冲突,有冲突的是任务中操作处理的资源或数据,需要对有写入的文件或数据进行排它锁,保障线程处理的安全性。

怎么停止scheduledexecutorservice 定时任务

你好,用 ScheduledExecutorService.shutdownNow() shutdown() 方法 还会把后续任务都执行完毕才关闭。

分布式定时任务调度框架实践

分布式任务调度框架几乎是每个大型应用必备的工具,本文介绍了任务调度框架使用的需求背景和痛点,对业界普遍使用的开源分布式任务调度框架的使用进行了探究实践,并分析了这几种框架的优劣势和对自身业务的思考。 一、业务背景 1.1 为什么需要使用定时任务调度 (1)时间驱动处理场景: 整点发送优惠券,每天更新收益,每天刷新标签数据和人群数据。 (2)批量处理数据: 按月批量统计报表数据,批量更新短信状态,实时性要求不高。 (3)异步执行解耦: 活动状态刷新,异步执行离线查询,与内部逻辑解耦。 1.2 使用需求和痛点 (1)任务执行监控告警能力。 (2)任务可灵活动态配置,无需重启。 (3)业务透明,低耦合,配置精简,开发方便。 (4)易测试。 (5)高可用,无单点故障。 (6)任务不可重复执行,防止逻辑异常。 (7)大任务的分发并行处理能力。 二、开源框架实践与 探索 2.1 Java 原生 Timer 和 ScheduledExecutorService 2.1.1 Timer使用 Timer缺陷: 由于上述缺陷,尽量不要使用Timer, idea中也会明确提示,使用ScheduledThreadPoolExecutor替代Timer 。 2.1.2 ScheduledExecutorService使用 ScheduledExecutorService对于Timer的缺陷进行了修补,首先ScheduledExecutorService内部实现是ScheduledThreadPool线程池,可以支持多个任务并发执行。 对于某一个线程执行的任务出现异常,也会处理,不会影响其他线程任务的执行,另外ScheduledExecutorService是基于时间间隔的延迟,执行不会由于系统时间的改变发生变化。 当然,ScheduledExecutorService也有自己的局限性:只能根据任务的延迟来进行调度,无法满足基于绝对时间和日历调度的需求。 2.2 Spring Task 2.2.1 Spring Task 使用 spring task 是spring自主开发的轻量级定时任务框架,不需要依赖其他额外的包,配置较为简单。 此处使用注解配置 2.2.2 Spring Task缺陷 Spring Task 本身不支持持久化,也没有推出官方的分布式集群模式,只能靠开发者在业务应用中自己手动扩展实现,无法满足可视化,易配置的需求。 2.3 永远经典的 Quartz 2.3.1 基本介绍 Quartz框架是Java领域最著名的开源任务调度工具,也是目前事实上的定时任务标准,几乎全部的开源定时任务框架都是基于Quartz核心调度构建而成。 2.3.2 原理解析 核心组件和架构 关键概念 (1) Scheduler :任务调度器,是执行任务调度的控制器。本质上是一个计划调度容器,注册了全部Trigger和对应的JobDetail, 使用线程池作为任务运行的基础组件,提高任务执行效率。 (2) Trigger :触发器,用于定义任务调度的时间规则,告诉任务调度器什么时候触发任务,其中CronTrigger是基于cron表达式构建的功能强大的触发器。 (3) Calendar :日历特定时间点的集合。一个trigger可以包含多个Calendar,可用于排除或包含某些时间点。 (4) JobDetail :是一个可执行的工作,用来描述Job实现类及其它相关的静态信息,如Job的名称、监听器等相关信息。 (5) Job :任务执行接口,只有一个execute方法,用于执行真正的业务逻辑。 (6) JobStore :任务存储方式,主要有RAMJobStore和JDBCJobStore,RAMJobStore是存储在JVM的内存中,有丢失和数量受限的风险,JDBCJobStore是将任务信息持久化到数据库中,支持集群。 2.3.3 实践说明 (1)关于Quartz的基本使用 (2)业务使用要满足动态修改和重启不丢失, 一般需要使用数据库进行保存。 (3)组件化 (4)扩展 2.3.4 缺陷和不足 (1)需要把任务信息持久化到业务数据表,和业务有耦合。 (2)调度逻辑和执行逻辑并存于同一个项目中,在机器性能固定的情况下,业务和调度之间不可避免地会相互影响。 (3)quartz集群模式下,是通过数据库独占锁来唯一获取任务,任务执行并没有实现完善的负载均衡机制。 2.4 轻量级神器 XXL-JOB 2.4.1 基本介绍 XXL-JOB是一个轻量级分布式任务调度平台,主打特点是平台化,易部署,开发迅速、学习简单、轻量级、易扩展,代码仍在持续更新中。 主要提供了任务的动态配置管理、任务监控和统计报表以及调度日志几大功能模块,支持多种运行模式和路由策略,可基于对应执行器机器集群数量进行简单分片数据处理。 2.4.2 原理解析 2.1.0版本前核心调度模块都是基于quartz框架,2.1.0版本开始自研调度组件,移除quartz依赖 ,使用时间轮调度。 2.4.3 实践说明 详细配置和介绍参考官方文档。 2.4.3.1 demo使用: @JobHandler(value="offlineTaskJobHandler") ,实现业务逻辑即可。(注:此次引入了dubbo,后文介绍)。 (滑动可查看) 示例2:分片广播任务。 (滑动可查看) 2.4.3.2 整合dubbo (1)引入dubbo-spring-boot-starter和业务facade jar包依赖。 (滑动可查看) (2)配置文件加入dubbo消费端配置(可根据环境定义多个配置文件,通过profile切换)。 (滑动可查看) (3)代码中通过@Reference注入facade接口即可。 (滑动可查看) (4)启动程序加入@EnableDubboConfiguration注解。 (滑动可查看) 2.4.4 任务可视化配置 内置了平台项目,方便了开发者对任务的管理和执行日志的监控,并提供了一些便于测试的功能。 2.4.5 扩展 (1)任务监控和报表的优化。 (2)任务报警方式的扩展,比如加入告警中心,提供内部消息,短信告警。 (3)对实际业务内部执行出现异常情况下的不同监控告警和重试策略。 2.5 高可用 Elastic-Job 2.5.1 基本介绍 Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成。 Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供分布式任务的协调服务。 Elastic-Job-Cloud使用Mesos + Docker的解决方案,额外提供资源治理、应用分发以及进程隔离等服务。 可惜的是已经两年没有迭代更新记录。 2.5.2 原理解析 2.5.3 实践说明 2.5.3.1 demo使用 (1)安装zookeeper,配置注册中心config,配置文件加入注册中心zk的配置。 (滑动可查看) (滑动可查看) (2)配置数据源config,并配置文件中加入数据源配置。 (滑动可查看) (滑动可查看) (3)配置事件config。 (滑动可查看) (4)为了便于灵活配置不同的任务触发事件,加入ElasticSimpleJob注解。 (滑动可查看) (5)对配置进行初始化。 (滑动可查看) (6)实现 SimpleJob接口,按上文中方法整合dubbo, 完成业务逻辑。 (滑动可查看) 2.6 其余开源框架 (1) Saturn :Saturn是唯品会开源的一个分布式任务调度平台,在Elastic Job的基础上进行了改造。 (2) SIA-TASK :是宜信开源的分布式任务调度平台。 三、优劣势对比和业务场景适配思考 业务思考: 四、结语 对于并发场景不是特别高的系统来说,xxl-job配置部署简单易用,不需要引入多余的组件,同时提供了可视化的控制台,使用起来非常友好,是一个比较好的选择。希望直接利用开源分布式框架能力的系统,建议根据自身的情况来进行合适的选型。 附:参考文献 高可用架构 改变互联网的构建方式

怎么停止scheduledexecutorservice 定时任务

用 ScheduledExecutorService.shutdownNow() shutdown() 方法 还会把后续任务都执行完毕才关闭。

Spring使用@Scheduled注解配置定时任务

项目中经常会用到定时任务。所以在这里总结一下在SSM框架中如何配置定时任务。 1、在spring的配置文件spring.xml(文件名可以任意)中增加如下配置 1):spring配置文件加入头部加入 2):spring配置文件加入定时任务注解配置 3):spring配置文件加入定时任务扫描包 4):spring配置文件加入配置定时任务的线程池。因为spring的定时任务默认是单线程,多个任务执行起来时间会有问题。 2、在package com.sc.api下新增定时任务相关类ScheduledApiTest 调用的两种方式: @Scheduled注解为定时任务,@Component 把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> 1):如果需要以固定速率执行,只要将注解中指定的属性名称改成fixedRate即可,以下方法将以一个固定速率1分钟来调用一次执行,这个周期是以上一个任务开始时间为基准,从上一任务开始执行后1分钟再次调用。 @Scheduled(fixedRate = 1000 60 30) //心跳更新。启动时执行一次,之后每隔1分钟执行一次 2):如果你需要在特定的时间执行,就需要用到cron,cron表达式里为执行的时机 @Scheduled(cron = "0 34 13 * * ?") //每天的13点30分执行一次。 3、启动tomcat服务,定时任务就会按时执行。 关于CRON表达式 含义

Spring使用@Scheduled进行定时任务,定的时间可否变

定时任务的实现方式有多种,例如JDK自带的Timer+TimerTask方式,Spring 3.0以后的调度任务(Scheduled Task),Quartz等。Timer+TimerTask是最基本的解决方案,但是比较远古了,这里不再讨论。Spring自带的ScheduledTask是一个轻量级的定时任务调度器,支持固定时间(支持cron表达式)和固定时间间隔调度任务,支持线程池管理。以上两种方式有一个共同的缺点,那就是应用服务器集群下会出现任务多次被调度执行的情况,因为集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行。Quartz是一个功能完善的任务调度框架,特别牛叉的是它支持集群环境下的任务调度,当然代价也很大,需要将任务调度状态序列化到数据库。Quartz框架需要10多张表协同,配置繁多,令人望而却步...经过折中考虑,还是选择了Spring的Scheduled Task来实现定时任务。如下:1. Spring配置文件application-context.xml中添加task命名空间和描述。[html] view plain copy<beans xmlns="" xmlns:task="" xsi:schemaLocation=" /spring-beans.xsd /spring-task.xsd"> 2. 添加调度器和线程池声明。[html] view plain copy<task:executor id="taskExecutor" pool-size="10" /> <task:annotation-driven executor="taskExecutor" /> 3. 实现调度方法。基本结构如下:[html] view plain copypackage com.netease.yx.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { @Scheduled(cron = "0 0 5 * * *") public void build() { System.out.println("Scheduled Task"); } } @Scheduled注解支持秒级的cron表达式,上述声明表示每天5点执行build任务。前文已经提过,这种方式在单台应用服务器上运行没有问题,但是在集群环境下,会造成build任务在5点的时候运行多次,遗憾的是,Scheduled Task在框架层面没有相应的解决方案,只能靠程序员在应用级别进行控制。如何控制看1. 无非是一个任务互斥访问的问题,声明一把全局的逗锁地作为互斥量,哪个应用服务器拿到这把逗锁地,就有执行任务的权利,未拿到逗锁地的应用服务器不进行任何任务相关的操作。2.这把逗锁地最好还能在下次任务执行时间点前失效。在项目中我将这个互斥量放在了redis缓存里,1小时过期,这个过期时间是由任务调度的间隔时间决定的,只要小于两次任务执行时间差,大于集群间应用服务器的时间差即可。完整定时任务类如下:[html] view plain copypackage com.netease.yx.service; import javax.annotation.Resource; import org.apache.commons.lang3.time.DateUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.netease.yx.service.ICacheService; @Service public class ScheduledService { @Resource private ICacheService cache = null; private static String CACHE_LOCK = "cache_lock"; private static int EXPIRE_PERIOD = (int)DateUtils.MILLIS_PER_HOUR / 1000; @Scheduled(cron = "0 0 5 * * *") public void build() { if (cache.get(CACHE_LOCK) == null) { cache.set(CACHE_LOCK, true, EXPIRE_PERIOD); doJob(); } } }

fedora8中添加cron定时任务

您好,10 5 * * * root run-parts /etc/cron.daily </cdoe> 然后我们到/etc/crond.daily目录中创建两个文件,一个是用来重启httpd服务器的,如下;<code> [root@localhost cron.daily]# touch httpd.sh [root@localhost cron.daily]# chmod 755 httpd.sh [root@localhost cron.daily]# echo "/etc/init.d/httpd restart" > httpd.sh [root@localhost cron.daily]# more httpd.sh /etc/init.d/httpd restart 如果我想在每天5点20分下载FC5的镜像,可以再创建一个文件fc5down.sh [root@localhost cron.daily]# touch fc5down.sh [root@localhost cron.daily]# chmod 755 fc5down.sh [root@localhost cron.daily]# echo "/usr/bin/wget http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso" > fc5down.sh [root@localhost cron.daily]# more fc5down.sh /usr/bin/wget http://mirrors.kernel.org/fedora/core/5/i386/iso/FC-5-i386-DVD.iso 然后我们重新启动一下crond就行了;[root@localhost cron.daily]# pkill crond [root@localhost cron.daily]# prep crond [root@localhost cron.daily]# crond&

linux下定时任务--记一次禅道备份数据到远程服务器

最近出了件大事,一大早被领导三番催促,原来是部署在我们机房机器上的禅道莫名挂了,所有用户均无法正常登陆,怀疑是机房机器变动导致部分数据丢失,好在禅道每天有备份,尝试使用备份数据重新覆盖安装,也是失败,最后检查是服务器磁盘满了。。。但也提醒了我这些数据的重要性(数据包已经有6G了,所有开发任务和缺陷、用例全在上面),于是着手把数据远程备份一次。 我使用的机器是Centos7,不同机器命令不太一样,如果使用的是Centos6及以下版本,sytemctl需要换成service 上方是给出的注释,从左往右依次是分、时、日、月、周,我设置的是每天凌晨2点半执行打包脚本,3点半把压缩包传至另一个服务器,4点则删除该压缩包,脚本比较简单 使用RSA非对称加密算法,将禅道服务器的公钥放到备份服务器的authorized_keys中 公钥/root/.ssh/id_rsa.pub 秘钥/root/.ssh/id_rsa 可以通过lszrz工具将公钥上传至B服务器中。 尝试一下: scp test.txt root@47.94.36.78:/root/ 大功告成

Spring调度定时任务的方式

spring 定时器任务scheduled-tasks默认配置是单线程串行执行的,多个任务相当于串行。每个job都是等待上个执行完了才执行下一个job。这就造成了若某个任务执行时间过长,其他任务一直在排队,业务逻辑没有及时处理的问题。 spring调度定时任务的方式就会导致:bTask会因为aTask的超时执行而延迟执行。 如下是scheduled定义了3个任务。 查看该任务17点的执行日志(task名字已修改) MyTask2每10秒钟执行一次,但是在17:15:05 到17:20:35之间,5分钟内定时任务没有执行。 执行命令 zgrep -e "2016-10-28 17:1" task.log.2016-10-28.log.gz (当天17点10几分发生的日志)。然后在查询日志发现,这5分钟之内有大量的日志在执行。 通过过以上日志可以看出,该线程 [pool-8-thread-1 - ] 一直在处理MyTask3任务,此时断定 task:scheduled 配置默认是单线程串行的。网上查找资料发现如下配置可以解决问题: Spring中可以通过配置方便的实现周期性定时任务管理,这需要用到以下几个类: concurrent:对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。 MethodInvokingJobDetailFactoryBean类默认是并发执行的,这时候如果不设置“concurrent”为false,很可能带来并发或者死锁的问题,而且几率较小,不容易复现,请大家使用的时候注意设置“concurrent”。

java中quartz定时任务的执行如何避免并发

只要在detail的参数里加上一句话就行了,把这个job设置成有状态的job<property name="concurrent" value="false" /> 扩展:通过concurrent属性指定任务的类型,默认情况下封装为无状态的任务,如果希望目标封装为有状态的任务,仅需要将concurrent设置为false就可以了。Spring通过名为concurrent的属性指定任务的类型,能够更直接地描述到任务执行的方式(有状态的任务不能并发执行,无状态的任务可并发执行)

java定时任务怎么实现

/*** 普通thread* 这是最常见的,创建一个thread,然后让它在while循环里一直运行着,* 通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下:* @author GT**/public class Task1 {public static void main(String[] args) {// run in a secondfinal long timeInterval = 1000;Runnable runnable = new Runnable() {public void run() {while (true) {// ------- code for task to runSystem.out.println("Hello !!");// ------- ends heretry {Thread.sleep(timeInterval);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread thread = new Thread(runnable);thread.start();}} [java] view plain copyimport java.util.Timer;import java.util.TimerTask;/**** 于第一种方式相比,优势 1>当启动和去取消任务时可以控制 2>第一次执行任务时可以指定你想要的delay时间** 在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。 Timer实例可以调度多任务,它是线程安全的。* 当Timer的构造器被调用时,它创建了一个线程,这个线程可以用来调度任务。 下面是代码:** @author GT**/public class Task2 {public static void main(String[] args) {TimerTask task = new TimerTask() {@Overridepublic void run() {// task to run goes hereSystem.out.println("Hello !!!");}};Timer timer = new Timer();long delay = 0;long intevalPeriod = 1 * 1000;// schedules the task to be run in an intervaltimer.scheduleAtFixedRate(task, delay, intevalPeriod);} // end of main} [java] view plain copyimport java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;/***** ScheduledExecutorService是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。* 相比于上两个方法,它有以下好处:* 1>相比于Timer的单线程,它是通过线程池的方式来执行任务的* 2>可以很灵活的去设定第一次执行任务delay时间* 3>提供了良好的约定,以便设定执行的时间间隔** 下面是实现代码,我们通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,通过代码里参数的控制,首次执行加了delay时间。*** @author GT**/public class Task3 {public static void main(String[] args) {Runnable runnable = new Runnable() {public void run() {// task to run goes hereSystem.out.println("Hello !!");}};ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);}}

如何在spring中配置定时任务

一、在web.xml文件中进行如下配置: <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml </param-value> </context-param>那么需要在工程下创建一个以applicationContext- 为开头的xml文件eg:applicationContext-jobconfig.xmlxml的头和结尾部分跟其他spring配置文件相似,就不赘述,正文如下: <bean id="youJobName(类别名)" class="com.******.YourJobClassLocation(类的定位)" /> <bean id="doYourJob(别名)" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="youJobName(类别名)""/> </property> <property name="targetMethod"> <value>runMethodName(定时执行的方法名)</value> </property> </bean> <bean id="youJobNameTrigger(触发器别名)" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="doYourJob(别名)""/> </property> <property name="cronExpression"> <value>0 0/20 * * * ?(定时的时间配置)</value> </property> </bean> <bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="youJobNameTrigger(触发器别名)"/> </list> </property> </bean>这样的配置几本就可以运转了,但是有一个地方可能是你需要根据你的需求来确定的,那就是触发时间。下面有一些关于时间配置的说明: <value>0 0/20 * * * ?</value>按顺序 <value> 秒 分 小时 日期 月份 星期 年 </value>字段顺序允许值允许的特殊字符秒 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 , - * / 表达式意义 "0 0 12 * * ?" 每天中午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期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2: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 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 每天早上6点 0 6 * * * 每两个小时 0 */2 * * * 晚上11点到早上7点之间每两个小时,早上八点 0 23-7/2,8 * * * 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 0 11 4 * 1-3 1月1日早上4点 0 4 1 1 *quartz的高级特性不仅如此 1 数据库存储 2 集群支持 3 数据库持久化任务,trigger 4 trigger 的停止,运行 5 任务的任意添加 6 比corntrigger 更详尽的任务安排 7 线程的内部数据交换

定时任务是什么意思,求大神解答

好像是“在每天凌晨1点到凌晨1:59期间的每1分钟触发一次”