use of org.quartz.JobBuilder in project xwiki-platform by xwiki.
the class SchedulerPlugin method scheduleJob.
public boolean scheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException {
boolean scheduled = true;
try {
// compute the job unique Id
String xjob = getObjectUniqueId(object, context);
// Load the job class.
// Note: Remember to always use the current thread's class loader and not the container's
// (Class.forName(...)) since otherwise we will not be able to load classes installed with EM.
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
String jobClassName = object.getStringValue("jobClass");
Class<Job> jobClass = (Class<Job>) Class.forName(jobClassName, true, currentThreadClassLoader);
// Build the new job.
JobBuilder jobBuilder = JobBuilder.newJob(jobClass);
jobBuilder.withIdentity(xjob);
jobBuilder.storeDurably();
JobDataMap data = new JobDataMap();
// Let's prepare an execution context...
XWikiContext stubContext = prepareJobStubContext(object, context);
data.put("context", stubContext);
data.put("xcontext", stubContext);
data.put("xwiki", new com.xpn.xwiki.api.XWiki(context.getWiki(), stubContext));
data.put("xjob", object);
data.put("services", Utils.getComponent(ScriptServiceManager.class));
jobBuilder.setJobData(data);
getScheduler().addJob(jobBuilder.build(), true);
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(xjob);
triggerBuilder.forJob(xjob);
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(object.getStringValue("cron")));
Trigger trigger = triggerBuilder.build();
JobState status = getJobStatus(object, context);
switch(status.getQuartzState()) {
case PAUSED:
// a paused job must be resumed, not scheduled
break;
case NORMAL:
if (getTrigger(object, context).compareTo(trigger) != 0) {
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 {
saveStatus("Normal", object, context);
}
break;
default:
LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName"));
getScheduler().scheduleJob(trigger);
saveStatus("Normal", object, context);
break;
}
} catch (SchedulerException e) {
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_SCHEDULE_JOB, "Error while scheduling job " + object.getStringValue("jobName"), e);
} catch (ClassNotFoundException e) {
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while loading job class for job : " + object.getStringValue("jobName"), e);
} catch (XWikiException e) {
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while saving job status for job : " + object.getStringValue("jobName"), e);
}
return scheduled;
}
use of org.quartz.JobBuilder in project syncope by apache.
the class SchedulingPullActions method schedule.
protected <T extends Job> void schedule(final Class<T> reference, final Map<String, Object> jobMap) throws JobExecutionException {
@SuppressWarnings("unchecked") T jobInstance = (T) ApplicationContextProvider.getBeanFactory().createBean(reference, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
String jobName = getClass().getName() + UUID_GENERATOR.generate();
jobMap.put(JobManager.DOMAIN_KEY, AuthContextUtils.getDomain());
ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance);
JobBuilder jobDetailBuilder = JobBuilder.newJob(reference).withIdentity(jobName).usingJobData(new JobDataMap(jobMap));
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(JobNamer.getTriggerName(jobName)).startNow();
try {
scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build());
} catch (SchedulerException e) {
throw new JobExecutionException("Could not schedule, aborting", e);
}
}
use of org.quartz.JobBuilder in project syncope by apache.
the class JobManagerImpl method registerJob.
private void registerJob(final String jobName, final Job jobInstance, final String cronExpression, final Date startAt, final Map<String, Object> jobMap) throws SchedulerException {
if (isRunning(new JobKey(jobName, Scheduler.DEFAULT_GROUP))) {
LOG.debug("Job {} already running, cancel", jobName);
return;
}
// 0. unregister job
unregisterJob(jobName);
// 1. Job bean
ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance);
// 2. JobDetail bean
JobBuilder jobDetailBuilder = JobBuilder.newJob(jobInstance.getClass()).withIdentity(jobName).usingJobData(new JobDataMap(jobMap));
// 3. Trigger
if (cronExpression == null && startAt == null) {
// Jobs added with no trigger must be durable
scheduler.getScheduler().addJob(jobDetailBuilder.storeDurably().build(), true);
} else {
TriggerBuilder<?> triggerBuilder;
if (cronExpression == null) {
triggerBuilder = TriggerBuilder.newTrigger().withIdentity(JobNamer.getTriggerName(jobName)).startAt(startAt);
} else {
triggerBuilder = TriggerBuilder.newTrigger().withIdentity(JobNamer.getTriggerName(jobName)).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
if (startAt == null) {
triggerBuilder = triggerBuilder.startNow();
} else {
triggerBuilder = triggerBuilder.startAt(startAt);
}
}
scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build());
}
}
use of org.quartz.JobBuilder in project serverless by bluenimble.
the class SchedulerPlugin method startJobs.
private void startJobs(ApiSpace space) throws PluginRegistryException {
JsonObject schedulerFeature = Json.getObject(space.getFeatures(), feature);
if (schedulerFeature == null || schedulerFeature.isEmpty()) {
return;
}
Iterator<String> keys = schedulerFeature.keys();
while (keys.hasNext()) {
String key = keys.next();
JsonObject source = Json.getObject(schedulerFeature, key);
if (!this.getName().equalsIgnoreCase(Json.getString(source, ApiSpace.Features.Provider))) {
continue;
}
JsonObject spec = Json.getObject(source, ApiSpace.Features.Spec);
if (spec == null) {
continue;
}
String expression = Json.getString(source, Spec.Expression);
if (Lang.isNullOrEmpty(expression)) {
continue;
}
String api = Json.getString(source, Spec.Api);
if (Lang.isNullOrEmpty(api)) {
continue;
}
String service = Json.getString(source, Spec.Service);
if (Lang.isNullOrEmpty(service)) {
continue;
}
String id = space.getNamespace() + Lang.DASH + key;
JobBuilder builder = newJob(ServiceJob.class).withIdentity(Prefix.Job + id, Prefix.Group + id);
builder.usingJobData(_Space, space.getNamespace());
builder.usingJobData(_Api, api);
builder.usingJobData(_Service, service);
JsonObject data = Json.getObject(spec, Spec.Data);
if (!Json.isNullOrEmpty(data)) {
Iterator<String> dataKeys = data.keys();
while (dataKeys.hasNext()) {
String dataKey = dataKeys.next();
builder.usingJobData(dataKey, String.valueOf(data.get(dataKey)));
}
}
JobDetail job = builder.build();
String timeZone = Json.getString(source, Spec.TimeZone);
CronScheduleBuilder csb = cronSchedule(Json.getString(source, Spec.Expression));
if (!Lang.isNullOrEmpty(timeZone)) {
csb.inTimeZone(TimeZone.getTimeZone(timeZone));
}
Trigger trigger = newTrigger().withIdentity(Prefix.Trigger + id, Prefix.Group + id).withSchedule(csb).forJob(job.getKey()).build();
try {
oScheduler.scheduleJob(trigger);
} catch (SchedulerException e) {
throw new PluginRegistryException(e.getMessage(), e);
}
}
}
use of org.quartz.JobBuilder in project kernel by exoplatform.
the class JobSchedulerServiceImpl method addCronJob.
public void addCronJob(JobInfo jinfo, String exp, JobDataMap jdatamap) throws Exception {
JobInfo jobinfo = getJobInfo(jinfo);
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).forJob(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(CronScheduleBuilder.cronSchedule(exp)).build();
@SuppressWarnings("unchecked") JobBuilder jb = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription());
JobDetail job = jdatamap == null ? jb.build() : jb.usingJobData(jdatamap).build();
scheduleJob(job, trigger);
}
Aggregations