作者:广东木叶冷 | 来源:互联网 | 2023-08-21 23:38
SpringQuartz可以采用xml形式进行job管理,但此种形式无法动态管理job。故,推荐采用DB来管理job,此种情况,可以进行动态配置job的执行时间,暂定、启动、删除job等操作。下面是
Spring Quartz可以采用xml形式进行job管理,但此种形式无法动态管理job。
故,推荐采用DB来管理job,此种情况,可以进行动态配置job的执行时间,暂定、启动、删除job等操作。
下面是核心业务类:
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import com.hafx.codesure.batchjob.bean.JobTriggerCron;
import com.hafx.codesure.batchjob.dao.QuartzDao;
import com.hafx.codesure.batchjob.dao.impl.QuartzDaoImpl;
import com.hafx.codesure.batchjob.service.QuartzOperateService;
public class QuartzOperateServiceImpl
{
public void startScheduler()
throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
}
public void stopScheduler()
throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.shutdown(false);
}
public void addJob(String jobName, String jobBean, String triggerName, String CronExp)
throws SchedulerException, ClassNotFoundException, ParseException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
// Initiate JobDetail with job name, job group, and executable job class
JobDetail jobDetail = new JobDetail(jobName, "DEFAULT", Class.forName(jobBean));
// Initiate CronTrigger with its name and group name
CronTrigger crOnTrigger= new CronTrigger(triggerName, "DEFAULT");
// setup CronExpression
CronExpression cexp = new CronExpression(CronExp);
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
// schedule a job with JobDetail and Trigger
scheduler.scheduleJob(jobDetail, cronTrigger);
// start the scheduler
//scheduler.rescheduleJob(triggerName,"DEFAULT",cronTrigger);
scheduler.start();
}
public void updateJob(String jobName, String triggerName, String CronExp)
throws SchedulerException, ClassNotFoundException, ParseException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
// Initiate CronTrigger with its name and group name
CronTrigger crOnTrigger= new CronTrigger(triggerName, "DEFAULT");
// setup CronExpression
CronExpression cexp = new CronExpression(CronExp);
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
cronTrigger.setJobName(jobName);
cronTrigger.setJobGroup("DEFAULT");
// rescheduleJob the job
scheduler.rescheduleJob(triggerName, "DEFAULT", cronTrigger);
}
public boolean deleteJob(String triggerName, String jobName)
throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
// pauseTrigger
scheduler.pauseTrigger(triggerName, "DEFAULT");
// deleteJob
scheduler.deleteJob(jobName, "DEFAULT");
// unscheduleJob
return scheduler.unscheduleJob(triggerName, "DEFAULT");
}
public void resumeJob(String triggerName, String jobName)
throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
// resumeTrigger
scheduler.resumeTrigger(triggerName, "DEFAULT");
// resumeJob
scheduler.resumeJob(jobName, "DEFAULT");
}
public void pauseJob(String triggerName, String jobName)
throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
// pauseTrigger
scheduler.pauseTrigger(triggerName, "DEFAULT");
// pauseJob
scheduler.pauseJob(jobName, Scheduler.DEFAULT_GROUP);
}
}
页面:
PS:如有需要对应工程的可以联系我。
PS:使用这种作业的JDBC存储形式,可以充分发挥quartz的集群特性。
2014.3.19 update
建表语句请到官网下载,http://www.quartz-scheduler.org/downloads。
下载后,里面的quartz-xx.xx \ docs \ dbTables 里面有各种db的建表语句。