Quartz任务调度、手动执行定时任务

来源:互联网 发布:华润网络校园招聘 编辑:程序博客网 时间:2024/04/27 00:22

页面点击触发定时任务,基于spring,struts.

1、请求接到后台,配置任务属性,添加到任务管理工具中

@Autowiredprivate SyncTaskJob job;@Overridepublic int triggerTask(TaskInfo ti) {String taskId = String.valueOf(ti.getId());    String job_name =ti.getName();  //动态任务调度    String date = ti.getSchedule();String job_group_name = "group"+ti.getName();  //任务组名String trigger_group_name = "triggerGroup"+ti.getName(); //触发器组名   String triggerName = "trigger"+ti.getName();int result = 0;    try {      result = QuartzManager.addTaskJob(job_name, job_group_name,    triggerName, trigger_group_name, job, date, taskId);    }  catch (Exception e) {      e.printStackTrace();      result = -1;    } return result;  }
2、任务管理工具
import org.quartz.CronTrigger;  import org.quartz.Job;import org.quartz.JobDetail;  import org.quartz.Scheduler;  import org.quartz.SchedulerException;  import org.quartz.SchedulerFactory;  import org.quartz.impl.StdSchedulerFactory;//任务管理类  public class QuartzManager {private static SchedulerFactory sf = new StdSchedulerFactory();     /** *//**     * 添加一个定时任务     * @param jobName 任务名     * @param jobGroupName 任务组名     * @param triggerName  触发器名     * @param triggerGroupName 触发器组名     * @param job     任务     * @param time    时间设置,参考quartz说明文档     * @throws SchedulerException     * @throws ParseException     */     public static int addTaskJob(String jobName,String jobGroupName,                               String triggerName,String triggerGroupName,                               Job job,String time, String taskId)                               throws SchedulerException, ParseException{     Scheduler sched = sf.getScheduler();     try{    String[] jobGroupNames = sched.getJobGroupNames();   if(null != jobGroupNames){   for(int i =0;i<jobGroupNames.length ; i++){   if(jobGroupNames[i].equals(jobGroupName)){   int state = sched.getTriggerState(triggerName, triggerGroupName);   if(4 == state){              return 1;       }else{      // time = "0/30 * * * * ?";       removeJob(jobName,jobGroupName,         triggerName,triggerGroupName);        add(sched,jobName,jobGroupName,triggerName,triggerGroupName,job,time, taskId);       return 0;       }       }   }    }   add(sched,jobName,jobGroupName,triggerName,triggerGroupName,job,time, taskId);   }catch(Exception e){   e.printStackTrace();   return -1;   }   return 0;   }     public static void add(Scheduler sched ,String jobName,String jobGroupName,                 String triggerName,String triggerGroupName,                 Job job,String time, String taskId){   try{   JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任务名,任务组,任务执行类   jobDetail.getJobDataMap().put("taskId", taskId);   //触发器CronTrigger  trigger =     new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组  trigger.setCronExpression(time);//触发器时间设定  sched.scheduleJob(jobDetail,trigger);if(!sched.isShutdown())  sched.start();     }catch(Exception e){   e.printStackTrace();   }}      /** *//**     * 移除一个任务     * @param jobName     * @param jobGroupName     * @param triggerName     * @param triggerGroupName     * @throws SchedulerException     */     public static void removeJob(String jobName,String jobGroupName,                                  String triggerName,String triggerGroupName)                                  throws SchedulerException{         Scheduler sched = sf.getScheduler();         sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器         sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器         sched.deleteJob(jobName,jobGroupName);//删除任务     }  }
3、job管理类

@Component()public  class SyncTaskJob implements Job{@Autowiredprivate SyncService syncService;@Autowiredprivate ExecutionLogService executionLogService;public void  execute(JobExecutionContext jobExecutionContext) {boolean isSuccess = false;Date start = null ;Date end = null;try {SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);//用于解决job类中不能用注解问题start = new Date();syncService.syncVms();end = new Date();isSuccess = true;}catch (Exception e){System.out.print(e.getMessage());isSuccess = false;}finally {String taskId = jobExecutionContext.getJobDetail().getJobDataMap().getString("taskId");executionLogService.writeLog(taskId, isSuccess, start,  end);}}}

原创粉丝点击