Bean類java
@Entity dom
@Table(name = "MYTASK")maven
public class MyTask implements Serializable{ui
/**this
* spa
*/.net
private static final long serialVersionUID = 1L;ip
@Idrem
@GeneratedValue(strategy = GenerationType.AUTO)文檔
private Long id;
/**
* 任務名
*/
@Column(name="JOB_NAME",nullable=false)
private String jobName;
/**
* 任務組名
*/
@Column(name="GROUP_NAME",nullable=false)
private String groupName;
/**
* 觸發器名
*/
@Column(name="TRIGGER_NAME",nullable=false)
private String triggerName;
/**
* 觸發器組名
*/
@Column(name="TRIGGER_GROUP_NAME",nullable=false)
private String triggerGroupName;
/**
* 任務
*/
@Column(name="JOB",nullable=false)
private String job;
/**
* cron表達式
*/
@Column(name="TIME",nullable=false)
private String time;
/**
* 任務狀態
* 0未啓動
* 1啓動
* 2暫停
* 3中止
*/
@Column(name="STATUS")
private Integer status=0;
/**
* 備註
*/
@Column(name="ISDELETE",nullable=false)
private Integer isdelete=0;
@Column(name="NOTE")
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getTriggerName() {
return triggerName;
}
public void setTriggerName(String triggerName) {
this.triggerName = triggerName;
}
public String getTriggerGroupName() {
return triggerGroupName;
}
public void setTriggerGroupName(String triggerGroupName) {
this.triggerGroupName = triggerGroupName;
}
public Integer getIsdelete() {
return isdelete;
}
public void setIsdelete(Integer isdelete) {
this.isdelete = isdelete;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
maven配置
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.2</version>
</dependency>
/**
動態生成任務類
*/
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
/**
* @Description: 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名
*
* @param jobName
* 任務名
* @param cls
* 任務
* @param time
* 時間設置,參考quartz說明文檔
*
*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName, Class cls, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
//JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任務名,任務組,任務執行類
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(
jobName, JOB_GROUP_NAME).usingJobData("domain",
"www.jmatrix.org").usingJobData("rank", "求別提~~~").build();
// 觸發器
//CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 觸發器名,觸發器組
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity(jobName, JOB_GROUP_NAME).withSchedule(CronScheduleBuilder.cronSchedule((time))).build();
sched.scheduleJob(jobDetail, (Trigger) trigger);
// 啓動
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 添加一個定時任務
*
* @param jobName
* 任務名
* @param jobGroupName
* 任務組名
* @param triggerName
* 觸發器名
* @param triggerGroupName
* 觸發器組名
* @param jobClass
* 任務
* @param time
* 時間設置,參考quartz說明文檔
*
*/
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class cls,String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(
jobName, jobGroupName).usingJobData("domain",
"www.jmatrix.org").usingJobData("rank", "求別提~~~").build();
// 觸發器
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName).withSchedule(CronScheduleBuilder.cronSchedule((time))).build();
sched.scheduleJob(jobDetail, trigger);
// 啓動
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名)
*
* @param jobName
* @param time
*
*/
public static void modifyJobTime(String jobName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) sched.getTrigger(new TriggerKey(jobName, JOB_GROUP_NAME));
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail = sched.getJobDetail(new JobKey(jobName));
Class objJobClass = jobDetail.getJobClass();
sched.resumeJob(new JobKey(jobName));
addJob(jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一個任務的觸發時間
*
* @param triggerName
* @param triggerGroupName
* @param time
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:49:37
* @version V2.0
*/
/* public static void modifyJobTime(String triggerName,
String triggerGroupName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
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 jobName
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:49:51
* @version V2.0
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(new TriggerKey(jobName, JOB_GROUP_NAME));// 中止觸發器
sched.resumeTrigger(new TriggerKey(jobName, JOB_GROUP_NAME));// 移除觸發器
sched.resumeJob(new JobKey(jobName));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一個任務
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(new TriggerKey(jobName, jobGroupName));// 暫停觸發器
sched.deleteJob(new JobKey(jobName,jobGroupName));//中止任務
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 暫停任務
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void pauseJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(new TriggerKey(jobName, jobGroupName));// 中止觸發器
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 從新啓動任務
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void resumeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.resumeTrigger(new TriggerKey(jobName, jobGroupName)); //從新啓動任務
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:啓動全部定時任務
*
*
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:關閉全部定時任務
*
*
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
此處省略後臺業務邏輯代碼。。。。。。。