use of org.quartz.Trigger in project camel by apache.
the class CamelJob method execute.
public void execute(JobExecutionContext context) throws JobExecutionException {
String camelContextName = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME);
String endpointUri = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT_URI);
SchedulerContext schedulerContext;
try {
schedulerContext = context.getScheduler().getContext();
} catch (SchedulerException e) {
throw new JobExecutionException("Failed to obtain scheduler context for job " + context.getJobDetail().getName());
}
CamelContext camelContext = (CamelContext) schedulerContext.get(QuartzConstants.QUARTZ_CAMEL_CONTEXT + "-" + camelContextName);
if (camelContext == null) {
throw new JobExecutionException("No CamelContext could be found with name: " + camelContextName);
}
Trigger trigger = context.getTrigger();
QuartzEndpoint endpoint = lookupQuartzEndpoint(camelContext, endpointUri, trigger);
if (endpoint == null) {
throw new JobExecutionException("No QuartzEndpoint could be found with endpointUri: " + endpointUri);
}
endpoint.onJobExecute(context);
}
use of org.quartz.Trigger in project camel by apache.
the class QuartzMessage method populateInitialHeaders.
@Override
protected void populateInitialHeaders(Map<String, Object> map) {
super.populateInitialHeaders(map);
if (jobExecutionContext != null) {
map.put("calendar", jobExecutionContext.getCalendar());
map.put("fireTime", jobExecutionContext.getFireTime());
map.put("jobDetail", jobExecutionContext.getJobDetail());
map.put("jobInstance", jobExecutionContext.getJobInstance());
map.put("jobRunTime", jobExecutionContext.getJobRunTime());
map.put("mergedJobDataMap", jobExecutionContext.getMergedJobDataMap());
map.put("nextFireTime", jobExecutionContext.getNextFireTime());
map.put("previousFireTime", jobExecutionContext.getPreviousFireTime());
map.put("refireCount", jobExecutionContext.getRefireCount());
map.put("result", jobExecutionContext.getResult());
map.put("scheduledFireTime", jobExecutionContext.getScheduledFireTime());
map.put("scheduler", jobExecutionContext.getScheduler());
Trigger trigger = jobExecutionContext.getTrigger();
map.put("trigger", trigger);
map.put("triggerName", trigger.getName());
map.put("triggerGroup", trigger.getGroup());
}
}
use of org.quartz.Trigger 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("quartz", 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.getName(), jobDetail.getGroup());
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.getFullName(), action, route.getId(), existingJobDetail.getFullName() });
}
// 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.getFullName(), action, route.getId() });
}
}
use of org.quartz.Trigger 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.Trigger in project camel by apache.
the class QuartzEndpoint method createTrigger.
private Trigger createTrigger(JobDetail jobDetail) throws Exception {
Trigger result;
Date startTime = new Date();
if (getComponent().getScheduler().isStarted()) {
startTime = new Date(System.currentTimeMillis() + triggerStartDelay);
}
if (cron != null) {
LOG.debug("Creating CronTrigger: {}", cron);
String timeZone = (String) triggerParameters.get("timeZone");
if (timeZone != null) {
if (ObjectHelper.isNotEmpty(customCalendar)) {
result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed().inTimeZone(TimeZone.getTimeZone(timeZone))).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR).build();
} else {
result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed().inTimeZone(TimeZone.getTimeZone(timeZone))).build();
}
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE, timeZone);
} else {
if (ObjectHelper.isNotEmpty(customCalendar)) {
result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed()).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR).build();
} else {
result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed()).build();
}
}
// enrich job map with details
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "cron");
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION, cron);
} else {
LOG.debug("Creating SimpleTrigger.");
int repeat = SimpleTrigger.REPEAT_INDEFINITELY;
String repeatString = (String) triggerParameters.get("repeatCount");
if (repeatString != null) {
repeat = EndpointHelper.resolveParameter(getCamelContext(), repeatString, Integer.class);
// need to update the parameters
triggerParameters.put("repeatCount", repeat);
}
// default use 1 sec interval
long interval = 1000;
String intervalString = (String) triggerParameters.get("repeatInterval");
if (intervalString != null) {
interval = EndpointHelper.resolveParameter(getCamelContext(), intervalString, Long.class);
// need to update the parameters
triggerParameters.put("repeatInterval", interval);
}
TriggerBuilder<SimpleTrigger> triggerBuilder;
if (ObjectHelper.isNotEmpty(customCalendar)) {
triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(repeat).withIntervalInMilliseconds(interval)).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
} else {
triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(repeat).withIntervalInMilliseconds(interval));
}
if (fireNow) {
triggerBuilder = triggerBuilder.startNow();
}
result = triggerBuilder.build();
// enrich job map with details
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "simple");
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_COUNTER, repeat);
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_INTERVAL, interval);
}
if (triggerParameters != null && triggerParameters.size() > 0) {
LOG.debug("Setting user extra triggerParameters {}", triggerParameters);
setProperties(result, triggerParameters);
}
LOG.debug("Created trigger={}", result);
return result;
}
Aggregations