private void onWikiDeletedEvent(String wikiId) throws SchedulerException { Set<JobKey> keys = getScheduler().getJobKeys(GroupMatcher.anyJobGroup()); String idPrefix = getWikiIdPrefix(wikiId); for (JobKey key : keys) { if (key.getName().startsWith(idPrefix)) { getScheduler().deleteJob(key); } } }
/** * Associates the scheduler with a StatusListener * * @throws SchedulerPluginException if the status listener failed to be set properly */ private void setStatusListener() throws SchedulerPluginException { StatusListener listener = new StatusListener(); try { getScheduler().addSchedulerListener(listener); getScheduler().addGlobalJobListener(listener); } catch (SchedulerException e) { throw new SchedulerPluginException( SchedulerPluginException.ERROR_SCHEDULERPLUGIN_INITIALIZE_STATUS_LISTENER, "Error while initializing the status listener", e); } }
/** * Retrieve the job's status of a given {@link com.xpn.xwiki.plugin.scheduler.SchedulerPlugin#XWIKI_JOB_CLASS} job * XObject, by asking the actual job status to the quartz scheduler instance. It's the actual status, as the one * stored in the XObject may be changed manually by users. * * @param object the XObject to give the status of * @return the status of the Job inside the quartz scheduler, as {@link com.xpn.xwiki.plugin.scheduler.JobState} * instance */ public JobState getJobStatus(BaseObject object, XWikiContext context) throws SchedulerException { int state = getScheduler().getTriggerState(getObjectUniqueId(object, context), Scheduler.DEFAULT_GROUP); return new JobState(state); }
/** * Associates the scheduler with a StatusListener * * @throws SchedulerPluginException if the status listener failed to be set properly */ private void setStatusListener() throws SchedulerPluginException { StatusListener listener = new StatusListener(); try { getScheduler().getListenerManager().addSchedulerListener(listener); getScheduler().getListenerManager().addJobListener(listener); } catch (SchedulerException e) { throw new SchedulerPluginException( SchedulerPluginException.ERROR_SCHEDULERPLUGIN_INITIALIZE_STATUS_LISTENER, "Error while initializing the status listener", e); } }
/** * Retrieve the job's status of a given {@link com.xpn.xwiki.plugin.scheduler.SchedulerPlugin#XWIKI_JOB_CLASS} job * XObject, by asking the actual job status to the quartz scheduler instance. It's the actual status, as the one * stored in the XObject may be changed manually by users. * * @param object the XObject to give the status of * @return the status of the Job inside the quartz scheduler, as {@link com.xpn.xwiki.plugin.scheduler.JobState} * instance */ public JobState getJobStatus(BaseObject object, XWikiContext context) throws SchedulerException { TriggerState state = getScheduler().getTriggerState(new TriggerKey(getObjectUniqueId(object, context))); return new JobState(state); }
/** * Trigger a job (execute it now) * * @param object the non-wrapped XObject Job to be triggered * @param context the XWiki context */ public void triggerJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { try { getScheduler().triggerJob(getObjectUniqueId(object, context), Scheduler.DEFAULT_GROUP); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_TRIGGER_JOB, "Error occured while trying to trigger job " + object.getStringValue("jobName"), e); } }
/** * Get Trigger object of the given job * * @param object the unwrapped XObject to be retrieve the trigger for * @param context the XWiki context * @return the trigger object of the given job */ private Trigger getTrigger(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); Trigger trigger; try { trigger = getScheduler().getTrigger(job, Scheduler.DEFAULT_GROUP); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while getting trigger for job " + job, e); } if (trigger == null) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_DOES_NOT_EXITS, "Job does not exists"); } return trigger; }
/** * Trigger a job (execute it now) * * @param object the non-wrapped XObject Job to be triggered * @param context the XWiki context */ public void triggerJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); try { getScheduler().triggerJob(new JobKey(job)); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_TRIGGER_JOB, "Error occured while trying to trigger job " + object.getStringValue("jobName"), e); } }
getScheduler().addJob(jobBuilder.build(), true); LOGGER.debug("Reschedule Job: [{}]", object.getStringValue("jobName")); getScheduler().rescheduleJob(trigger.getKey(), trigger); break; case NONE: LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName")); getScheduler().scheduleJob(trigger); LOGGER.info("XWiki Job Status: [{}]", object.getStringValue("status")); if (object.getStringValue("status").equals("Paused")) { getScheduler().pauseJob(new JobKey(xjob)); saveStatus("Paused", object, context); } else { default: LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName")); getScheduler().scheduleJob(trigger); saveStatus("Normal", object, context); break;
/** * Get Trigger object of the given job * * @param object the unwrapped XObject to be retrieve the trigger for * @param context the XWiki context * @return the trigger object of the given job */ private Trigger getTrigger(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); Trigger trigger; try { trigger = getScheduler().getTrigger(new TriggerKey(job)); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while getting trigger for job " + job, e); } if (trigger == null) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_DOES_NOT_EXITS, "Job does not exists"); } return trigger; }
getScheduler().addJob(job, true); LOG.debug("Reschedule Job : " + object.getStringValue("jobName")); getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger); break; case Trigger.STATE_NONE: LOG.debug("Schedule Job : " + object.getStringValue("jobName")); getScheduler().scheduleJob(trigger); LOG.info("XWiki Job Status :" + object.getStringValue("status")); if (object.getStringValue("status").equals("Paused")) { getScheduler().pauseJob(xjob, Scheduler.DEFAULT_GROUP); saveStatus("Paused", object, context); } else { default: LOG.debug("Schedule Job : " + object.getStringValue("jobName")); getScheduler().scheduleJob(trigger); saveStatus("Normal", object, context); break;
/** * Resume the job with the given name (un-pause) * * @param object the non-wrapped XObject Job to be resumed */ public void resumeJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { try { getScheduler().resumeJob(getObjectUniqueId(object, context), Scheduler.DEFAULT_GROUP); saveStatus("Normal", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, "Error occured while trying to resume job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, "Error occured while trying to save status of job " + object.getStringValue("jobName"), e); } }
/** * Unschedule the given job * * @param object the unwrapped XObject job to be unscheduled */ public void unscheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { try { getScheduler().deleteJob(getObjectUniqueId(object, context), Scheduler.DEFAULT_GROUP); saveStatus("None", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while unscheduling job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while saving status of job " + object.getStringValue("jobName"), e); } }
/** * Pause the job with the given name by pausing all of its current triggers. * * @param object the non-wrapped XObject Job to be paused */ public void pauseJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { try { getScheduler().pauseJob(getObjectUniqueId(object, context), Scheduler.DEFAULT_GROUP); saveStatus("Paused", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, "Error occured while trying to pause job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, "Error occured while trying to save status of job " + object.getStringValue("jobName"), e); } }
/** * Unschedule the given job * * @param object the unwrapped XObject job to be unscheduled */ public void unscheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); try { getScheduler().deleteJob(new JobKey(job)); saveStatus("None", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while unscheduling job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while saving status of job " + object.getStringValue("jobName"), e); } }
/** * Pause the job with the given name by pausing all of its current triggers. * * @param object the non-wrapped XObject Job to be paused */ public void pauseJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); try { getScheduler().pauseJob(new JobKey(job)); saveStatus("Paused", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, "Error occured while trying to pause job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_PAUSE_JOB, "Error occured while trying to save status of job " + object.getStringValue("jobName"), e); } }
/** * Resume the job with the given name (un-pause) * * @param object the non-wrapped XObject Job to be resumed */ public void resumeJob(BaseObject object, XWikiContext context) throws SchedulerPluginException { String job = getObjectUniqueId(object, context); try { getScheduler().resumeJob(new JobKey(job)); saveStatus("Normal", object, context); } catch (SchedulerException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, "Error occured while trying to resume job " + object.getStringValue("jobName"), e); } catch (XWikiException e) { throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_RESUME_JOB, "Error occured while trying to save status of job " + object.getStringValue("jobName"), e); } }