use of org.quartz.JobDetail in project camel by apache.
the class QuartzEndpoint method addJobInScheduler.
private void addJobInScheduler() throws Exception {
// Add or use existing trigger to/from scheduler
Scheduler scheduler = getComponent().getScheduler();
JobDetail jobDetail;
Trigger oldTrigger = scheduler.getTrigger(triggerKey);
boolean triggerExisted = oldTrigger != null;
if (triggerExisted && !isRecoverableJob()) {
ensureNoDupTriggerKey();
}
jobDetail = createJobDetail();
Trigger trigger = createTrigger(jobDetail);
QuartzHelper.updateJobDataMap(getCamelContext(), jobDetail, getEndpointUri(), isUsingFixedCamelContextName());
if (triggerExisted) {
// Reschedule job if trigger settings were changed
if (hasTriggerChanged(oldTrigger, trigger)) {
scheduler.rescheduleJob(triggerKey, trigger);
}
} else {
try {
// Schedule it now. Remember that scheduler might not be started it, but we can schedule now.
scheduler.scheduleJob(jobDetail, trigger);
} catch (ObjectAlreadyExistsException ex) {
// some other VM might may have stored the job & trigger in DB in clustered mode, in the mean time
if (!(getComponent().isClustered())) {
throw ex;
} else {
trigger = scheduler.getTrigger(triggerKey);
if (trigger == null) {
throw new SchedulerException("Trigger could not be found in quartz scheduler.");
}
}
}
}
if (LOG.isInfoEnabled()) {
LOG.info("Job {} (triggerType={}, jobClass={}) is scheduled. Next fire date is {}", new Object[] { trigger.getKey(), trigger.getClass().getSimpleName(), jobDetail.getJobClass().getSimpleName(), trigger.getNextFireTime() });
}
// Increase camel job count for this endpoint
AtomicInteger number = (AtomicInteger) scheduler.getContext().get(QuartzConstants.QUARTZ_CAMEL_JOBS_COUNT);
if (number != null) {
number.incrementAndGet();
}
jobAdded.set(true);
}
use of org.quartz.JobDetail in project camel by apache.
the class QuartzEndpoint method createJobDetail.
private JobDetail createJobDetail() throws Exception {
// Camel endpoint timer will assume one to one for JobDetail and Trigger, so let's use same name as trigger
String name = triggerKey.getName();
String group = triggerKey.getGroup();
Class<? extends Job> jobClass = stateful ? StatefulCamelJob.class : CamelJob.class;
LOG.debug("Creating new {}.", jobClass.getSimpleName());
JobBuilder builder = JobBuilder.newJob(jobClass).withIdentity(name, group);
if (durableJob) {
builder = builder.storeDurably();
}
if (recoverableJob) {
builder = builder.requestRecovery();
}
JobDetail result = builder.build();
// Let user parameters to further set JobDetail properties.
if (jobParameters != null && jobParameters.size() > 0) {
LOG.debug("Setting user extra jobParameters {}", jobParameters);
setProperties(result, jobParameters);
}
LOG.debug("Created jobDetail={}", result);
return result;
}
use of org.quartz.JobDetail in project camel by apache.
the class QuartzEndpointConfigureTest method testConfigureName.
@Test
public void testConfigureName() throws Exception {
QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myName");
Scheduler scheduler = endpoint.getComponent().getScheduler();
TriggerKey triggerKey = endpoint.getTriggerKey();
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
assertEquals("getName()", "myName", triggerKey.getName());
assertEquals("getGroup()", "Camel_" + context.getManagementName(), triggerKey.getGroup());
assertEquals("getJobName", "myName", jobDetail.getKey().getName());
assertEquals("getJobGroup", "Camel_" + context.getManagementName(), jobDetail.getKey().getGroup());
}
use of org.quartz.JobDetail in project camel by apache.
the class QuartzEndpointConfigureTest method testConfigureAnotherCronExpression.
@Test
public void testConfigureAnotherCronExpression() throws Exception {
QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myGroup/myTimerName?cron=0+0+*+*+*+?");
Scheduler scheduler = endpoint.getComponent().getScheduler();
TriggerKey triggerKey = endpoint.getTriggerKey();
Trigger trigger = scheduler.getTrigger(triggerKey);
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
assertEquals("getName()", "myTimerName", triggerKey.getName());
assertEquals("getGroup()", "myGroup", triggerKey.getGroup());
assertEquals("getJobName", "myTimerName", jobDetail.getKey().getName());
assertEquals("getJobGroup", "myGroup", jobDetail.getKey().getGroup());
assertIsInstanceOf(CronTrigger.class, trigger);
CronTrigger cronTrigger = (CronTrigger) trigger;
assertEquals("cron expression", "0 0 * * * ?", cronTrigger.getCronExpression());
}
use of org.quartz.JobDetail in project camel by apache.
the class ScheduledRoutePolicy method scheduleRoute.
public void scheduleRoute(Action action, Route route) throws Exception {
JobDetail jobDetail = createJobDetail(action, route);
Trigger trigger = createTrigger(action, route);
updateScheduledRouteDetails(action, jobDetail, trigger, route);
loadCallbackDataIntoSchedulerContext(jobDetail, action, route);
boolean isClustered = route.getRouteContext().getCamelContext().getComponent("quartz2", QuartzComponent.class).isClustered();
if (isClustered) {
// check to see if the same job has already been setup through another node of the cluster
JobDetail existingJobDetail = getScheduler().getJobDetail(jobDetail.getKey());
if (jobDetail.equals(existingJobDetail)) {
if (LOG.isInfoEnabled()) {
LOG.info("Skipping to schedule the job: {} for action: {} on route {} as the job: {} already existing inside the cluster", new Object[] { jobDetail.getKey(), action, route.getId(), existingJobDetail.getKey() });
}
// skip scheduling the same job again as one is already existing for the same routeId and action
return;
}
}
getScheduler().scheduleJob(jobDetail, trigger);
if (LOG.isInfoEnabled()) {
LOG.info("Scheduled trigger: {} for action: {} on route {}", new Object[] { trigger.getKey(), action, route.getId() });
}
}
Aggregations