首先需要在服务启动时,初始化调度类。mgr = ScheduleMgr.getInstance(); mgr.start();
再读取配置文件TASK.XML中的配置信息。mgr.doAfterStartService();
通过读取到的类和方法定时运行相应的方法来实现功能。
ScheduleMgr.java
public class ScheduleMgr { private static ScheduleMgr scheduleMgr = null; public static final String GLOBAL_SCHEDULE = "GLOBAL_SCHEDULE"; private static Scheduler scheduler; private List
listLisenter = new ArrayList (); /** * getInstance方法 * @return */ public static ScheduleMgr getInstance() { if (scheduleMgr == null) { scheduleMgr = new ScheduleMgr(); } return scheduleMgr; } public String getName() { return "计划任务管理"; } public void start() { try { // 实例化调度类 scheduler = StdSchedulerFactory.getDefaultScheduler(); // 注册监听类,用于处理调度通知、日志收 for (JobListener tmpJobListener : listLisenter) { scheduler.getListenerManager().addJobListener(tmpJobListener); } scheduler.clear(); scheduler.start(); ThreadUtils.setThreadVar("DEFAULT_SCHEDULE", scheduler); // 设置全局Schedule AppContextWrapper appWrapper = GlobalServletContext.getInstance() .getAppContextWrapper(); appWrapper.setAttribute(GLOBAL_SCHEDULE, scheduler); } catch (SchedulerException e1) { Debug.logError(e1, "failed initialization scheduling..."); } } public void doAfterStartService() { // 加载系统调度任务 try { // A.处理宕机延时任务 List downTimetaskList = findDownTimeScheduleTaskList(); registerTask(scheduler, downTimetaskList); // B.处理未运行系统任�? List taskList = findScheduleTaskList(); registerTask(scheduler, taskList); } catch (SchedulerException e1) { Debug.logError(e1, "failed initialization scheduling..."); } } public void createScheduleFromClassInfo(List taskList){ try { registerTask(scheduler, taskList); } catch (SchedulerException e) { Debug.logError(e, "failed initialization scheduling..."); } } public List findDownTimeScheduleTaskList() { return new ArrayList(); } public List findScheduleTaskList() { XmlParser xml = new TaskXMLParser(); Map taskMap = xml.load("task.xml"); List taskList = new ArrayList (); for (Iterator iter = taskMap.keySet().iterator(); iter.hasNext();) { String key = (String) iter.next(); taskList.add((ScheduleTask) taskMap.get(key)); } return taskList; } private void registerTask(Scheduler scheduler, List taskList) throws SchedulerException { ScheduleServiceApi api = new ScheduleServiceImpl(scheduler); for (ScheduleTask task : taskList) { api.add(task); } } public void stop() { try { if (scheduler.isStarted()) { scheduler.shutdown(); } } catch (SchedulerException e) { Debug.logError(e, this.getName() + " 卸载失败"); } } public void clear() { if(scheduler != null){ try { scheduler.clear(); } catch (SchedulerException e) { e.printStackTrace(); } } } public Scheduler getScheduler() { return scheduler; }} task.xml
1 0 3 * * ? * creatQuartzJob cn.wmc.maxplug.trbs.action.PublishTrainingAction 30 0/1 * * * ? * updateDateTime cn.wmc.maxplug.trbs.action.PublishTrainingAction public class ScheduleTask { /** 任务编码 */ private String taskCode; /** 任务名称 */ private String taskName; /** 任务类型 */ private String taskType; /** */ private String taskScript; /** */ private String[] taskParam; private TaskPlan taskPlan; private boolean isDeploy; public ScheduleTask(){ } public ScheduleTask(String taskCode, String taskName, String taskType, String taskScript, String[] taskParam) { this.taskCode = taskCode; this.taskName = taskName; this.taskType = taskType; this.taskScript = taskScript; this.taskParam = taskParam; this.isDeploy = false; } public String getTaskCode() { return taskCode; } public String getTaskName() { return taskName; } public String getTaskType() { return taskType; } public String getTaskScript() { return taskScript; } public String[] getTaskParam() { return taskParam; } public void setTaskCode(String taskCode) { this.taskCode = taskCode; } public void setTaskName(String taskName) { this.taskName = taskName; } public void setTaskType(String taskType) { this.taskType = taskType; } public void setTaskScript(String taskScript) { this.taskScript = taskScript; } public void setTaskParam(String[] taskParam) { this.taskParam = taskParam; } public TaskPlan getTaskPlan() { return taskPlan; } public void setTaskPlan(TaskPlan taskPlan) { this.taskPlan = taskPlan; } public boolean isDeploy() { return isDeploy; }}
public abstract class XmlParser { protected final FileLocator fileLocator = new FileLocator(); public Map load(String configFileName) { Map
mps = new LinkedHashMap (); try { if (CommonUtils.isNullOrEmpty(configFileName)) return mps; Document doc = buildDocument(configFileName); if (doc == null) return mps; Element root = doc.getRootElement(); parse(root, mps); } catch (Exception ex) { ex.printStackTrace(); } return mps; } protected Document buildDocument(String configFileName) { Document doc = null; try { InputStream xmlStream = getInputStream(configFileName); SAXBuilder builder = new SAXBuilder(); doc = builder.build(xmlStream); } catch (Exception e) { e.printStackTrace(); } return doc; } protected InputStream getInputStream(String configFileName) throws Exception { return fileLocator.getConfPathXmlStream(configFileName); } public abstract void parse(Element root, Map results) throws Exception;}