Search in sources :

Example 76 with Trigger

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) Trigger(org.quartz.Trigger) SchedulerContext(org.quartz.SchedulerContext)

Example 77 with Trigger

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());
    }
}
Also used : Trigger(org.quartz.Trigger)

Example 78 with Trigger

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() });
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) QuartzComponent(org.apache.camel.component.quartz.QuartzComponent)

Example 79 with Trigger

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);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(org.quartz.Scheduler) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 80 with Trigger

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger) Date(java.util.Date) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint)

Aggregations

Trigger (org.quartz.Trigger)104 JobDetail (org.quartz.JobDetail)51 SchedulerException (org.quartz.SchedulerException)40 CronTrigger (org.quartz.CronTrigger)29 Test (org.junit.Test)23 Scheduler (org.quartz.Scheduler)22 JobDataMap (org.quartz.JobDataMap)21 SimpleTrigger (org.quartz.SimpleTrigger)20 TriggerKey (org.quartz.TriggerKey)20 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)19 JobKey (org.quartz.JobKey)16 ArrayList (java.util.ArrayList)13 Date (java.util.Date)12 List (java.util.List)5 Command (org.openhab.core.types.Command)5 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4 ParseException (java.text.ParseException)4 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)4 ProtoTrigger (co.cask.cdap.proto.ProtoTrigger)3