quartz在spring下的动态控制(增、删、改、查、暂停、恢复、中断等)

由于业务需要,研究了一下quartz的动态配置,其实没有什么技术含量,需要的同仁们看下吧。有问题加qq群沟通:52200686 。

先把源码地址贴上吧,http://download.csdn.net/detail/celins7/8859007

所需数据库表生成的sql文件,稍后在最下面附上,请根据自己的数据库导入。

代码:

Action类 ConfigQuartzAction 中的方法:

        StdScheduler scheduler = (StdScheduler)SpringContext.getBean("quScheduler");
ConfigQuartzJobs quartzJobs = (ConfigQuartzJobs)SpringContext.getBean("quartzJobs");
/**

* @Title: list
* @author 孔庆胜 
* @Description: 所有定时任务列表
* @return
* @throws SchedulerException
*/
public String list(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
List<CronTriggerModel> cronList = new ArrayList<CronTriggerModel>();
try {
String[] triggerGroups;
       String[] triggers;
       triggerGroups = scheduler.getTriggerGroupNames();
       for (int i = 0; i < triggerGroups.length; i++) {
          triggers = scheduler.getTriggerNames(triggerGroups[i]);
          for (int j = 0; j < triggers.length; j++) {
             Trigger tg = scheduler.getTrigger(triggers[j], triggerGroups[i]);
             if(tg instanceof CronTrigger){
             JobDetail jobDetail = scheduler.getJobDetail(tg.getJobName(), tg.getJobGroup());
             CronTriggerModel ctm = new CronTriggerModel();
             ctm.setTriggerName(((CronTrigger)tg).getName());
             ctm.setTriggerGroupName(((CronTrigger)tg).getGroup());
             ctm.setJobName(((CronTrigger)tg).getJobName());
             ctm.setJobGroupName(((CronTrigger)tg).getJobGroup());
             ctm.setClassPath(jobDetail.getJobClass().getCanonicalName());
             ctm.setCronExpression(((CronTrigger)tg).getCronExpression());
             ctm.setStartTime(format.format(((CronTrigger)tg).getStartTime()));
             ctm.setNextFireTime(format.format(((CronTrigger)tg).getNextFireTime()));
             ctm.setPriority(((CronTrigger)tg).getPriority());
             ctm.setDescription(((CronTrigger)tg).getDescription());
             if(scheduler.getTriggerState(tg.getName(), tg.getGroup())==Trigger.STATE_NORMAL){
             ctm.setState("正常");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_PAUSED){
             ctm.setState("暂停");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_COMPLETE){
             ctm.setState("已完成");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_ERROR){
             ctm.setState("错误");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_BLOCKED){
             ctm.setState("锁定");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_NONE){
             ctm.setState("无状态");
             }
             cronList.add(ctm);
             }
          }
       }
       this.getAttributeUtil().setAttribute("cronList", Scope.REQUEST, cronList);
} catch (BeansException e) {
e.printStackTrace();
} catch (SchedulerException e) {
e.printStackTrace();
}

return "list";
}
/**

* @Title: simpList
* @author 孔庆胜 
* @Description:循环任务列表
* @date: 2015-6-25 下午6:24:25
* @return
*/
public String simpList(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<SimpleTriggerModel> simpList = new ArrayList<SimpleTriggerModel>();
try {
String[] triggerGroups;
       String[] triggers;
       triggerGroups = scheduler.getTriggerGroupNames();
       for (int i = 0; i < triggerGroups.length; i++) {
          triggers = scheduler.getTriggerNames(triggerGroups[i]);
          for (int j = 0; j < triggers.length; j++) {
             Trigger tg = scheduler.getTrigger(triggers[j], triggerGroups[i]);
             if (tg instanceof SimpleTrigger) {
             JobDetail jobDetail = scheduler.getJobDetail(tg.getJobName(), tg.getJobGroup());
             SimpleTriggerModel sim = new SimpleTriggerModel();
             sim.setTriggerName(((SimpleTrigger)tg).getName());
             sim.setTriggerGroupName(((SimpleTrigger)tg).getGroup());
             sim.setJobName(((SimpleTrigger)tg).getJobName());
             sim.setJobGroupName(((SimpleTrigger)tg).getJobGroup());
             sim.setRepeatInterval(((SimpleTrigger)tg).getRepeatInterval());
             sim.setRepeatCount(((SimpleTrigger)tg).getRepeatCount());
             sim.setStartTime(format.format(((SimpleTrigger)tg).getStartTime()));
             sim.setNextFireTime(((SimpleTrigger)tg).getNextFireTime()==null?"无":format.
             format(((SimpleTrigger)tg).getNextFireTime()));
             sim.setPriority(((SimpleTrigger)tg).getPriority());
             sim.setDescription(((SimpleTrigger)tg).getDescription());
             sim.setClassPath(jobDetail.getJobClass().getCanonicalName());
             sim.setTimesTriggered(((SimpleTrigger)tg).getTimesTriggered());
             if(scheduler.getTriggerState(tg.getName(), tg.getGroup())==Trigger.STATE_NORMAL){
             sim.setState("正常");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_PAUSED){
             sim.setState("暂停");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_COMPLETE){
             sim.setState("已完成");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_ERROR){
             sim.setState("错误");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_BLOCKED){
             sim.setState("锁定");
             }else if(scheduler.getTriggerState(tg.getName(), tg.getGroup())== Trigger.STATE_NONE){
             sim.setState("无状态");
             }
             simpList.add(sim);
             }
          }
       }
       this.getAttributeUtil().setAttribute("simpList", Scope.REQUEST, simpList);
} catch (BeansException e) {
e.printStackTrace();
} catch (SchedulerException e) {
e.printStackTrace();
}
return "simpList";
}

/**

* @Title: toAdd
* @author 孔庆胜 
* @Description:跳转到添加页面
* @date: 2015-6-24 下午4:35:33
* @return
*/
public String toAdd(){
List<String> jobList = quartzJobs.getJobsList();
this.getAttributeUtil().setAttribute("jobList", Scope.REQUEST, jobList);
return "toAdd";
}
/**

* @Title: add
* @author 孔庆胜 
* @Description: 定时任务添加
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @param time
* @param priority
* @param description
* @return
*/
    public String add(@Read(key = "jobName")String jobName, 
             @Read(key = "jobGroupName")String jobGroupName, 
             @Read(key = "triggerName")String triggerName, 
             @Read(key = "triggerGroupName")String triggerGroupName,
             @Read(key = "time")String time,
             @Read(key = "priority")String priority,
             @Read(key = "description")String description,
             @Read(key = "classPath")String classPath){
    try {
    if(jobName == null || jobGroupName == null || triggerName == null || triggerGroupName == null
|| time == null || priority == null || description == null || classPath == null
|| "".equals(jobName) || "".equals(triggerName) || "".equals(time) 
|| "".equals(classPath)){
this.getAttributeUtil().setAttribute("error","信息填写不完整!");
return "reAdd";
}

    Class<?> jobClass = Class.forName(classPath);
JobDetail jobDetail = new JobDetail(jobName, 
jobGroupName.equals("")?"DefultCronJobGroup":jobGroupName, jobClass);
CronTrigger cronTrigger = new CronTrigger(triggerName, 
triggerGroupName.equals("")?"DefultCronTriggerGroup":triggerGroupName);
CronExpression cexp = new CronExpression(time);
cronTrigger.setCronExpression(cexp);
cronTrigger.setPriority(priority.equals("")?5:Integer.parseInt(priority));
cronTrigger.setDescription(description);
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (NumberFormatException e) {
this.getAttributeUtil().setAttribute("error","优先级格式错误!");
return "reAdd";
} catch (ParseException e) {
this.getAttributeUtil().setAttribute("error","时间表达式格式错误!");
return "reAdd";
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","添加任务失败,请查看是否有重名!");
return "reAdd";
} catch (ClassNotFoundException e) {
this.getAttributeUtil().setAttribute("error","执行类没有找到!");
return "reAdd";
}
   
return "add";
}
    
    /**
     * 
     * @Title: simpAdd
     * @author 孔庆胜 
     * @Description: 添加次数定时任务
     * @date: 2015-6-25 上午11:39:28
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @param repeatInterval
     * @param repeatCount
     * @param priority
     * @param description
     * @param classPath
     * @return
     */
    public String simpAdd(@Read(key = "jobName")String jobName, 
         @Read(key = "jobGroupName")String jobGroupName, 
         @Read(key = "triggerName")String triggerName, 
         @Read(key = "triggerGroupName")String triggerGroupName,
         @Read(key = "repeatInterval")String repeatInterval,
         @Read(key = "repeatCount")String repeatCount,
         @Read(key = "priority")String priority,
         @Read(key = "description")String description,
         @Read(key = "classPath")String classPath){
    if(jobName == null || jobGroupName == null || triggerName == null || triggerGroupName == null
|| repeatInterval == null || priority == null || description == null || classPath == null
|| repeatCount == null || "".equals(jobName) || "".equals(triggerName) 
|| "".equals(repeatInterval) || "".equals(classPath) || "".equals(repeatCount)){
this.getAttributeUtil().setAttribute("error","信息填写不完整!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
}
    try {
Class<?> jobClass = Class.forName(classPath);
JobDetail jobDetail = new JobDetail(jobName, 
jobGroupName.equals("")?"DefultSimpleJobGroup":jobGroupName, jobClass);
SimpleTrigger simpleTrigger = new SimpleTrigger(triggerName, 
triggerGroupName.equals("")?"DefultSimpleTriggerGroup":triggerGroupName);
if(Long.parseLong(repeatInterval) < 1){
this.getAttributeUtil().setAttribute("error","重复间隔输入错误,应为正整数!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
}
if(Integer.parseInt(repeatCount) < 1){
this.getAttributeUtil().setAttribute("error","重复次数输入错误,应为正整数!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
}
simpleTrigger.setStartTime(new Date());
simpleTrigger.setRepeatInterval(Long.parseLong(repeatInterval));
simpleTrigger.setRepeatCount(Integer.parseInt(repeatCount));
simpleTrigger.setPriority(priority.equals("")?5:Integer.parseInt(priority));
simpleTrigger.setDescription(description);
scheduler.scheduleJob(jobDetail, simpleTrigger);
} catch (NumberFormatException e) {
this.getAttributeUtil().setAttribute("error","数据格式化错误!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
} catch (ClassNotFoundException e) {
this.getAttributeUtil().setAttribute("error","执行类没有找到!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","添加任务失败,请查看是否有重名!");
this.getAttributeUtil().setAttribute("simple","block");
return "reAdd";
}
    return "simpAdd";
    }

    /**
     * 
     * @Title: toEdit
     * @author 孔庆胜 
     * @Description: 跳转修改页面
     * @date: 2015-6-25 上午11:39:04
     * @param triggerName
     * @param triggerGroupName
     * @return
     */
    public String toEdit(@Read(key = "tName")String triggerName, 
                         @Read(key = "tgName")String triggerGroupName){
    try {
Trigger trigger = (Trigger)scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
this.getAttributeUtil().setAttribute("error","触发器没有找到!");
return "add";
}
JobDetail jobDetail = scheduler.getJobDetail(trigger.getJobName(), trigger.getJobGroup());
if(trigger instanceof CronTrigger){
CronTrigger cronTrigger = (CronTrigger)trigger;
CronTriggerModel ctm = new CronTriggerModel();
ctm.setJobName(cronTrigger.getJobName());
ctm.setJobGroupName(cronTrigger.getJobGroup());
ctm.setTriggerName(triggerName);
ctm.setTriggerGroupName(triggerGroupName);
ctm.setCronExpression(cronTrigger.getCronExpression());
ctm.setPriority(cronTrigger.getPriority());
ctm.setDescription(cronTrigger.getDescription());
ctm.setClassPath(jobDetail.getJobClass().getCanonicalName());
this.getAttributeUtil().setAttribute("ctm", Scope.REQUEST, ctm);
}else if(trigger instanceof SimpleTrigger){
SimpleTrigger simpleTrigger = (SimpleTrigger)trigger;
SimpleTriggerModel stm = new SimpleTriggerModel();
stm.setJobName(simpleTrigger.getJobName());
stm.setJobGroupName(simpleTrigger.getJobGroup());
stm.setTriggerName(triggerName);
stm.setTriggerGroupName(triggerGroupName);
stm.setRepeatInterval(simpleTrigger.getRepeatInterval());
stm.setRepeatCount(simpleTrigger.getRepeatCount());
stm.setPriority(simpleTrigger.getPriority());
stm.setDescription(simpleTrigger.getDescription());
stm.setClassPath(jobDetail.getJobClass().getCanonicalName());
this.getAttributeUtil().setAttribute("stm", Scope.REQUEST, stm);
return "toSimpleEdit";
}
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","查找任务失败,请确认任务信息!");
return "add";
}

    return "toEdit";
    }
    /**
     * 
     * @Title: edit
     * @author 孔庆胜 
     * @Description: 定时任务修改
     * @param triggerName
     * @param triggerGroupName
     * @param time
     * @param priority
     * @param description
     * @return
     */
    public String edit(@Read(key = "triggerName")String triggerName, 
              @Read(key = "triggerGroupName")String triggerGroupName,
              @Read(key = "time")String time,
              @Read(key = "priority")String priority,
              @Read(key = "description")String description) {
try {
if(null == triggerName || null == triggerGroupName || null == time || null == priority 
|| "".equals(time) || null == description || "".equals(triggerName) 
|| "".equals(triggerGroupName)){
this.getAttributeUtil().setAttribute("error","信息填写不完整!");
return "edit";
}
CronTrigger cronTrigger = (CronTrigger)scheduler.getTrigger(triggerName, triggerGroupName);
if(cronTrigger == null){
this.getAttributeUtil().setAttribute("error","定时任务没有找到!");
return "edit";
}

if(!"".equals(time)){
CronExpression cexp = new CronExpression(time);
cronTrigger.setCronExpression(cexp);
}
if(!"".equals(priority)){
cronTrigger.setPriority(Integer.parseInt(priority));
}
cronTrigger.setDescription(description);
scheduler.rescheduleJob(triggerName, triggerGroupName, cronTrigger);
} catch (NumberFormatException e) {
this.getAttributeUtil().setAttribute("error","优先级格式错误!");
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","修改任务失败,请确认修改信息!");
} catch (ParseException e) {
this.getAttributeUtil().setAttribute("error","时间表达式格式错误!");
}
return "edit";
}
    
    public String simpEdit(@Read(key = "triggerName")String triggerName, 
          @Read(key = "triggerGroupName")String triggerGroupName,
          @Read(key = "repeatInterval")String repeatInterval,
          @Read(key = "repeatCount")String repeatCount,
          @Read(key = "priority")String priority,
          @Read(key = "description")String description){
    if(null == triggerName || null == triggerGroupName || null == repeatInterval || null == priority 
|| null == description || null == repeatCount || "".equals(triggerName) 
|| "".equals(triggerGroupName) || "".equals(repeatInterval) || "".equals(repeatCount)){
this.getAttributeUtil().setAttribute("error","信息填写不完整!");
return "simpAdd";
}
    try {
SimpleTrigger simpTrigger = (SimpleTrigger)scheduler.getTrigger(triggerName, triggerGroupName);
if(simpTrigger == null){
this.getAttributeUtil().setAttribute("error","定时任务没有找到!");
return "simpAdd";
}
if(!"".equals(repeatInterval)){
if(Long.parseLong(repeatInterval) < 1){
this.getAttributeUtil().setAttribute("error","重复间隔输入错误,应为正整数!");
return "simpAdd";
}
simpTrigger.setRepeatInterval(Long.parseLong(repeatInterval));
}
if(!"".equals(repeatCount)){
if(Integer.parseInt(repeatCount) < 1){
this.getAttributeUtil().setAttribute("error","重复次数输入错误,应为正整数!");
return "simpAdd";
}
simpTrigger.setRepeatCount(Integer.parseInt(repeatCount));
}
if(!"".equals(priority)){
simpTrigger.setPriority(Integer.parseInt(priority));
}
simpTrigger.setDescription(description);
scheduler.rescheduleJob(triggerName, triggerGroupName, simpTrigger);
    } catch (NumberFormatException e) {
    this.getAttributeUtil().setAttribute("error","数据格式化错误!");
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","修改任务失败,请确认修改信息!");
}
    return "simpAdd";
    }
    
    /**
     * 
     * @Title: delete
     * @author 孔庆胜 
     * @Description: 定时任务删除
     * @param triggerName
     * @param triggerGroupName
     * @return
     */
public String delete(@Read(key = "tName")String triggerName, 
            @Read(key = "tgName")String triggerGroupName) {
int state = 0;
try {
Trigger trigger = scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
return "delete";
}
if(trigger instanceof CronTrigger){
state = 1;
}else if(trigger instanceof SimpleTrigger){
state = 2;
}
scheduler.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
scheduler.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
scheduler.deleteJob(trigger.getJobName(), trigger.getJobGroup());// 删除任务
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","删除任务失败!");
}
if(state == 2){
return "simpAdd";
}
return "delete";
}


/**

* @Title: replay
* @author 孔庆胜 
* @Description: 定时任务恢复执行
* @param triggerName
* @param triggerGroupName
* @return
*/
public String replay(@Read(key = "tName")String triggerName, 
            @Read(key = "tgName")String triggerGroupName){
Trigger trigger = null;
try {
trigger = scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
return "replay";
}
scheduler.resumeTrigger(triggerName, triggerGroupName);

} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","恢复任务失败!");
}
if(trigger instanceof SimpleTrigger){
return "simpAdd";
}
return "replay";
}

/**

* @Title: pause
* @author 孔庆胜 
* @Description: 定时任务暂停
* @param triggerName
* @param triggerGroupName
* @return
*/
    public String pause(@Read(key = "tName")String triggerName, 
               @Read(key = "tgName")String triggerGroupName){
    Trigger trigger = null;
    try {
trigger = scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
return "pause";
}
scheduler.pauseTrigger(triggerName, triggerGroupName);

} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","暂停任务失败!");
}
    if(trigger instanceof SimpleTrigger){
return "simpAdd";
}
return "pause";
}
    
    /**
     * 
     * @Title: atOnceRun
     * @author 孔庆胜 
     * @Description: 定时任务手动执行一次
     * @param triggerName
     * @param triggerGroupName
     * @return
     */
    public String atOnceRun(@Read(key = "tName")String triggerName, 
                   @Read(key = "tgName")String triggerGroupName){
    Trigger trigger = null;
    try {
trigger = scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
return "pause";
}
scheduler.triggerJob(trigger.getJobName(), trigger.getJobGroup());

} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","手动执行任务失败!");
}
    if(trigger instanceof SimpleTrigger){
return "simpAdd";
}
    return "atOnce";
    }
    
    /**
     * 
     * @Title: runList
     * @author 孔庆胜 
     * @Description: 查询正在执行的任务列表
     * @return
     */
    @SuppressWarnings("unchecked")
public String runList(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
    List<CronTriggerModel> jobList = new ArrayList<CronTriggerModel>(executingJobs.size());
    for (JobExecutionContext executingJob : executingJobs) {
    CronTriggerModel job = new CronTriggerModel();
       Trigger trigger = executingJob.getTrigger();
       JobDetail jobDetail = executingJob.getJobDetail();
       job.setJobName(trigger.getJobName());
       job.setJobGroupName(trigger.getJobGroup());
       job.setTriggerName(trigger.getName());
       job.setTriggerGroupName(trigger.getGroup());
       job.setStartTime(format.format(executingJob.getFireTime()));
       job.setNextFireTime(trigger.getNextFireTime()==null?"无":format.format(trigger.getNextFireTime()));
       job.setPriority(trigger.getPriority());
       job.setDescription(trigger.getDescription());
       Class<?>[] faceGroup = (jobDetail.getJobClass()).getInterfaces();
for(int i=0; i<faceGroup.length; i++){
if((faceGroup[i].getSimpleName()).equals("InterruptableJob")){
job.setImplInte(true);
break;
}
}
       jobList.add(job);
    }
    this.getAttributeUtil().setAttribute("runList", Scope.REQUEST, jobList);
    return "runlist";
    }
    
    /**
     * @Title: interruptJob
     * @Description:中断正在执行的定时任务
     * @param triggerName
     * @param triggerGroupName
     * @return
     */
    public String interruptJob(@Read(key = "tName")String triggerName, 
                               @Read(key = "tgName")String triggerGroupName){
    try {
Trigger trigger = scheduler.getTrigger(triggerName, triggerGroupName);
if(trigger == null){
this.getAttributeUtil().setAttribute("error","定时任务没有找到!");
return "interrupt";
}
scheduler.interrupt(trigger.getJobName(), trigger.getJobGroup());
} catch (UnableToInterruptJobException e) {
this.getAttributeUtil().setAttribute("error","定时任务中断失败!\n请确定job类是否实现了InterruptableJob接口。");
} catch (SchedulerException e) {
this.getAttributeUtil().setAttribute("error","定时任务中断失败!");
}
    return "interrupt";
    }


SpringContext类代码:


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class SpringContext  implements ApplicationContextAware{

private static  ApplicationContext applicationContext;  


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  
        SpringContext.applicationContext = applicationContext;  
    }  
    
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
    
}


ConfigQuartzJobs.java类代码:


/**  
 * @Title: ConfigQuartzJobs.java
 * @Package cn.bss.quartz.bean.ConfigQuartzJobs
 * @Description: 用于获取配置文件中添加的jobs类路径
 * @author 孔庆胜   
 * @date 2015-6-26 上午9:27:10
 */
public class ConfigQuartzJobs implements Serializable{


private static final long serialVersionUID = 125634763456453L;


private List<String> jobsList;


public List<String> getJobsList() {
return jobsList;
}


public void setJobsList(List<String> jobsList) {
this.jobsList = jobsList;
}


}


CronTriggerModel 实体类,代码:


public class CronTriggerModel implements Serializable{


private static final long serialVersionUID = 134823872478353L;


private String triggerName;
private String triggerGroupName;
private String jobName;
private String jobGroupName;
private String classPath;
private String cronExpression;
private String startTime;
private String previousFireTime;
private String nextFireTime;
private int priority;
private String description;
private String state;
private boolean implInte;

}


SimpleTriggerModel实体类代码:


public class SimpleTriggerModel implements Serializable{


private static final long serialVersionUID = 134823478237482L;

private String triggerName;
private String triggerGroupName;
private String jobName;
private String jobGroupName;
private long repeatInterval;         //重复间隔
private int repeatCount;             //重复次数
private int timesTriggered;          //已执行次数
private String startTime;
private String nextFireTime;
private String endTime;
private int priority;
private String description;
private String state;
private String classPath;

}


CronQuartzJob 类代码:

/**
 * 
* @Title: CronQuartzJob.java
* @Package cn.bss.quartz.job.CronQuartzJob
* @Description: 定时任务执行类
* @author 孔庆胜   
* @date 2015-6-23 下午2:48:31
* @version V1.0
 */
public class CronQuartzJob implements Job,InterruptableJob {

Log log = LogFactory.getLog("StatQuartzLogFile");

    private volatile Thread  thisThread;
    
    public CronQuartzJob() {
    }
    
    public void execute(JobExecutionContext context){
    try {
thisThread = Thread.currentThread();
Thread.sleep(5000);
} catch (Exception e) {
log.error("定时任务执行错误,执行类:"+this.getClass().getName(), e);
}
    }


/**
* 中断定时任务
* @see org.quartz.InterruptableJob#interrupt()
*/

@Override
public void interrupt(){
if (thisThread != null) {
 thisThread.interrupt();
}

}

}


上面的两个实体,记得生成构造方法。

对于quartz的动态控制就用到上面的几个类 ,其它的就是spring的集成配置了。


quartz.properties 配置文件内容:


org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false


org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true


# Using RAMJobStore
## if using RAMJobStore, please be sure that you comment out
## org.quartz.jobStore.tablePrefix, org.quartz.jobStore.driverDelegateClass, org.quartz.jobStore.dataSource
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore


# Using JobStoreTX
## Be sure to run the appropriate script(under docs/dbTables) first to create database/tables
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX


# Configuring JDBCJobStore with the Table Prefix
org.quartz.jobStore.tablePrefix = QRTZ_


# Using DriverDelegate
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 15000


applicationContext-quartz.xml  文件内容:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName">

<bean id="springContext" class="cn.bss.quartz.util.SpringContext"></bean> 

<bean id="quScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="baseDataSource" />
<property name="overwriteExistingJobs" value="false" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:/config/quartz.properties" />
<property name="triggers">
<list></list>
</property>
</bean>
    <bean id="quartzJobs" class="cn.bss.quartz.bean.ConfigQuartzJobs">
        <property name="jobsList">
            <list>
                <value>cn.bss.quartz.job.CronQuartzJob</value>
            </list>
        </property>
    </bean>
</beans>


还有一些就是前台显示的jsp,就不贴了,有action的方法,在前台写个调用,相信都会写。


创建表的sql文件下载地址 http://download.csdn.net/detail/celins7/8858977

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值