Bean类
@Entity
@Table(name = "MYTASK")
public class MyTask implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@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);
}
}
}
此处省略后台业务逻辑代码。。。。。。。