use of org.quartz.JobKey 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.JobKey in project deltaspike by apache.
the class AbstractQuartzScheduler method createJobKey.
private JobKey createJobKey(Class<?> jobClass) {
Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
if (scheduled == null) {
throw new IllegalStateException("@" + Scheduled.class.getName() + " is missing on " + jobClass.getName());
}
String groupName = scheduled.group().getSimpleName();
String jobName = getJobName(jobClass);
if (!Scheduled.class.getSimpleName().equals(groupName)) {
return new JobKey(jobName, groupName);
}
return new JobKey(jobName);
}
use of org.quartz.JobKey in project karaf by apache.
the class QuartzScheduler method schedule.
/**
* Schedule a job
* @see org.apache.karaf.scheduler.Scheduler#schedule(java.lang.Object, org.apache.karaf.scheduler.ScheduleOptions)
* @throws SchedulerException if the job can't be scheduled
* @throws IllegalArgumentException If the preconditions are not met
*/
public void schedule(final Object job, final ScheduleOptions options) throws IllegalArgumentException, SchedulerException {
this.checkJob(job);
if (!(options instanceof InternalScheduleOptions)) {
throw new IllegalArgumentException("Options has not been created via schedule or is null.");
}
final InternalScheduleOptions opts = (InternalScheduleOptions) options;
if (opts.argumentException != null) {
throw opts.argumentException;
}
// as this method might be called from unbind and during
// unbind a deactivate could happen, we check the scheduler first
final org.quartz.Scheduler s = this.scheduler;
if (s == null) {
throw new IllegalStateException("Scheduler is not available anymore.");
}
final String name;
if (opts.name != null) {
// if there is already a job with the name, remove it first
try {
final JobKey key = JobKey.jobKey(opts.name);
final JobDetail jobdetail = s.getJobDetail(key);
if (jobdetail != null) {
s.deleteJob(key);
this.logger.debug("Unscheduling job with name {}", opts.name);
}
} catch (final SchedulerException ignored) {
// ignore
}
name = opts.name;
} else {
name = job.getClass().getName() + ':' + UUID.randomUUID();
opts.name = name;
}
final Trigger trigger = opts.trigger.withIdentity(name).build();
// create the data map
final JobDataMap jobDataMap = this.initDataMap(name, job, opts);
final JobDetail detail = this.createJobDetail(name, jobDataMap, opts.canRunConcurrently);
this.logger.debug("Scheduling job {} with name {} and trigger {}", job, name, trigger);
s.scheduleJob(detail, trigger);
}
use of org.quartz.JobKey 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.JobKey in project sling by apache.
the class WhiteboardHandlerTest method testUnregisterService.
@Test
public void testUnregisterService() throws SchedulerException {
Thread service = new Thread();
String schedulerName = "testScheduler";
Long period = 1L;
Integer times = 2;
Dictionary<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_RUN_ON, Scheduler.VALUE_RUN_ON_LEADER);
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_EXPRESSION, "0 * * * * ?");
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_CONCURRENT, Boolean.FALSE);
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_IMMEDIATE, Boolean.FALSE);
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_NAME, schedulerName);
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_PERIOD, period);
serviceProps.put(Scheduler.PROPERTY_SCHEDULER_TIMES, times);
serviceProps.put(Constants.SERVICE_PID, "1");
serviceProps.put(Constants.SERVICE_ID, 1L);
//Register service only to get a reference of it.
//This reference is needed to test our method.
final ServiceRegistration<?> reg = context.registerService(Runnable.class.getName(), service, serviceProps);
ServiceReference<?> reference = reg.getReference();
handler.register(reference, service);
JobKey jobKey = JobKey.jobKey(schedulerName + "." + reference.getProperty(Constants.SERVICE_ID));
assertNotNull(quartzScheduler.getSchedulers().get("testName").getScheduler().getJobDetail(jobKey));
reg.unregister();
handler.unregister(reference);
assertNull(quartzScheduler.getSchedulers().get("testName").getScheduler().getJobDetail(jobKey));
}
Aggregations