Search in sources :

Example 36 with Trigger

use of org.quartz.Trigger in project Dempsy by Dempsy.

the class RelativeOutputSchedule method start.

/**
 * Container will invoke this method.
 */
@Override
public void start() {
    try {
        JobDetail jobDetail = super.getJobDetail();
        Trigger trigger = getSimpleTrigger(timeUnit, (int) interval);
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    } catch (SchedulerException se) {
        logger.error("Error occurred while starting the relative scheduler : " + se.getMessage(), se);
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 37 with Trigger

use of org.quartz.Trigger in project opennms by OpenNMS.

the class Poller method schedulePolls.

private void schedulePolls() throws Exception {
    LOG.debug("Enter schedulePolls");
    Collection<PolledService> polledServices = m_pollerFrontEnd.getPolledServices();
    if (polledServices == null || polledServices.size() == 0) {
        LOG.warn("No polling scheduled.");
        LOG.debug("Exit schedulePolls");
        return;
    }
    long startTime = System.currentTimeMillis();
    long scheduleSpacing = m_initialSpreadTime / polledServices.size();
    for (PolledService polledService : polledServices) {
        String jobName = polledService.toString();
        // remove any currently scheduled job
        if (m_scheduler.deleteJob(new JobKey(jobName, PollJobDetail.GROUP))) {
            LOG.debug("Job for {} already scheduled.  Rescheduling", polledService);
        } else {
            LOG.debug("Scheduling job for {}", polledService);
        }
        Date initialPollTime = new Date(startTime);
        m_pollerFrontEnd.setInitialPollTime(polledService.getServiceId(), initialPollTime);
        /*
            JobBuilder.newJob(PollJob.class)
                .withIdentity(jobName)
                .usingJobData(PollJobDetail.JOB_DATA_MAP_KEY_POLLEDSERVICE, polledService)
                .usingJobData(PollJobDetail.JOB_DATA_MAP_KEY_POLLERFRONTEND, m_pollerFrontEnd);
            */
        PollJobDetail jobDetail = new PollJobDetail(jobName, PollJob.class);
        jobDetail.setPolledService(polledService);
        jobDetail.setPollerFrontEnd(m_pollerFrontEnd);
        SimpleTriggerFactoryBean triggerFactory = new PolledServiceTrigger(jobDetail);
        triggerFactory.setStartTime(initialPollTime);
        Trigger pollTrigger = triggerFactory.getObject();
        m_scheduler.scheduleJob(jobDetail, pollTrigger);
        startTime += scheduleSpacing;
    }
    LOG.debug("Exit schedulePolls");
}
Also used : JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) Date(java.util.Date)

Example 38 with Trigger

use of org.quartz.Trigger in project series-rest-api by 52North.

the class JobScheduler method scheduleJob.

private void scheduleJob(ScheduledJob taskToSchedule) {
    try {
        JobDetail details = taskToSchedule.createJobDetails();
        Trigger trigger = taskToSchedule.createTrigger(details.getKey());
        scheduler.scheduleJob(details, trigger);
        if (taskToSchedule.isTriggerAtStartup()) {
            LOGGER.debug("Schedule job '{}' to run once at startup.", details.getKey());
            Trigger onceAtStartup = TriggerBuilder.newTrigger().withIdentity("onceAtStartup").forJob(details.getKey()).build();
            scheduler.scheduleJob(onceAtStartup);
        }
    } catch (SchedulerException e) {
        LOGGER.warn("Could not schdule Job '{}'.", taskToSchedule.getJobName(), e);
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 39 with Trigger

use of org.quartz.Trigger 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);
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) Trigger(org.quartz.Trigger)

Example 40 with Trigger

use of org.quartz.Trigger 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);
    }
}
Also used : Scheduled(org.apache.deltaspike.scheduler.api.Scheduled) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) List(java.util.List)

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