热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringQuartz基于数据库管理Job

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的建表语句。



推荐阅读
author-avatar
广东木叶冷
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有