Search in sources :

Example 51 with Scheduler

use of org.quartz.Scheduler 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 52 with Scheduler

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

the class QuartzComponentTest method testQuartzComponentCustomScheduler.

@Test
public void testQuartzComponentCustomScheduler() throws Exception {
    QuartzComponent comp = new QuartzComponent();
    comp.setCamelContext(context);
    SchedulerFactory fac = new StdSchedulerFactory();
    comp.setFactory(fac);
    assertSame(fac, comp.getFactory());
    Scheduler sch = fac.getScheduler();
    comp.setScheduler(sch);
    assertSame(sch, comp.getScheduler());
    comp.start();
    comp.stop();
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Scheduler(org.quartz.Scheduler) SchedulerFactory(org.quartz.SchedulerFactory) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Test(org.junit.Test)

Example 53 with Scheduler

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

the class QuartzEndpoint method pauseTrigger.

public void pauseTrigger() throws Exception {
    Scheduler scheduler = getComponent().getScheduler();
    boolean isClustered = scheduler.getMetaData().isJobStoreClustered();
    if (jobPaused.get() || isClustered) {
        return;
    }
    jobPaused.set(true);
    if (!scheduler.isShutdown()) {
        LOG.info("Pausing trigger {}", triggerKey);
        scheduler.pauseTrigger(triggerKey);
    }
}
Also used : Scheduler(org.quartz.Scheduler)

Example 54 with Scheduler

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

the class QuartzEndpoint method removeJobInScheduler.

private void removeJobInScheduler() throws Exception {
    Scheduler scheduler = getComponent().getScheduler();
    if (scheduler == null) {
        return;
    }
    if (deleteJob) {
        boolean isClustered = scheduler.getMetaData().isJobStoreClustered();
        if (!scheduler.isShutdown() && !isClustered) {
            LOG.info("Deleting job {}", triggerKey);
            scheduler.unscheduleJob(triggerKey);
            jobAdded.set(false);
        }
    } else if (pauseJob) {
        pauseTrigger();
    }
    // Decrement camel job count for this endpoint
    AtomicInteger number = (AtomicInteger) scheduler.getContext().get(QuartzConstants.QUARTZ_CAMEL_JOBS_COUNT);
    if (number != null) {
        number.decrementAndGet();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(org.quartz.Scheduler)

Example 55 with Scheduler

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

the class QuartzEndpoint method addJobInScheduler.

private void addJobInScheduler() throws Exception {
    // Add or use existing trigger to/from scheduler
    Scheduler scheduler = getComponent().getScheduler();
    JobDetail jobDetail;
    Trigger oldTrigger = scheduler.getTrigger(triggerKey);
    boolean triggerExisted = oldTrigger != null;
    if (triggerExisted && !isRecoverableJob()) {
        ensureNoDupTriggerKey();
    }
    jobDetail = createJobDetail();
    Trigger trigger = createTrigger(jobDetail);
    QuartzHelper.updateJobDataMap(getCamelContext(), jobDetail, getEndpointUri(), isUsingFixedCamelContextName());
    if (triggerExisted) {
        // Reschedule job if trigger settings were changed
        if (hasTriggerChanged(oldTrigger, trigger)) {
            scheduler.rescheduleJob(triggerKey, trigger);
        }
    } else {
        try {
            // Schedule it now. Remember that scheduler might not be started it, but we can schedule now.
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (ObjectAlreadyExistsException ex) {
            // some other VM might may have stored the job & trigger in DB in clustered mode, in the mean time
            if (!(getComponent().isClustered())) {
                throw ex;
            } else {
                trigger = scheduler.getTrigger(triggerKey);
                if (trigger == null) {
                    throw new SchedulerException("Trigger could not be found in quartz scheduler.");
                }
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Job {} (triggerType={}, jobClass={}) is scheduled. Next fire date is {}", new Object[] { trigger.getKey(), trigger.getClass().getSimpleName(), jobDetail.getJobClass().getSimpleName(), trigger.getNextFireTime() });
    }
    // Increase camel job count for this endpoint
    AtomicInteger number = (AtomicInteger) scheduler.getContext().get(QuartzConstants.QUARTZ_CAMEL_JOBS_COUNT);
    if (number != null) {
        number.incrementAndGet();
    }
    jobAdded.set(true);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(org.quartz.Scheduler) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Aggregations

Scheduler (org.quartz.Scheduler)74 SchedulerException (org.quartz.SchedulerException)38 JobDetail (org.quartz.JobDetail)34 Trigger (org.quartz.Trigger)23 Test (org.junit.Test)19 JobKey (org.quartz.JobKey)17 SimpleTrigger (org.quartz.SimpleTrigger)16 CronTrigger (org.quartz.CronTrigger)15 JobDataMap (org.quartz.JobDataMap)10 TriggerKey (org.quartz.TriggerKey)10 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)9 ArrayList (java.util.ArrayList)7 Date (java.util.Date)6 Command (org.openhab.core.types.Command)5 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4 IOException (java.io.IOException)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)3