Search in sources :

Example 16 with Trigger

use of org.quartz.Trigger 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 17 with Trigger

use of org.quartz.Trigger 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 18 with Trigger

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

the class WeatherJobScheduler method scheduleIntervalJob.

/**
     * Schedules the WeatherJob with the specified interval and starts it
     * immediately.
     */
public void scheduleIntervalJob(LocationConfig locationConfig) {
    String jobName = "weatherJob-" + locationConfig.getLocationId();
    int interval = locationConfig.getUpdateInterval() * 60;
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put("locationId", locationConfig.getLocationId());
    try {
        Trigger trigger = newTrigger().withIdentity(jobName + "-Trigger", JOB_GROUP).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(interval)).build();
        JobDetail jobDetail = newJob(WeatherJob.class).withIdentity(jobName, JOB_GROUP).usingJobData(jobDataMap).build();
        scheduler.scheduleJob(jobDetail, trigger);
        logger.info("Starting and scheduling {} with interval of {} minutes", jobName, locationConfig.getUpdateInterval());
    } catch (SchedulerException ex) {
        logger.error(ex.getMessage(), ex);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 19 with Trigger

use of org.quartz.Trigger in project spring-framework by spring-projects.

the class SchedulerAccessor method registerJobsAndTriggers.

/**
	 * Register jobs and triggers (within a transaction, if possible).
	 */
protected void registerJobsAndTriggers() throws SchedulerException {
    TransactionStatus transactionStatus = null;
    if (this.transactionManager != null) {
        transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
    }
    try {
        if (this.jobSchedulingDataLocations != null) {
            ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
            clh.initialize();
            XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
            for (String location : this.jobSchedulingDataLocations) {
                dataProcessor.processFileAndScheduleJobs(location, getScheduler());
            }
        }
        // Register JobDetails.
        if (this.jobDetails != null) {
            for (JobDetail jobDetail : this.jobDetails) {
                addJobToScheduler(jobDetail);
            }
        } else {
            // Create empty list for easier checks when registering triggers.
            this.jobDetails = new LinkedList<>();
        }
        // Register Calendars.
        if (this.calendars != null) {
            for (String calendarName : this.calendars.keySet()) {
                Calendar calendar = this.calendars.get(calendarName);
                getScheduler().addCalendar(calendarName, calendar, true, true);
            }
        }
        // Register Triggers.
        if (this.triggers != null) {
            for (Trigger trigger : this.triggers) {
                addTriggerToScheduler(trigger);
            }
        }
    } catch (Throwable ex) {
        if (transactionStatus != null) {
            try {
                this.transactionManager.rollback(transactionStatus);
            } catch (TransactionException tex) {
                logger.error("Job registration exception overridden by rollback exception", ex);
                throw tex;
            }
        }
        if (ex instanceof SchedulerException) {
            throw (SchedulerException) ex;
        }
        if (ex instanceof Exception) {
            throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
        }
        throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
    }
    if (transactionStatus != null) {
        this.transactionManager.commit(transactionStatus);
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) SchedulerException(org.quartz.SchedulerException) XMLSchedulingDataProcessor(org.quartz.xml.XMLSchedulingDataProcessor) Calendar(org.quartz.Calendar) TransactionStatus(org.springframework.transaction.TransactionStatus) ClassLoadHelper(org.quartz.spi.ClassLoadHelper) SchedulerException(org.quartz.SchedulerException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) TransactionException(org.springframework.transaction.TransactionException) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TransactionException(org.springframework.transaction.TransactionException)

Example 20 with Trigger

use of org.quartz.Trigger in project cachecloud by sohutv.

the class SchedulerCenterTest method testSchedule.

@Test
public void testSchedule() {
    TriggerKey key = TriggerKey.triggerKey("appInfoAlertTrigger", "appAlert");
    Trigger trigger = schedulerCenter.getTrigger(key);
    if (trigger != null) {
        boolean isSchedule = schedulerCenter.unscheduleJob(key);
        logger.warn("isSchedule={}", isSchedule);
    }
//        try {
//            TimeUnit.SECONDS.sleep(5);
//        } catch (InterruptedException e) {
//            logger.error("{}", e);
//        }
}
Also used : TriggerKey(org.quartz.TriggerKey) Trigger(org.quartz.Trigger) BaseTest(com.sohu.test.BaseTest) Test(org.junit.Test)

Aggregations

Trigger (org.quartz.Trigger)98 JobDetail (org.quartz.JobDetail)47 SchedulerException (org.quartz.SchedulerException)37 CronTrigger (org.quartz.CronTrigger)28 Test (org.junit.Test)23 JobDataMap (org.quartz.JobDataMap)21 Scheduler (org.quartz.Scheduler)21 TriggerKey (org.quartz.TriggerKey)20 SimpleTrigger (org.quartz.SimpleTrigger)19 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)18 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 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4 ParseException (java.text.ParseException)4 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)4