spring Quartz 操作 以及 动态加载到内存中

 

 

 

package com.nantian.ecm.cebecm.ecmdownloadcenter.quartz;

 

import java.text.ParseException;

import java.util.*;

import org.apache.log4j.Logger;

import org.quartz.*;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

 

/**

 * Quartz Job 管理类

 * 

 * 封装了使用到的各种Job操作

 * 

 * @author ldd

 * 

 */

public class QuartzJobManager implements ApplicationContextAware{

private Logger logger = Logger.getLogger(getClass());

private static final String SCHEDULER_FACTORY_NAME = "schedulerFactory";

private static final int STATE_ERROR = -2;

private Scheduler scheduler;

 

 

 

 

 

 

/**

* 获取全部任务名称列表

* @return Job名称List

*/

public String[] readJobs() {

try {

String[] jobs = scheduler.getTriggerNames(scheduler.DEFAULT_GROUP);

return jobs;

} catch (SchedulerException e) {

logger.error("获取任务列表时出错", e);

return null;

}

}

 

/**

* 获取全部任务状态

* @return 关键字为Job名称,值为任务状态

*/

public Map readJobsState() {

String[] jobs = readJobs();

if (jobs != null) {

Map jobsMap = new HashMap();

for (int i = 0; i < jobs.length; i++) {

String job = jobs[i];

int jobState = readjobState(job);

jobsMap.put(job, jobState);

}

return jobsMap;

} else {

return null;

}

}

 

/**

* 获取Job状态

* @param jobName

*            Job名称

* @return job状态: state的值代表该任务触发器的状态:

*         STATE_BLOCKED 4 STATE_COMPLETE 2 STATE_ERROR 3 STATE_NONE -1

*         STATE_NORMAL 0 STATE_PAUSED 1

*/

public int readjobState(String jobName) {

int triggerState = STATE_ERROR;

try {

triggerState = scheduler.getTriggerState(jobName,

scheduler.DEFAULT_GROUP);

} catch (SchedulerException e) {

logger.error("获取任务状态=" + jobName + " 出错!", e);

} finally {

 

}

 

return triggerState;

}

 

/**

* 暂停Job

* @param jobName

*            job名称

* @return 暂停后的job状态

*/

public int pauseJob(String jobName) {

int triggerState = STATE_ERROR;

try {

scheduler.pauseTrigger(jobName, scheduler.DEFAULT_GROUP);

triggerState = scheduler.getTriggerState(jobName,

scheduler.DEFAULT_GROUP);

} catch (SchedulerException e) {

logger.error("暂停任务=" + jobName + " 出错!", e);

} finally {

 

}

 

return triggerState;

}

 

/**

* 启用Job

* @param jobName

*            job名称

* @return 启用后的job 状态

*/

public int resumeJob(String jobName) {

int triggerState = STATE_ERROR;

try {

scheduler.resumeTrigger(jobName, scheduler.DEFAULT_GROUP);

triggerState = scheduler.getTriggerState(jobName,

scheduler.DEFAULT_GROUP);

} catch (SchedulerException e) {

logger.error("恢复任务=" + jobName + " 出错!", e);

} finally {

 

}

 

return triggerState;

}

     

 

 

 

/**

* 新增任务

* @param jobName

* @param job

* @param time

* @throws SchedulerException

* @throws ParseException

*/

public  void addJob(String jobName, Job job, String time)

throws SchedulerException, ParseException {

 

JobDetail jobDetail = new JobDetail(jobName, scheduler.DEFAULT_GROUP, job

.getClass());

CronTrigger trigger = new CronTrigger(jobName, scheduler.DEFAULT_MANUAL_TRIGGERS);

trigger.setCronExpression(time);

scheduler.scheduleJob(jobDetail, trigger);

 

if (!scheduler.isShutdown())

scheduler.start();

}

 

/**

* 更新任务时间

* @param jobName

* @param time

* @throws SchedulerException

* @throws ParseException

*/

public  void modifyJobTime(String jobName, String time)

throws SchedulerException, ParseException {

Trigger trigger = scheduler.getTrigger(jobName, scheduler.DEFAULT_MANUAL_TRIGGERS);

if (trigger != null) {

CronTrigger ct = (CronTrigger) trigger;

ct.setCronExpression(time);

scheduler.resumeTrigger(jobName, scheduler.DEFAULT_MANUAL_TRIGGERS);

}

}

    

/**

* 使用默认触发器名称

* @param jobName

* @throws SchedulerException

*/

public  void removeJob(String jobName) throws SchedulerException {

 

scheduler.pauseTrigger(jobName, scheduler.DEFAULT_MANUAL_TRIGGERS);// 停止触发器

scheduler.unscheduleJob(jobName, scheduler.DEFAULT_MANUAL_TRIGGERS);// 移除触发器

scheduler.deleteJob(jobName, scheduler.DEFAULT_GROUP);// 删除任务

}

 

 

/**

* 重新设置Trigger表达式

* @param jobName

* @param cronExpression

*/

public void updateTrigger(String jobName, String cronExpression) {

try {

CronTrigger t = (CronTrigger) scheduler.getTrigger(jobName,

scheduler.DEFAULT_GROUP);

 

try {

t.setCronExpression(cronExpression);

logger.info("Trigger=" + jobName + ",from ["

+ t.getCronExpression() + "] change to ["

+ cronExpression + "]");

} catch (ParseException e) {

logger.error("设置Trigger=" + jobName + " cronExpression="

+ cronExpression + " 出错!", e);

}

} catch (SchedulerException e) {

logger.error("获取Trigger=" + jobName + " 出错!", e);

}

 

}

 

@Override

public void setApplicationContext(ApplicationContext ctx)

throws BeansException {

scheduler = (org.quartz.impl.StdScheduler) ctx

.getBean(SCHEDULER_FACTORY_NAME);

}

 

}

 

你可能感兴趣的:(spring)