Java 定时任务JOB创建过程
1.创建QuartzManager管理类。
如果需要可根据自身的需要对此类的方法进行重载,以符合自身业务
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import java.util.Map;
public class QuartzManager {
private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param map
* 描述
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, Map map, @SuppressWarnings("rawtypes") Class cls, String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类
jobDetail.setJobDataMap(new JobDataMap(map));
//jobDetail.setDescription(description);
// 触发器
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 添加一个定时任务
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) {
try {
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
* @param time
*
* @Title: QuartzManager.java
*/
@SuppressWarnings("rawtypes")
public static void modifyJobTime(Scheduler sched, String jobName, String time) {
try {
CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail = sched.getJobDetail(jobName, JOB_GROUP_NAME);
Class objJobClass = jobDetail.getJobClass();
removeJob(sched, jobName);
addJob(sched, jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间
*
* @param sched
* 调度器 *
* @param sched
* 调度器
* @param triggerName
* @param triggerGroupName
* @param time
*
* @Title: QuartzManager.java
*/
public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) {
try {
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
sched.resumeTrigger(triggerName, triggerGroupName);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param sched
* 调度器
* @param jobName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched, String jobName) {
try {
sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务
*
* @param sched
* 调度器
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
* @Title: QuartzManager.java
*/
public static void removeJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:启动所有定时任务
*
* @param sched
* 调度器
*
* @Title: QuartzManager.java
*/
public static void startJobs(Scheduler sched) {
try {
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:关闭所有定时任务
*
*
* @param sched
* 调度器
*
*
* @Title: QuartzManager.java
*/
public static void shutdownJobs(Scheduler sched) {
try {
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2.创建定时任务实现类
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class QuartzJob implements Job{
@Override
public void execute(JobExecutionContext push) throws JobExecutionException {
//System.out.println("我只是一个定时任务,你们看不见我");
//TODO业务代码省略
}
}
3.如何使用,创建以上两个类之后,我们就该调用了
//
public void test(){
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler sche = schedulerFactory.getScheduler();
String taskID = "7879879798797";
Map map = new HashMap();
map.put("test1","我是业务参数1");
map.put("test2","我是业务参数2");
QuartzManager.addJob(sche, taskID,map, QuartzJob.class, CornUtil.getCron(DateUtil.stringToDate("2030-07-19 12:12:12","yyyy-MM-dd HH:mm:ss")));
}
附CornUtil类(此类是时间转core表达式)
import java.text.SimpleDateFormat;
import java.util.Date;
public class CornUtil {
/***
*
* @param date
* @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String formatDateByPattern(Date date, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatTimeStr = null;
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
/***
* convert Date to cron ,eg. "0 07 10 15 1 ? 2016"
* @param date : 时间点
* @return
*/
public static String getCron(Date date) {
String dateFormat = "ss mm HH dd MM ? yyyy";
return formatDateByPattern(date, dateFormat);
}
}
附时间转化类DateUtil
import java.text.;
import java.util.;
public class DateUtil {
public static final String ISO_DATE_FORMAT = "yyyyMMdd";
public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";
/**
* yyyy-MM-dd hh:mm:ss
*/
public static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String DATE_PATTERN = "yyyyMMddHHmmss";
private static boolean LENIENT_DATE = false;
private static Random random = new java.util.Random();
private static final int ID_BYTES = 10;
public synchronized static String generateId() {
StringBuffer result = new StringBuffer();
result = result.append(System.currentTimeMillis());
for (int i = 0; i < ID_BYTES; i++) {
result = result.append(random.nextInt(10));
}
return result.toString();
}
protected static final float normalizedJulian(float JD) {
float f = Math.round(JD + 0.5f) - 0.5f;
return f;
}
/**
* Returns the Date from a julian. The Julian date will be converted to noon GMT,
* such that it matches the nearest half-integer (i.e., a julian date of 1.4 gets
* changed to 1.5, and 0.9 gets changed to 0.5.)
*
* @param JD the Julian date
* @return the Gregorian date
*/
public static final Date toDate(float JD) {
/* To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so
* that it ends in 0.5). Do the following calculations, again dropping the fractional part of all
* multiplicatons and divisions. Note: This method will not give dates accurately on the
* Gregorian Proleptic Calendar, i.e., the calendar you get by extending the Gregorian
* calendar backwards to years earlier than 1582. using the Gregorian leap year
* rules. In particular, the method fails if Y<400. */
float Z = (normalizedJulian(JD)) + 0.5f;
float W = (int) ((Z - 1867216.25f) / 36524.25f);
float X = (int) (W / 4f);
float A = Z + 1 + W - X;
float B = A + 1524;
float C = (int) ((B - 122.1) / 365.25);
float D = (int) (365.25f * C);
float E = (int) ((B - D) / 30.6001);
float F = (int) (30.6001f * E);
int day = (int) (B - D - F);
int month = (int) (E - 1);
if (month > 12) {
month = month - 12;
}
int year = (int) (C - 4715); //(if Month is January or February) or C-4716 (otherwise)
if (month > 2) {
year--;
}
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1); // damn 0 offsets
c.set(Calendar.DATE, day);
return c.getTime();
}
/**
* Returns the days between two dates. Positive values indicate that
* the second date is after the first, and negative values indicate, well,
* the opposite. Relying on specific times is problematic.
*
* @param early the "first date"
* @param late the "second date"
* @return the days between the two dates
*/
public static final int daysBetween(Date early, Date late) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(early);
c2.setTime(late);
return daysBetween(c1, c2);
}
/**
* Returns the days between two dates. Positive values indicate that
* the second date is after the first, and negative values indicate, well,
* the opposite.
*
* @param early
* @param late
* @return the days between two dates.
*/
public static final int daysBetween(Calendar early, Calendar late) {
return (int) (toJulian(late) - toJulian(early));
}
/**
* Return a Julian date based on the input parameter. This is
* based from calculations found at
* <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
* (Gregorian Calendar)</a>, provided by Bill Jeffrys.
* @param c a calendar instance
* @return the julian day number
*/
public static final float toJulian(Calendar c) {
int Y = c.get(Calendar.YEAR);
int M = c.get(Calendar.MONTH);
int D = c.get(Calendar.DATE);
int A = Y / 100;
int B = A / 4;
int C = 2 - A + B;
float E = (int) (365.25f * (Y + 4716));
float F = (int) (30.6001f * (M + 1));
float JD = C + D + E + F - 1524.5f;
return JD;
}
/**
* Return a Julian date based on the input parameter. This is
* based from calculations found at
* <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
* (Gregorian Calendar)</a>, provided by Bill Jeffrys.
* @param date
* @return the julian day number
*/
public static final float toJulian(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return toJulian(c);
}
/**
* @param isoString
* @param fmt
* @param field Calendar.YEAR/Calendar.MONTH/Calendar.DATE
* @param amount
* @return
* @throws ParseException
*/
public static final String dateIncrease(String isoString, String fmt,
int field, int amount) {
try {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(stringToDate(isoString, fmt, true));
cal.add(field, amount);
return dateToString(cal.getTime(), fmt);
} catch (Exception ex) {
return null;
}
}
/**
* Time Field Rolling function.
* Rolls (up/down) a single unit of time on the given time field.
*
* @param isoString
* @param field the time field.
* @param up Indicates if rolling up or rolling down the field value.
* @param expanded use formating char's
* @exception ParseException if an unknown field value is given.
*/
public static final String roll(String isoString, String fmt, int field,
boolean up) throws ParseException {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(stringToDate(isoString, fmt));
cal.roll(field, up);
return dateToString(cal.getTime(), fmt);
}
/**
* Time Field Rolling function.
* Rolls (up/down) a single unit of time on the given time field.
*
* @param isoString
* @param field the time field.
* @param up Indicates if rolling up or rolling down the field value.
* @exception ParseException if an unknown field value is given.
*/
public static final String roll(String isoString, int field, boolean up) throws
ParseException {
return roll(isoString, DATETIME_PATTERN, field, up);
}
/**
* java.util.Date
* @param dateText
* @param format
* @param lenient
* @return
*/
public static Date stringToDate(String dateText, String format,
boolean lenient) {
if (dateText == null) {
return null;
}
DateFormat df = null;
try {
if (format == null) {
df = new SimpleDateFormat();
} else {
df = new SimpleDateFormat(format);
}
// setLenient avoids allowing dates like 9/32/2001
// which would otherwise parse to 10/2/2001
df.setLenient(false);
return df.parse(dateText);
} catch (ParseException e) {
return null;
}
}
/**
* @return Timestamp
*/
public static java.sql.Timestamp getCurrentTimestamp() {
return new java.sql.Timestamp(new java.util.Date().getTime());
}
/** java.util.Date
* @param dateText
* @param format
* @return
*/
public static Date stringToDate(String dateString, String format) {
return stringToDate(dateString, format, LENIENT_DATE);
}
/**
* java.util.Date
* @param dateText
*/
public static Date stringToDate(String dateString) {
return stringToDate(dateString, ISO_EXPANDED_DATE_FORMAT, LENIENT_DATE);
}
/**
* @return
* @param pattern
* @param date
*/
public static String dateToString(Date date, String pattern) {
if (date == null) {
return null;
}
try {
SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
sfDate.setLenient(false);
return sfDate.format(date);
} catch (Exception e) {
return null;
}
}
/**
* yyyy-MM-dd
* @param date
* @return
*/
public static String dateToString(Date date) {
return dateToString(date, ISO_EXPANDED_DATE_FORMAT);
}
/**
* @return
*/
public static Date getCurrentDateTime() {
java.util.Calendar calNow = java.util.Calendar.getInstance();
java.util.Date dtNow = calNow.getTime();
return dtNow;
}
/**
*
* @param pattern
* @return
*/
public static String getCurrentDateString(String pattern) {
return dateToString(getCurrentDateTime(), pattern);
}
/**
* yyyy-MM-dd
* @return
*/
public static String getCurrentDateString() {
return dateToString(getCurrentDateTime(), ISO_EXPANDED_DATE_FORMAT);
}
/**
* 返回固定格式的当前时间
* yyyy-MM-dd hh:mm:ss
* @param date
* @return
*/
public static String dateToStringWithTime( ) {
return dateToString(new java.util.Date(), DATETIME_PATTERN);
}
/**
* yyyy-MM-dd hh:mm:ss
* @param date
* @return
*/
public static String dateToStringWithTime(Date date) {
return dateToString(date, DATETIME_PATTERN);
}
/**
*
* @param date
* @param days
* @return java.util.Date
*/
public static Date dateIncreaseByDay(Date date, int days) {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
/**
*
* @param date
* @param days
* @return java.util.Date
*/
public static Date dateIncreaseByMonth(Date date, int mnt) {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(date);
cal.add(Calendar.MONTH, mnt);
return cal.getTime();
}
/**
*
* @param date
* @param mnt
* @return java.util.Date
*/
public static Date dateIncreaseByYear(Date date, int mnt) {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(date);
cal.add(Calendar.YEAR, mnt);
return cal.getTime();
}
/**
*
* @param date yyyy-MM-dd
* @param days
* @return yyyy-MM-dd
*/
public static String dateIncreaseByDay(String date, int days) {
return dateIncreaseByDay(date, ISO_DATE_FORMAT, days);
}
/**
* @param date
* @param fmt
* @param days
* @return
*/
public static String dateIncreaseByDay(String date, String fmt, int days) {
return dateIncrease(date, fmt, Calendar.DATE, days);
}
/**
*
* @param src
* @param srcfmt
* @param desfmt
* @return
*/
public static String stringToString(String src, String srcfmt,
String desfmt) {
return dateToString(stringToDate(src, srcfmt), desfmt);
}
/**
*
* @param date
* @return string
*/
public static String getYear(Date date) {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
"yyyy");
String cur_year = formater.format(date);
return cur_year;
}
/**
*
* @param date
* @return string
*/
public static String getMonth(Date date) {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
"MM");
String cur_month = formater.format(date);
return cur_month;
}
/**
* @param date
* @return string
*/
public static String getDay(Date date) {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
"dd");
String cur_day = formater.format(date);
return cur_day;
}
/**
* @param date
* @return string
*/
public static String getHour(Date date) {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat(
"HH");
String cur_day = formater.format(date);
return cur_day;
}
public static int getMinsFromDate(java.util.Date dt) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(dt);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
return ((hour * 60) + min);
}
/**
* Function to convert String to Date Object. If invalid input then current or next day date
* is returned (Added by Ali Naqvi on 2006-5-16).
* @param str String input in YYYY-MM-DD HH:MM[:SS] format.
* @param isExpiry boolean if set and input string is invalid then next day date is returned
* @return Date
*/
public static java.util.Date convertToDate(String str, boolean isExpiry) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
java.util.Date dt = null;
try {
dt = fmt.parse(str);
} catch (ParseException ex) {
Calendar cal = Calendar.getInstance();
if (isExpiry) {
cal.add(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
} else {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
}
dt = cal.getTime();
}
return dt;
}
public static java.util.Date convertToDate(String str) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm");
java.util.Date dt = null;
try {
dt = fmt.parse(str);
} catch (ParseException ex) {
dt = new java.util.Date();
}
return dt;
}
public static String dateFromat(Date date, int minute) {
String dateFormat = null;
int year = Integer.parseInt(getYear(date));
int month = Integer.parseInt(getMonth(date));
int day = Integer.parseInt(getDay(date));
int hour = minute / 60;
int min = minute % 60;
dateFormat = String.valueOf(year)
+
(month > 9 ? String.valueOf(month) :
"0" + String.valueOf(month))
+
(day > 9 ? String.valueOf(day) : "0" + String.valueOf(day))
+ " "
+
(hour > 9 ? String.valueOf(hour) : "0" + String.valueOf(hour))
+
(min > 9 ? String.valueOf(min) : "0" + String.valueOf(min))
+ "00";
return dateFormat;
}
public static String sDateFormat() {
return new SimpleDateFormat(DATE_PATTERN).format(Calendar.getInstance().getTime());
}
public static Long getCurrentTimestampLong() {
return new java.util.Date().getTime();
}
public static void main(String[] args)
{
String timeDir=DateUtil.dateToString(new Date(),DateUtil.ISO_EXPANDED_DATE_FORMAT);
System.out.println(timeDir);
}
}
今天的文章Java 定时任务JOB分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/10167.html