Search in sources :

Example 11 with CronTrigger

use of org.quartz.CronTrigger in project openhab1-addons by openhab.

the class Db4oPersistenceService method scheduleJob.

/**
     * Schedules new quartz scheduler jobs for committing transactions and
     * backing up the database
     */
private void scheduleJob() {
    try {
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        // schedule commit-job
        JobDetail job = newJob(CommitJob.class).withIdentity("Commit_Transaction", SCHEDULER_GROUP).build();
        SimpleTrigger trigger = newTrigger().withIdentity("Commit_Transaction", SCHEDULER_GROUP).withSchedule(repeatSecondlyForever(commitInterval)).build();
        sched.scheduleJob(job, trigger);
        logger.debug("Scheduled Commit-Job with interval {}sec.", commitInterval);
        // schedule backup-job
        JobDetail backupJob = newJob(BackupJob.class).withIdentity("Backup_DB", SCHEDULER_GROUP).build();
        CronTrigger backupTrigger = newTrigger().withIdentity("Backup_DB", SCHEDULER_GROUP).withSchedule(CronScheduleBuilder.cronSchedule(backupInterval)).build();
        sched.scheduleJob(backupJob, backupTrigger);
        logger.debug("Scheduled Backup-Job with cron expression '{}'", backupInterval);
    } catch (SchedulerException e) {
        logger.warn("Could not create Job: {}", e.getMessage());
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) SimpleTrigger(org.quartz.SimpleTrigger)

Example 12 with CronTrigger

use of org.quartz.CronTrigger in project openhab1-addons by openhab.

the class PlugwiseBinding method scheduleJobs.

private void scheduleJobs(Scheduler scheduler) {
    for (PlugwiseBindingProvider provider : providers) {
        for (PlugwiseBindingConfigElement element : provider.getIntervalList()) {
            PlugwiseCommandType type = element.getCommandType();
            if (type.getJobClass() == null) {
                continue;
            }
            if (stick.getDevice(element.getId()) == null) {
                logger.debug("The Plugwise device with id {} is not yet defined", element.getId());
                // check if the config string really contains a MAC address
                Pattern MAC_PATTERN = Pattern.compile("(\\w{16})");
                Matcher matcher = MAC_PATTERN.matcher(element.getId());
                if (matcher.matches()) {
                    List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
                    if (!cps.isEmpty()) {
                        CirclePlus cp = cps.get(0);
                        if (!cp.getMAC().equals(element.getId())) {
                            // a circleplus has been added/detected and it is not what is in the binding config
                            PlugwiseDevice device = new Circle(element.getId(), stick, element.getId());
                            stick.addDevice(device);
                            logger.debug("Plugwise added Circle with MAC address: {}", element.getId());
                        }
                    } else {
                        logger.warn("Plugwise can not guess the device that should be added. Consider defining it in the openHAB configuration file");
                    }
                } else {
                    logger.warn("Plugwise can not add a valid device without a proper MAC address. {} can not be used", element.getId());
                }
            }
            if (stick.getDevice(element.getId()) != null) {
                String jobName = element.getId() + "-" + type.getJobClass().toString();
                if (!isExistingJob(scheduler, jobName)) {
                    // set up the Quartz jobs
                    JobDataMap map = new JobDataMap();
                    map.put(STICK_JOB_DATA_KEY, stick);
                    map.put(MAC_JOB_DATA_KEY, stick.getDevice(element.getId()).MAC);
                    JobDetail job = newJob(type.getJobClass()).withIdentity(jobName, "Plugwise-" + provider.toString()).usingJobData(map).build();
                    Trigger trigger = newTrigger().withIdentity(element.getId() + "-" + type.getJobClass().toString(), "Plugwise-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(element.getInterval())).build();
                    try {
                        scheduler.scheduleJob(job, trigger);
                    } catch (SchedulerException e) {
                        logger.error("An exception occurred while scheduling a Plugwise Quartz Job", e);
                    }
                }
            } else {
                logger.error("Error scheduling a Quartz Job for a non-defined Plugwise device (" + element.getId() + ")");
            }
        }
    }
    List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
    if (!cps.isEmpty()) {
        CirclePlus cp = cps.get(0);
        String jobName = cp.MAC + "-SetCirclePlusClock";
        if (!isExistingJob(scheduler, jobName)) {
            JobDataMap map = new JobDataMap();
            map.put(CirclePlus.CIRCLE_PLUS_JOB_DATA_KEY, cp);
            JobDetail job = newJob(SetClockJob.class).withIdentity(jobName, "Plugwise").usingJobData(map).build();
            CronTrigger trigger = newTrigger().withIdentity(jobName, "Plugwise").startNow().withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
            try {
                Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
                sched.scheduleJob(job, trigger);
            } catch (SchedulerException e) {
                logger.error("Error scheduling Circle+ setClock Quartz Job", e);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) JobDataMap(org.quartz.JobDataMap) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Matcher(java.util.regex.Matcher) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) Scheduler(org.quartz.Scheduler) PlugwiseBindingConfigElement(org.openhab.binding.plugwise.internal.PlugwiseGenericBindingProvider.PlugwiseBindingConfigElement) SetClockJob(org.openhab.binding.plugwise.internal.CirclePlus.SetClockJob) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) PlugwiseBindingProvider(org.openhab.binding.plugwise.PlugwiseBindingProvider)

Example 13 with CronTrigger

use of org.quartz.CronTrigger in project openhab1-addons by openhab.

the class DropboxSynchronizer method schedule.

/**
     * Schedules either a job handling the Upload (<code>LOCAL_TO_DROPBOX</code>)
     * or Download (<code>DROPBOX_TO_LOCAL</code>) direction depending on
     * <code>isUpload</code>.
     * 
     * @param interval the Trigger interval as cron expression
     * @param isUpload
     */
private void schedule(String interval, boolean isUpload) {
    String direction = isUpload ? "Upload" : "Download";
    try {
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        JobDetail job = newJob(SynchronizationJob.class).withIdentity(direction, DROPBOX_SCHEDULER_GROUP).build();
        CronTrigger trigger = newTrigger().withIdentity(direction, DROPBOX_SCHEDULER_GROUP).withSchedule(CronScheduleBuilder.cronSchedule(interval)).build();
        logger.debug("Scheduled synchronization job (direction={}) with cron expression '{}'", direction, interval);
        sched.scheduleJob(job, trigger);
    } catch (SchedulerException e) {
        logger.warn("Could not create synchronization job: {}", e.getMessage());
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Example 14 with CronTrigger

use of org.quartz.CronTrigger in project openhab1-addons by openhab.

the class JobScheduler method startAndScheduleDailyJob.

/**
     * Schedules a daily job at midnight for astro calculation and starts it
     * immediately too.
     */
public void startAndScheduleDailyJob() {
    String jobName = DailyJob.class.getSimpleName();
    CronTrigger cronTrigger = newTrigger().withIdentity(jobName + "-Trigger", JOB_GROUP).startNow().withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
    schedule(jobName, DailyJob.class, cronTrigger, new JobDataMap());
    logger.info("Scheduled a daily job at midnight for astro calculation");
    Trigger trigger = newTrigger().withIdentity(jobName + "-StartupTrigger", JOB_GROUP).startNow().build();
    schedule(jobName + "-Startup", DailyJob.class, trigger, new JobDataMap());
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDataMap(org.quartz.JobDataMap) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger)

Example 15 with CronTrigger

use of org.quartz.CronTrigger in project camel by apache.

the class QuartzComponent method hasTriggerChanged.

private static boolean hasTriggerChanged(Trigger oldTrigger, Trigger newTrigger) {
    if (newTrigger instanceof CronTrigger && oldTrigger instanceof CronTrigger) {
        CronTrigger newCron = (CronTrigger) newTrigger;
        CronTrigger oldCron = (CronTrigger) oldTrigger;
        return !newCron.getCronExpression().equals(oldCron.getCronExpression());
    } else if (newTrigger instanceof SimpleTrigger && oldTrigger instanceof SimpleTrigger) {
        SimpleTrigger newSimple = (SimpleTrigger) newTrigger;
        SimpleTrigger oldSimple = (SimpleTrigger) oldTrigger;
        return newSimple.getRepeatInterval() != oldSimple.getRepeatInterval() || newSimple.getRepeatCount() != oldSimple.getRepeatCount();
    } else {
        return !newTrigger.getClass().equals(oldTrigger.getClass()) || !newTrigger.equals(oldTrigger);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger)

Aggregations

CronTrigger (org.quartz.CronTrigger)42 JobDetail (org.quartz.JobDetail)20 Test (org.junit.Test)17 Trigger (org.quartz.Trigger)14 SchedulerException (org.quartz.SchedulerException)13 Scheduler (org.quartz.Scheduler)12 SimpleTrigger (org.quartz.SimpleTrigger)11 TriggerKey (org.quartz.TriggerKey)7 JobKey (org.quartz.JobKey)5 Date (java.util.Date)4 CamelContext (org.apache.camel.CamelContext)4 JobDataMap (org.quartz.JobDataMap)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2 JobExecutionException (org.quartz.JobExecutionException)2