Search in sources :

Example 66 with TriggerKey

use of org.quartz.TriggerKey in project ddf by codice.

the class ScheduledCommandTask method newTask.

@Override
public void newTask() {
    LOGGER.trace("Creating new Task.");
    long identifier = System.currentTimeMillis();
    this.jobKey = new JobKey("job" + identifier, CommandJob.class.getSimpleName());
    this.triggerKey = new TriggerKey("trigger" + identifier, CommandJob.class.getSimpleName());
    JobDetail jobDetail = createJob();
    Trigger trigger = createTrigger();
    if (trigger == null) {
        return;
    }
    try {
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
        LOGGER.info("Error with scheduling of task.", e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 67 with TriggerKey

use of org.quartz.TriggerKey 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 68 with TriggerKey

use of org.quartz.TriggerKey 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)

Example 69 with TriggerKey

use of org.quartz.TriggerKey in project opennms by OpenNMS.

the class DefaultSchedulerServiceIT method testScheduleAndRemove.

@Test
public void testScheduleAndRemove() throws SchedulerException {
    expect(m_reportWrapperService.validate(m_criteria, REPORT_ID)).andReturn(true);
    replay(m_reportWrapperService);
    MockRequestContext context = new MockRequestContext();
    DeliveryOptions deliveryOptions = new DeliveryOptions();
    deliveryOptions.setInstanceId("testScheduleAndRemoveTrigger");
    assertEquals("success", m_schedulerService.addCronTrigger(REPORT_ID, m_criteria, deliveryOptions, CRON_EXPRESSION, context));
    verify(m_reportWrapperService);
    Set<TriggerKey> triggers = m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(TRIGGER_GROUP));
    assertEquals(1, triggers.size());
    assertEquals("testScheduleAndRemoveTrigger", triggers.iterator().next().getName());
    m_schedulerService.removeTrigger("testScheduleAndRemoveTrigger");
    assertEquals(0, m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(TRIGGER_GROUP)).size());
}
Also used : TriggerKey(org.quartz.TriggerKey) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DeliveryOptions(org.opennms.reporting.core.DeliveryOptions) Test(org.junit.Test)

Example 70 with TriggerKey

use of org.quartz.TriggerKey in project opennms by OpenNMS.

the class DefaultSchedulerServiceIT method testMultipleTriggers.

@Test
public void testMultipleTriggers() throws SchedulerException {
    expect(m_reportWrapperService.validate(m_criteria, REPORT_ID)).andReturn(true).times(2);
    replay(m_reportWrapperService);
    MockRequestContext context = new MockRequestContext();
    // this trigger fires every 10 minutes starting at 0 minutes past the hour
    DeliveryOptions deliveryOptions1 = new DeliveryOptions();
    deliveryOptions1.setInstanceId("trigger1");
    assertEquals("success", m_schedulerService.addCronTrigger(REPORT_ID, m_criteria, deliveryOptions1, "0 0/10 * * * ?", context));
    // this trigger fires every 10 minutes starting at 5 minutes past the hour
    DeliveryOptions deliveryOptions2 = new DeliveryOptions();
    deliveryOptions2.setInstanceId("trigger2");
    assertEquals("success", m_schedulerService.addCronTrigger(REPORT_ID, m_criteria, deliveryOptions2, "0 5/10 * * * ?", context));
    verify(m_reportWrapperService);
    final List<String> triggers = m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(TRIGGER_GROUP)).stream().map(TriggerKey::getName).collect(Collectors.toList());
    assertEquals(2, triggers.size());
    assertTrue(triggers.contains("trigger1"));
    assertTrue(triggers.contains("trigger2"));
    m_schedulerService.removeTrigger("trigger1");
    m_schedulerService.removeTrigger("trigger2");
    assertEquals(0, m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(TRIGGER_GROUP)).size());
}
Also used : TriggerKey(org.quartz.TriggerKey) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DeliveryOptions(org.opennms.reporting.core.DeliveryOptions) Test(org.junit.Test)

Aggregations

TriggerKey (org.quartz.TriggerKey)112 Trigger (org.quartz.Trigger)49 JobKey (org.quartz.JobKey)30 SchedulerException (org.quartz.SchedulerException)30 CronTrigger (org.quartz.CronTrigger)25 JobDetail (org.quartz.JobDetail)22 Test (org.junit.Test)18 Scheduler (org.quartz.Scheduler)18 SimpleTrigger (org.quartz.SimpleTrigger)15 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 ArrayList (java.util.ArrayList)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 HashMap (java.util.HashMap)9 Date (java.util.Date)6 List (java.util.List)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ParseException (java.text.ParseException)4