use of org.quartz.JobDetail in project sling by apache.
the class QuartzScheduler method removeJob.
/**
* @see org.apache.sling.commons.scheduler.Scheduler#removeJob(java.lang.String)
*/
public void removeJob(final Long bundleId, final String jobName) throws NoSuchElementException {
// as this method might be called from unbind and during
// unbind a deactivate could happen, we check the scheduler first
final Map<String, SchedulerProxy> proxies;
synchronized (this.schedulers) {
if (this.active) {
proxies = new HashMap<>(this.schedulers);
} else {
proxies = Collections.emptyMap();
}
}
for (final SchedulerProxy proxy : proxies.values()) {
synchronized (proxy) {
try {
final JobKey key = JobKey.jobKey(jobName);
final JobDetail jobdetail = proxy.getScheduler().getJobDetail(key);
if (jobdetail != null) {
proxy.getScheduler().deleteJob(key);
this.logger.debug("Unscheduling job with name {}", jobName);
return;
}
} catch (final SchedulerException ignored) {
// ignore
}
}
}
if (this.active) {
throw new NoSuchElementException("No job found with name " + jobName);
}
}
use of org.quartz.JobDetail in project sling by apache.
the class QuartzJobExecutorTest method testJobExecutedWithTwoRunOnParams.
@Test
public void testJobExecutedWithTwoRunOnParams() throws SchedulerException {
Job job = new SimpleJob();
String jobName = "testName";
Map<String, Serializable> jobConfig = new HashMap<String, Serializable>();
//Adding a job just to receive a JobDetail object which is needed for testing
quartzScheduler.addJob(1L, 1L, jobName, job, jobConfig, "0 * * * * ?", true);
JobDetail jobDetail = quartzScheduler.getSchedulers().get("testName").getScheduler().getJobDetail(JobKey.jobKey(jobName));
when(executionContext.getJobDetail()).thenReturn(jobDetail);
//Job with this config should not be executed
jobDetail.getJobDataMap().put(QuartzScheduler.DATA_MAP_RUN_ON, new String[] { VALUE_RUN_ON_LEADER, VALUE_RUN_ON_SINGLE });
//In this case, when SLING_ID is equal to one of values above
//Job should be executed
QuartzJobExecutor.SLING_ID = VALUE_RUN_ON_SINGLE;
isRunnablePseudoJobCompleted = false;
jobExecutor.execute(executionContext);
assertTrue(isRunnablePseudoJobCompleted);
}
use of org.quartz.JobDetail in project deltaspike by apache.
the class AbstractQuartzScheduler method isExecutingJob.
@Override
public boolean isExecutingJob(Class<? extends T> jobClass) {
try {
JobKey jobKey = createJobKey(jobClass);
JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
if (jobDetail == null) {
return false;
}
for (JobExecutionContext jobExecutionContext : this.scheduler.getCurrentlyExecutingJobs()) {
if (jobKey.equals(jobExecutionContext.getJobDetail().getKey())) {
return true;
}
}
return false;
} catch (SchedulerException e) {
throw ExceptionUtils.throwAsRuntimeException(e);
}
}
use of org.quartz.JobDetail in project deltaspike by apache.
the class AbstractQuartzScheduler method registerNewJob.
@Override
public void registerNewJob(Class<? extends T> jobClass) {
JobKey jobKey = createJobKey(jobClass);
try {
Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
String description = scheduled.description();
if ("".equals(scheduled.description())) {
description = jobClass.getName();
}
JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
Trigger trigger;
if (jobDetail == null) {
Class<? extends Job> jobClassToAdd = createFinalJobClass(jobClass);
jobDetail = JobBuilder.newJob(jobClassToAdd).withDescription(description).withIdentity(jobKey).build();
scheduleNewJob(scheduled, jobKey, jobDetail);
} else if (scheduled.overrideOnStartup()) {
List<? extends Trigger> existingTriggers = this.scheduler.getTriggersOfJob(jobKey);
if (existingTriggers == null || existingTriggers.isEmpty()) {
scheduleNewJob(scheduled, jobKey, jobDetail);
return;
}
if (existingTriggers.size() > 1) {
throw new IllegalStateException("multiple triggers found for " + jobKey + " ('" + jobDetail + "')" + ", but aren't supported by @" + Scheduled.class.getName() + "#overrideOnStartup");
}
trigger = existingTriggers.iterator().next();
if (scheduled.cronExpression().startsWith("{") && scheduled.cronExpression().endsWith("}")) {
this.scheduler.unscheduleJobs(Arrays.asList(trigger.getKey()));
scheduleNewJob(scheduled, jobKey, jobDetail);
} else {
trigger = TriggerBuilder.newTrigger().withIdentity(trigger.getKey()).withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression())).build();
this.scheduler.rescheduleJob(trigger.getKey(), trigger);
}
} else {
Logger.getLogger(AbstractQuartzScheduler.class.getName()).info(jobKey + " exists already and will be ignored.");
}
} catch (SchedulerException e) {
throw ExceptionUtils.throwAsRuntimeException(e);
}
}
use of org.quartz.JobDetail in project engine by craftercms.
the class SchedulingUtils method createJobContext.
public static JobContext createJobContext(SiteContext siteContext, String scriptUrl, String cronExpression, ServletContext servletContext) {
String jobName = siteContext.getSiteName() + ":" + scriptUrl;
JobDetail detail = SchedulingUtils.createScriptJob(siteContext, jobName, scriptUrl, servletContext);
Trigger trigger = SchedulingUtils.createCronTrigger("trigger for " + jobName, cronExpression);
String description = "Job{url='" + scriptUrl + "', cron='" + cronExpression + "'}";
return new JobContext(detail, trigger, description);
}
Aggregations