quartz的定时任务使用的补充。
来源:互联网 发布:vb datagrid增加列 编辑:程序博客网 时间:2024/06/04 16:07
package com.jzdata.logv.alert.job;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.ScheduleBuilder;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import com.jzdata.logv.alert.common.DateHelper;
public class AlertTriggerBuilder {
private static Logger logger = Logger.getLogger(AlertTriggerBuilder.class);
public static Trigger buildTrigger(JSONObject queryJo){
String interval = queryJo.getString("run_every");
String startTime = queryJo.getString("first_execute");
String group = queryJo.getString("alert_name");
String name = queryJo.getString("alert_type");
return buildTrigger(interval, startTime, name, group);
}
// 重点在合理 的配置
public static Trigger buildTrigger(String interval, String startTime, String name, String group){
Date triggerStartTime = DateHelper.stringToDate(startTime);
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(name, group)
.withSchedule(buildCron(interval, startTime))
.startAt(triggerStartTime)
.build();
return trigger;
}
// 这是配置 时间的间隙。
public static ScheduleBuilder<?> buildCron(String interval, String startTime){
ScheduleBuilder<?> cron = null;
int num = Integer.parseInt(interval.substring(0, interval.length()-1).trim());
String unit = interval.substring(interval.length()-1);
if(unit.equals("s")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(num);
} else if(unit.equals("m")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(num);
} else if(unit.equals("h")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num);
} else if(unit.equals("d")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num * 24);
} else if(unit.equals("w")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num * 24 * 7);
} else if(unit.equals("M")){
cron = CronScheduleBuilder.cronSchedule(lowerMonth(startTime, num));
} else if(unit.equals("y")){
// num default to be 1 when unit is y
cron = CronScheduleBuilder.cronSchedule(lowerYear(startTime));
}
return cron;
}
public static String lowerMonth(String startTime, int num){
Date date = DateHelper.stringToDate(startTime);
int s = date.getSeconds();
int m = date.getMinutes();
int h = date.getHours();
int d = date.getDate();
int M = date.getMonth()+1;
String cron = "" + s + " " + m + " " + h + " " + d + " " + setList(M,num,12) + " " + "?";
return cron;
}
public static String lowerYear(String startTime){
Date date = DateHelper.stringToDate(startTime);
int s = date.getSeconds();
int m = date.getMinutes();
int h = date.getHours();
int d = date.getDate();
int M = date.getMonth()+1;
String cron = "" + s + " " + m + " " + h + " " + d + " " + M + " " + "?" + " " + "*";
return cron;
}
public static String setList(int M, int num, int up){
List<Integer> list = new ArrayList<Integer>();
int temp = M;
list.add(temp);
temp = M - num;
while(temp > 0){
list.add(0, temp);
temp -= num;
}
temp = M + num;
while(temp <= up){
list.add(temp);
temp += num;
}
String numList = "";
for(int i : list){
numList += (i + ",");
}
return numList.substring(0, numList.length()-1);
}
public static void main(String[] args){
String s = lowerYear("2016-03-31 15:58:27");
System.out.println(s);
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.ScheduleBuilder;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import com.jzdata.logv.alert.common.DateHelper;
public class AlertTriggerBuilder {
private static Logger logger = Logger.getLogger(AlertTriggerBuilder.class);
public static Trigger buildTrigger(JSONObject queryJo){
String interval = queryJo.getString("run_every");
String startTime = queryJo.getString("first_execute");
String group = queryJo.getString("alert_name");
String name = queryJo.getString("alert_type");
return buildTrigger(interval, startTime, name, group);
}
// 重点在合理 的配置
public static Trigger buildTrigger(String interval, String startTime, String name, String group){
Date triggerStartTime = DateHelper.stringToDate(startTime);
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(name, group)
.withSchedule(buildCron(interval, startTime))
.startAt(triggerStartTime)
.build();
return trigger;
}
// 这是配置 时间的间隙。
public static ScheduleBuilder<?> buildCron(String interval, String startTime){
ScheduleBuilder<?> cron = null;
int num = Integer.parseInt(interval.substring(0, interval.length()-1).trim());
String unit = interval.substring(interval.length()-1);
if(unit.equals("s")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(num);
} else if(unit.equals("m")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(num);
} else if(unit.equals("h")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num);
} else if(unit.equals("d")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num * 24);
} else if(unit.equals("w")){
cron = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInHours(num * 24 * 7);
} else if(unit.equals("M")){
cron = CronScheduleBuilder.cronSchedule(lowerMonth(startTime, num));
} else if(unit.equals("y")){
// num default to be 1 when unit is y
cron = CronScheduleBuilder.cronSchedule(lowerYear(startTime));
}
return cron;
}
public static String lowerMonth(String startTime, int num){
Date date = DateHelper.stringToDate(startTime);
int s = date.getSeconds();
int m = date.getMinutes();
int h = date.getHours();
int d = date.getDate();
int M = date.getMonth()+1;
String cron = "" + s + " " + m + " " + h + " " + d + " " + setList(M,num,12) + " " + "?";
return cron;
}
public static String lowerYear(String startTime){
Date date = DateHelper.stringToDate(startTime);
int s = date.getSeconds();
int m = date.getMinutes();
int h = date.getHours();
int d = date.getDate();
int M = date.getMonth()+1;
String cron = "" + s + " " + m + " " + h + " " + d + " " + M + " " + "?" + " " + "*";
return cron;
}
public static String setList(int M, int num, int up){
List<Integer> list = new ArrayList<Integer>();
int temp = M;
list.add(temp);
temp = M - num;
while(temp > 0){
list.add(0, temp);
temp -= num;
}
temp = M + num;
while(temp <= up){
list.add(temp);
temp += num;
}
String numList = "";
for(int i : list){
numList += (i + ",");
}
return numList.substring(0, numList.length()-1);
}
public static void main(String[] args){
String s = lowerYear("2016-03-31 15:58:27");
System.out.println(s);
}
}
- 字段 允许值 允许的特殊字符
- 秒 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触发
- 特殊字符 意义
- * 表示所有值;
- ? 表示未说明的值,即不关心它为何值;
- - 表示一个指定的范围;
- , 表示附加一个可能值;
- / 符号前表示开始时间,符号后表示每次递增的值;
- L("last") ("last") "L" 用在day-of-month字段意思是 "这个月最后一天";用在 day-of-week字段, 它简单意思是 "7" or "SAT"。 如果在day-of-week字段里和数字联合使用,它的意思就是 "这个月的最后一个星期几" – 例如: "6L" means "这个月的最后一个星期五". 当我们用“L”时,不指明一个列表值或者范围是很重要的,不然的话,我们会得到一些意想不到的结果。
- W("weekday") 只能用在day-of-month字段。用来描叙最接近指定天的工作日(周一到周五)。例如:在day-of-month字段用“15W”指“最接近这个月第15天的工作日”,即如果这个月第15天是周六,那么触发器将会在这个月第14天即周五触发;如果这个月第15天是周日,那么触发器将会在这个月第16天即周一触发;如果这个月第15天是周二,那么就在触发器这天触发。注意一点:这个用法只会在当前月计算值,不会越过当前月。“W”字符仅能在day-of-month指明一天,不能是一个范围或列表。也可以用“LW”来指定这个月的最后一个工作日。
- # 只能用在day-of-week字段。用来指定这个月的第几个周几。例:在day-of-week字段用"6#3"指这个月第3个周五(6指周五,3指第3个)。如果指定的日期不存在,触发器就不会触发。
- C 指和calendar联系后计算过的值。例:在day-of-month 字段用“5C”指在这个月第5天或之后包括calendar的第一天;在day-of-week字段用“1C”指在这周日或之后包括calendar的第一天
0 0
- quartz的定时任务使用的补充。
- quartz定时任务的使用
- quartz的定时任务
- 定时任务调度器Quartz的使用
- 使用spring 的Quartz 定时任务入门
- 使用Spring+Quartz的定时任务。
- sping quartz定时任务的使用
- Java定时任务Quartz的使用
- 关于Quartz定时任务的使用
- spring的quartz定时任务
- quartz的定时调度任务
- quartz 简单的定时任务
- 定时任务,quartz的demo
- 定时任务 quartz使用
- 使用Quartz实现比Timer更强大的任务定时
- Spring与Quartz的结合使用 主要用于定时任务!!!
- 定时任务-quartz的使用,实现可页面化管理
- quartz系列(三):定时任务的集群使用
- Hibernate学习及问题记录(1)——纠结的第一战
- javascript中this执行上下文的改变
- SVN 代码回滚(转)
- Android性能优化总结
- C 程序的存储空间布局
- quartz的定时任务使用的补充。
- windows go开发环境(3) IDE
- 关于销售订单的状态
- apache https ssl配置详细步骤讲解
- JAVA 使用Dom4j 解析XML
- centOS6.5下安装jdk1.7
- BigDecimal.dive(one,scale,BigDecimal.ROUND_HALF_UP)不能四舍五入!!
- 乱序字符串
- sicily Tiling a Grid With Dominoes