标签:quartz springmvc easyui 动态添加修改暂停恢复
1、把quartz的表达式信息放到数据库中:
package cn.edu.nuc.entity; public class ScheduleJob { private Integer id; /** 任务id */ private String jobId; /** 任务名称 */ private String jobName; /** 任务分组 */ private String jobGroup; /** 任务状态 0禁用 1启用 2删除*/ private int jobStatus; /** 任务运行时间表达式 */ private String cronExpression; /** 任务描述 */ private String desc; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getJobId() { return jobId; } public void setJobId(String jobId) { this.jobId = jobId; } public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } public String getJobGroup() { return jobGroup; } public void setJobGroup(String jobGroup) { this.jobGroup = jobGroup; } public int getJobStatus() { return jobStatus; } public void setJobStatus(int jobStatus) { this.jobStatus = jobStatus; } public String getCronExpression() { return cronExpression; } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public ScheduleJob() { super(); } public ScheduleJob(String jobId, String jobName, String jobGroup, int jobStatus, String cronExpression, String desc) { super(); this.jobId = jobId; this.jobName = jobName; this.jobGroup = jobGroup; this.jobStatus = jobStatus; this.cronExpression = cronExpression; this.desc = desc; } }
2、spring集成quartz
quartz.xml
<bean name="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> </bean>使用quartz的本身调度(集成Job类)
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import cn.edu.nuc.entity.ScheduleJob; public class QuartzJobFactory implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("任务成功运行"); ScheduleJob scheduleJob = (ScheduleJob)context.getMergedJobDataMap().get("scheduleJob"); System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]"); } }
public void init() throws Exception { Scheduler scheduler = schedulerFactoryBean.getScheduler(); // 可执行的任务列表 List<ScheduleJob> taskList = quartzService.getJobs(); for (ScheduleJob job : taskList) { TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(),job.getJobGroup()); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); // 不存在,创建一个 if (null == trigger) { JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class) .withIdentity(job.getJobName(),job.getJobGroup()).build(); jobDetail.getJobDataMap().put("scheduleJob", job); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(job.getCronExpression()); // 按新的表达式构建一个新的trigger trigger = TriggerBuilder.newTrigger() .withIdentity(job.getJobName(),job.getJobGroup()) .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); } else { // trigger已存在,则更新相应的定时设置 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(job.getCronExpression()); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, trigger); } } }
//暂停、恢复 public void pauseQuartz(String id,int flag) throws Exception { ScheduleJob scheduleJob = quartzJobDao.get(Integer.parseInt(id)); Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); if (flag == 0) { scheduler.pauseJob(jobKey); } else { scheduler.resumeJob(jobKey); } /** 任务状态 0禁用 1启用 2删除*/ Map map = new HashMap(); map.put("id", id); map.put("jobStatus", flag); quartzJobDao.updateParam(map, "updateJob"); } //新增或修改 public void updateQuartz(ScheduleJob job) throws Exception { Scheduler scheduler = schedulerFactoryBean.getScheduler(); if(null!=job){ //获取触发器标识 TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); //获取触发器trigger CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if(null==trigger){//不存在任务 //创建任务 JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class) .withIdentity(job.getJobName(), job.getJobGroup()) .build(); jobDetail.getJobDataMap().put("scheduleJob", job); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job .getCronExpression()); //按新的cronExpression表达式构建一个新的trigger trigger = TriggerBuilder.newTrigger() .withIdentity(job.getJobName(), job.getJobGroup()) .withSchedule(scheduleBuilder) .build(); scheduler.scheduleJob(jobDetail, trigger); //把任务插入数据库 quartzJobDao.insert(job); }else{//存在任务 // Trigger已存在,那么更新相应的定时设置 //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); //按新的cronExpression表达式重新构建trigger Trigger newtrigger = trigger.getTriggerBuilder() .withIdentity(triggerKey) .withSchedule(scheduleBuilder) .build(); //按新的trigger重新设置job执行 scheduler.rescheduleJob(triggerKey, newtrigger); //更新数据库中的任务 Map map = new HashMap(); map.put("jobName", job.getJobName()); map.put("jobGroup", job.getJobGroup()); map.put("cronExpression", job.getCronExpression()); quartzJobDao.updateParam(map, "updateJobTriger"); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
springmvc+easyui实现界面控制quartz的暂停、恢复、修改、添加
标签:quartz springmvc easyui 动态添加修改暂停恢复
原文地址:http://blog.csdn.net/liuxiao723846/article/details/46913205