Search in sources :

Example 26 with SchedulerException

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

the class DropboxSynchronizer method cancelAllJobs.

/**
     * Delete all quartz scheduler jobs of the group <code>Dropbox</code>.
     */
private void cancelAllJobs() {
    try {
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        Set<JobKey> jobKeys = sched.getJobKeys(jobGroupEquals(DROPBOX_SCHEDULER_GROUP));
        if (jobKeys.size() > 0) {
            sched.deleteJobs(new ArrayList<JobKey>(jobKeys));
            logger.debug("Found {} synchronization jobs to delete from DefaultScheduler (keys={})", jobKeys.size(), jobKeys);
        }
    } catch (SchedulerException e) {
        logger.warn("Couldn't remove synchronization job: {}", e.getMessage());
    }
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Example 27 with SchedulerException

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

the class JobScheduler method stop.

/**
     * Stops all scheduled jobs and clears the PlanetPublisher cache.
     */
public void stop() {
    try {
        for (JobKey jobKey : scheduler.getJobKeys(jobGroupEquals(JOB_GROUP))) {
            logger.info("Deleting astro job: " + jobKey.getName());
            scheduler.deleteJob(jobKey);
        }
    } catch (SchedulerException ex) {
        logger.error(ex.getMessage(), ex);
    }
    PlanetPublisher.getInstance().clear();
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException)

Example 28 with SchedulerException

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

the class JobScheduler method schedule.

/**
     * Schedules a job by trigger.
     */
private void schedule(String jobName, Class<? extends Job> job, Trigger trigger, JobDataMap jobDataMap) {
    try {
        JobDetail jobDetail = newJob(job).withIdentity(jobName, JOB_GROUP).usingJobData(jobDataMap).build();
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException ex) {
        logger.error(ex.getMessage(), ex);
    }
}
Also used : JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException)

Example 29 with SchedulerException

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

the class OceanicBinding method execute.

@Override
protected void execute() {
    if (isProperlyConfigured()) {
        Scheduler sched = null;
        try {
            sched = StdSchedulerFactory.getDefaultScheduler();
        } catch (SchedulerException e) {
            logger.error("An exception occurred while getting a reference to the Quartz Scheduler");
        }
        // reset the contextMap before rebuilding it
        for (String serialPort : serialDevices.keySet()) {
            Set<String> itemNames = contextMap.get(serialPort);
            if (itemNames != null) {
                contextMap.clear();
            }
        }
        for (OceanicBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                String serialPort = provider.getSerialPort(itemName);
                SerialDevice serialDevice = serialDevices.get(serialPort);
                boolean serialDeviceReady = true;
                if (serialDevice == null) {
                    serialDevice = new SerialDevice(serialPort);
                    try {
                        serialDevice.initialize();
                    } catch (InitializationException e) {
                        logger.error("Could not open serial port " + serialPort + ": " + e.getMessage());
                        serialDeviceReady = false;
                    } catch (Throwable e) {
                        logger.error("Could not open serial port " + serialPort + ": " + e.getMessage());
                        serialDeviceReady = false;
                    }
                    if (serialDeviceReady) {
                        serialDevice.setEventPublisher(eventPublisher);
                        serialDevices.put(serialPort, serialDevice);
                    }
                }
                Set<String> itemNames = contextMap.get(serialPort);
                if (itemNames == null) {
                    itemNames = new HashSet<String>();
                    contextMap.put(serialPort, itemNames);
                }
                itemNames.add(itemName);
                if (serialDeviceReady) {
                    // set up the polling jobs
                    boolean jobExists = false;
                    // enumerate each job group
                    try {
                        for (String group : sched.getJobGroupNames()) {
                            // enumerate each job in group
                            if (group.equals("Oceanic-" + provider.toString())) {
                                for (JobKey jobKey : sched.getJobKeys(jobGroupEquals(group))) {
                                    if (jobKey.getName().equals(itemName + "-" + provider.getValueSelector(itemName).toString())) {
                                        jobExists = true;
                                        break;
                                    }
                                }
                            }
                        }
                    } catch (SchedulerException e1) {
                        logger.error("An exception occurred while querying the Quartz Scheduler ({})", e1.getMessage());
                    }
                    if (!jobExists && OceanicValueSelector.getValueSelector(provider.getValueSelector(itemName), ValueSelectorType.GET) != null) {
                        // set up the Quartz jobs
                        JobDataMap map = new JobDataMap();
                        map.put("SerialPort", serialPort);
                        map.put("ValueSelector", OceanicValueSelector.getValueSelector(provider.getValueSelector(itemName), ValueSelectorType.GET));
                        map.put("Binding", this);
                        JobDetail job = newJob(OceanicBinding.PollJob.class).withIdentity(itemName + "-" + provider.getValueSelector(itemName).toString(), "Oceanic-" + provider.toString()).usingJobData(map).build();
                        Trigger trigger = newTrigger().withIdentity(itemName + "-" + provider.getValueSelector(itemName).toString(), "Oceanic-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(provider.getPollingInterval(itemName))).build();
                        try {
                            logger.debug("Adding a poll job {} for {}", job.getKey(), itemName);
                            sched.scheduleJob(job, trigger);
                        } catch (SchedulerException e) {
                            logger.error("An exception occurred while scheduling a Quartz Job");
                        }
                    }
                    // kill the Quartz jobs that we do not need anymore
                    try {
                        for (String group : sched.getJobGroupNames()) {
                            // enumerate each job in group
                            if (group.equals("Oceanic-" + provider.toString())) {
                                for (JobKey jobKey : sched.getJobKeys(jobGroupEquals(group))) {
                                    if (findFirstMatchingBindingProvider(jobKey.getName().split("-")[0]) == null) {
                                        logger.debug("Removing a poll job {} for {}", jobKey, itemName);
                                        sched.deleteJob(jobKey);
                                    }
                                }
                            }
                        }
                    } catch (SchedulerException e1) {
                        logger.error("An exception occurred while querying the Quartz Scheduler ({})", e1.getMessage());
                    }
                }
            }
        }
        // close down the serial ports that do not have any Items anymore associated to them
        for (String serialPort : serialDevices.keySet()) {
            SerialDevice serialDevice = serialDevices.get(serialPort);
            Set<String> itemNames = contextMap.get(serialPort);
            if (itemNames == null || itemNames.size() == 0) {
                contextMap.remove(serialPort);
                logger.debug("Closing the serial port {}", serialPort);
                serialDevice.close();
                serialDevices.remove(serialPort);
            }
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) OceanicBindingProvider(org.openhab.binding.oceanic.OceanicBindingProvider) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger)

Example 30 with SchedulerException

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

the class FHTBinding method scheduleJob.

/**
     * The user may configure this binding to update the internal clock of
     * FHT80b devices via rf command. The method takes care of scheduling this
     * job.
     */
private JobKey scheduleJob(Class<? extends Job> jobClass, String cronExpression) {
    JobKey jobKey = null;
    try {
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        JobDetail detail = JobBuilder.newJob(jobClass).withIdentity("FHT " + jobClass.getSimpleName(), "cul").build();
        detail.getJobDataMap().put(FHTBinding.class.getName(), this);
        CronTrigger trigger = TriggerBuilder.newTrigger().forJob(detail).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
        jobKey = detail.getKey();
        sched.scheduleJob(detail, trigger);
    } catch (SchedulerException e) {
        logger.error("Can't schedule time update job", e);
    }
    return jobKey;
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Aggregations

SchedulerException (org.quartz.SchedulerException)133 JobDetail (org.quartz.JobDetail)59 Trigger (org.quartz.Trigger)42 Scheduler (org.quartz.Scheduler)37 JobKey (org.quartz.JobKey)33 SimpleTrigger (org.quartz.SimpleTrigger)19 JobDataMap (org.quartz.JobDataMap)18 CronTrigger (org.quartz.CronTrigger)17 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 ApplicationContext (org.springframework.context.ApplicationContext)15 ArrayList (java.util.ArrayList)12 SchedulerContext (org.quartz.SchedulerContext)12 IOException (java.io.IOException)11 TriggerKey (org.quartz.TriggerKey)10 Date (java.util.Date)9 JobExecutionException (org.quartz.JobExecutionException)9 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)6 ParseException (java.text.ParseException)5 Command (org.openhab.core.types.Command)5 JobSystemException (com.dangdang.ddframe.job.exception.JobSystemException)4