Search in sources :

Example 46 with JobKey

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

the class HDanywhereBinding 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");
        }
        for (HDanywhereBindingProvider provider : providers) {
            HashMap<String, Integer> compiledList = provider.getIntervalList();
            if (compiledList != null) {
                Iterator<String> pbcIterator = compiledList.keySet().iterator();
                while (pbcIterator.hasNext()) {
                    String aHost = pbcIterator.next();
                    boolean jobExists = false;
                    // enumerate each job group
                    try {
                        for (String group : sched.getJobGroupNames()) {
                            // enumerate each job in group
                            for (JobKey jobKey : sched.getJobKeys(jobGroupEquals(group))) {
                                if (jobKey.getName().equals(aHost)) {
                                    jobExists = true;
                                    break;
                                }
                            }
                        }
                    } catch (SchedulerException e1) {
                        logger.error("An exception occurred while querying the Quartz Scheduler ({})", e1.getMessage());
                    }
                    if (!jobExists) {
                        // set up the Quartz jobs
                        JobDataMap map = new JobDataMap();
                        map.put("host", aHost);
                        map.put("binding", this);
                        JobDetail job = newJob(HDanywhereBinding.PollJob.class).withIdentity(aHost, "HDanywhere-" + provider.toString()).usingJobData(map).build();
                        Trigger trigger = newTrigger().withIdentity(aHost, "HDanywhere-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(compiledList.get(aHost))).build();
                        try {
                            sched.scheduleJob(job, trigger);
                        } catch (SchedulerException e) {
                            logger.error("An exception occurred while scheduling a Quartz Job");
                        }
                    }
                }
            }
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) HDanywhereBindingProvider(org.openhab.binding.hdanywhere.HDanywhereBindingProvider) Scheduler(org.quartz.Scheduler) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger)

Example 47 with JobKey

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

the class JobScheduler method schedule.

/**
     * Schedules a job at the specified date/time, deletes a previously
     * scheduled job.
     */
private void schedule(Calendar calendar, String jobName, JobDataMap jobDataMap, Class<? extends Job> jobClass) {
    if (System.currentTimeMillis() < calendar.getTimeInMillis()) {
        try {
            JobKey jobKey = new JobKey(jobName, JOB_GROUP);
            if (scheduler.getJobDetail(jobKey) != null) {
                scheduler.deleteJob(jobKey);
            }
            Trigger trigger = newTrigger().withIdentity(jobName + "-Trigger", JOB_GROUP).startAt(calendar.getTime()).build();
            JobDetail jobDetail = newJob(jobClass).withIdentity(jobKey).usingJobData(jobDataMap).build();
            scheduler.scheduleJob(jobDetail, trigger);
            logger.debug("Scheduled job with name {} at {}", jobName, sdf.format(calendar.getTime()));
        } catch (SchedulerException ex) {
            logger.error(ex.getMessage(), ex);
        }
    } else {
        logger.debug("Skipping job with name {} for today, starttime is in the past", jobName);
    }
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) SchedulerException(org.quartz.SchedulerException)

Example 48 with JobKey

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

the class FritzboxBinding method updated.

/**
     * {@inheritDoc}
     */
@Override
@SuppressWarnings("rawtypes")
public void updated(Dictionary config) throws ConfigurationException {
    if (config != null) {
        String ip = Objects.toString(config.get("ip"), null);
        if (StringUtils.isNotBlank(ip)) {
            if (!ip.equals(FritzboxBinding.ip)) {
                // only do something if the ip has changed
                FritzboxBinding.ip = ip;
                conditionalDeActivate();
                // and thus blocks the monitor thread
                try {
                    Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
                    JobKey jobKey = jobKey("Reconnect", "FritzBox");
                    TriggerKey triggerKey = triggerKey("Reconnect", "FritzBox");
                    if (sched.checkExists(jobKey)) {
                        logger.debug("Daily reconnection job already exists");
                    } else {
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
                        JobDetail job = newJob(ReconnectJob.class).withIdentity(jobKey).build();
                        CronTrigger trigger = newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                        sched.scheduleJob(job, trigger);
                        logger.debug("Scheduled a daily reconnection to FritzBox on {}:{}", ip, MONITOR_PORT);
                    }
                } catch (SchedulerException e) {
                    logger.warn("Could not create daily reconnection job", e);
                }
            }
        }
        String password = Objects.toString(config.get("password"), null);
        if (StringUtils.isNotBlank(password)) {
            FritzboxBinding.password = password;
        }
        String username = Objects.toString(config.get("user"), null);
        if (StringUtils.isNotBlank(username)) {
            FritzboxBinding.username = username;
        }
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Example 49 with JobKey

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

the class CallMonitor method shutdownReconnectJob.

/***
     * cancel the reconnect job
     */
public void shutdownReconnectJob() {
    Scheduler sched = null;
    try {
        sched = StdSchedulerFactory.getDefaultScheduler();
        JobKey jobKey = jobKey("Reconnect", "FritzBox");
        if (sched.checkExists(jobKey)) {
            logger.debug("Found reconnection job. Shutting down...");
            sched.deleteJob(jobKey);
        }
    } catch (SchedulerException e) {
        logger.warn("Error shutting down reconnect job: {}", e.getLocalizedMessage());
    }
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Example 50 with JobKey

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

the class CallMonitor method setupReconnectJob.

/***
     * reset the connection to fbox periodically
     */
public void setupReconnectJob() {
    try {
        // String cronPattern = "0 0 0 * * ?"; //every day
        // String cronPattern = "0 * * * * ?"; //every minute
        // every 2 hrs
        String cronPattern = "0 0 0/2 * * ?";
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        JobKey jobKey = jobKey("Reconnect", "FritzBox");
        TriggerKey triggerKey = triggerKey("Reconnect", "FritzBox");
        if (sched.checkExists(jobKey)) {
            logger.debug("reconnection job already exists");
        } else {
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronPattern);
            JobDetail job = newJob(ReconnectJob.class).withIdentity(jobKey).build();
            CronTrigger trigger = newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            sched.scheduleJob(job, trigger);
            logger.debug("Scheduled reconnection job to FritzBox: {}", cronPattern);
        }
    } catch (SchedulerException e) {
        logger.warn("Could not create daily reconnection job", e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Aggregations

JobKey (org.quartz.JobKey)66 SchedulerException (org.quartz.SchedulerException)33 JobDetail (org.quartz.JobDetail)22 Trigger (org.quartz.Trigger)19 Scheduler (org.quartz.Scheduler)17 TriggerKey (org.quartz.TriggerKey)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 List (java.util.List)6 CronTrigger (org.quartz.CronTrigger)6 JobDataMap (org.quartz.JobDataMap)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 Hashtable (java.util.Hashtable)2 Scheduled (org.apache.deltaspike.scheduler.api.Scheduled)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2