Search in sources :

Example 1 with SchedulerError

use of org.apache.karaf.scheduler.SchedulerError in project karaf by apache.

the class QuartzScheduler method trigger.

@Override
public boolean trigger(String jobName) throws SchedulerError {
    final org.quartz.Scheduler s = this.scheduler;
    if (jobName != null && s != null) {
        try {
            final JobKey key = JobKey.jobKey(jobName);
            final JobDetail jobdetail = s.getJobDetail(key);
            if (jobdetail != null) {
                this.scheduler.triggerJob(key, jobdetail.getJobDataMap());
                return true;
            }
        } catch (SchedulerException ex) {
            throw new SchedulerError(ex);
        }
    }
    return false;
}
Also used : SchedulerError(org.apache.karaf.scheduler.SchedulerError) org.quartz(org.quartz)

Example 2 with SchedulerError

use of org.apache.karaf.scheduler.SchedulerError 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 SchedulerError 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, SchedulerError {
    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;
    // 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.compile().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);
    try {
        s.scheduleJob(detail, trigger);
    } catch (SchedulerException ex) {
        throw new SchedulerError(ex);
    }
}
Also used : SchedulerError(org.apache.karaf.scheduler.SchedulerError) org.quartz(org.quartz)

Example 3 with SchedulerError

use of org.apache.karaf.scheduler.SchedulerError in project openk9 by smclab.

the class DriverManagerActivator method _schedule.

private Mono<Void> _schedule(Datasource datasource) {
    return Mono.create(sink -> {
        try {
            Map<String, ScheduleOptions> jobs = _scheduler.getJobs();
            String jobName = _PREFIX + datasource.getName();
            ScheduleOptions scheduleOptions = jobs.get(jobName);
            if (scheduleOptions != null) {
                _scheduler.unschedule(jobName);
            }
            _scheduler.schedule(_createJob(datasource.getDriverServiceName(), datasource.getDatasourceId()), _scheduler.EXPR(datasource.getScheduling()).name(jobName));
            _scheduler.trigger(jobName);
            sink.success();
        } catch (SchedulerError schedulerError) {
            sink.error(schedulerError);
        }
    });
}
Also used : SchedulerError(org.apache.karaf.scheduler.SchedulerError) ScheduleOptions(org.apache.karaf.scheduler.ScheduleOptions)

Example 4 with SchedulerError

use of org.apache.karaf.scheduler.SchedulerError in project karaf by apache.

the class QuartzScheduler method reschedule.

@Override
public void reschedule(String jobName, ScheduleOptions options) throws SchedulerError {
    final org.quartz.Scheduler s = this.scheduler;
    if (jobName == null) {
        throw new IllegalArgumentException("Job name is mandatory");
    }
    JobKey key = JobKey.jobKey(jobName);
    if (key == null) {
        throw new IllegalStateException("No job found with name " + jobName);
    }
    try {
        JobDetail detail = s.getJobDetail(key);
        final String contextKey = key.toString();
        JobDataMap karafContext = ((KarafStdScheduler) s).getStorage().get(contextKey);
        Object job = karafContext.get(QuartzScheduler.DATA_MAP_OBJECT);
        s.deleteJob(key);
        final InternalScheduleOptions opts = (InternalScheduleOptions) options;
        Trigger trigger = opts.compile().withIdentity(jobName).build();
        JobDataMap jobDataMap = this.initDataMap(jobName, job, opts);
        detail = createJobDetail(jobName, jobDataMap, opts.canRunConcurrently);
        logger.debug("Update job scheduling {} with name {} and trigger {}", job, jobName, trigger);
        s.scheduleJob(detail, trigger);
    } catch (SchedulerException e) {
        throw new SchedulerError(e);
    }
}
Also used : SchedulerError(org.apache.karaf.scheduler.SchedulerError) org.quartz(org.quartz)

Example 5 with SchedulerError

use of org.apache.karaf.scheduler.SchedulerError in project karaf by apache.

the class QuartzScheduler method getJobs.

@Override
public Map<String, ScheduleOptions> getJobs() throws SchedulerError {
    try {
        Map<String, ScheduleOptions> jobs = new HashMap<>();
        org.quartz.Scheduler s = this.scheduler;
        if (s != null) {
            for (String group : s.getJobGroupNames()) {
                for (JobKey key : s.getJobKeys(GroupMatcher.jobGroupEquals(group))) {
                    JobDetail detail = s.getJobDetail(key);
                    ScheduleOptions options = (ScheduleOptions) detail.getJobDataMap().get(DATA_MAP_OPTIONS);
                    jobs.put(key.getName(), options);
                }
            }
        }
        return jobs;
    } catch (SchedulerException ex) {
        throw new SchedulerError(ex);
    }
}
Also used : SchedulerError(org.apache.karaf.scheduler.SchedulerError) ScheduleOptions(org.apache.karaf.scheduler.ScheduleOptions) org.quartz(org.quartz)

Aggregations

SchedulerError (org.apache.karaf.scheduler.SchedulerError)7 org.quartz (org.quartz)4 ScheduleOptions (org.apache.karaf.scheduler.ScheduleOptions)3 Map (java.util.Map)2 DatasourceRepository (io.openk9.datasource.repository.DatasourceRepository)1 ObjectNode (io.openk9.json.api.ObjectNode)1 Datasource (io.openk9.model.Datasource)1 AutoCloseables (io.openk9.osgi.util.AutoCloseables)1 PluginDriverManagerClient (io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient)1 SchedulerEnabledDTO (io.openk9.plugin.driver.manager.model.SchedulerEnabledDTO)1 EntityEvent (io.openk9.sql.api.event.EntityEvent)1 EntityEventBus (io.openk9.sql.api.event.EntityEventBus)1 SchedulerException (io.openk9.sql.scheduler.web.exception.SchedulerException)1 Date (java.util.Date)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Job (org.apache.karaf.scheduler.Job)1 Scheduler (org.apache.karaf.scheduler.Scheduler)1 Activate (org.osgi.service.component.annotations.Activate)1 Component (org.osgi.service.component.annotations.Component)1