package com.unis.uvm.quartz; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import com.unis.uvm.utility.LogUtil; /** * To create scheduler job * * @author xpz * @version [version, Jul 18, 2015] * @see [about class/method] * @since [product/module version] */ public class ScheduleJobFactory { private static Log log = LogFactory.getLog(ScheduleJobFactory.class); /*** * create a simpleScheduler job * * @param scheduler: the scheduler * @param jobName: the job name * @param jobGroup: the job group * @param triggerName: the trigger name * @param triggerGroup: the trigger group * @param ssb: the schedBuilder (eg:SimpleScheduleBuilder ssb = * SimpleScheduleBuilder.simpleSchedule() .withIntervalInMinutes(1) * .repeatForever();) * @param jobClass: the class which implement * job/StatefulJob(@DisallowConcurrentExecution) * @param params [explain parameter] * @return void [explain return type] * @exception throws [exception type] [explain exception] * @see [class,class#method,class#member] */ public static void createSimpleJob(Scheduler scheduler, String jobName, String jobGroup, String triggerName, String triggerGroup, SimpleScheduleBuilder ssb, Class<? extends Job> jobClass, String[] params) { // build a schedule job JobDetail job = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroup) .build(); // set job params if (params != null && params.length > 0) { for (String param : params) { job.getJobDataMap().put(param, param); } } // build a schedule simpleTrigger Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(triggerName, triggerGroup) .startNow() .withSchedule(ssb) .build(); // create a new schedule job try { scheduler.scheduleJob(job, trigger); } catch (SchedulerException e) { LogUtil.logError(log, e, "error with create a simple schedule job!"); } } /*** * create a cronScheduler job * * @param scheduler: the scheduler * @param jobName: the job name * @param jobGroup: the job group * @param triggerName: the trigger name * @param triggerGroup: the trigger group * @param cronExpression: the cron expression * @param jobClass: the class which implements * job/StatefulJob(@DisallowConcurrentExecution) * @param params: the param need to trransfer * @return void [explain return type] * @exception throws [exception type] [explain exception] * @see [class,class#method,class#member] */ public static void createCronJob(Scheduler scheduler, String jobName, String jobGroup, String triggerName, String triggerGroup, String cronExpression, Class<? extends Job> jobClass, String[] params) { // build a schedule job JobDetail job = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroup) .build(); // set job params if (params != null && params.length > 0) { for (String param : params) { job.getJobDataMap().put(param, param); } } // build a schedule cronTrigger CronTrigger trigger = TriggerBuilder.newTrigger() .withIdentity(triggerName, triggerGroup) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) .build(); // create a new schedule job try { scheduler.scheduleJob(job, trigger); } catch (SchedulerException e) { LogUtil.logError(log, e, "error with create a cron schedule job!"); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 14:50:12