Search in sources :

Example 91 with JobDetail

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

Example 92 with JobDetail

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

the class QuartzEndpoint method createJobDetail.

private JobDetail createJobDetail() throws Exception {
    // Camel endpoint timer will assume one to one for JobDetail and Trigger, so let's use same name as trigger
    String name = triggerKey.getName();
    String group = triggerKey.getGroup();
    Class<? extends Job> jobClass = stateful ? StatefulCamelJob.class : CamelJob.class;
    LOG.debug("Creating new {}.", jobClass.getSimpleName());
    JobBuilder builder = JobBuilder.newJob(jobClass).withIdentity(name, group);
    if (durableJob) {
        builder = builder.storeDurably();
    }
    if (recoverableJob) {
        builder = builder.requestRecovery();
    }
    JobDetail result = builder.build();
    // Let user parameters to further set JobDetail properties.
    if (jobParameters != null && jobParameters.size() > 0) {
        LOG.debug("Setting user extra jobParameters {}", jobParameters);
        setProperties(result, jobParameters);
    }
    LOG.debug("Created jobDetail={}", result);
    return result;
}
Also used : JobDetail(org.quartz.JobDetail) JobBuilder(org.quartz.JobBuilder)

Example 93 with JobDetail

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

the class QuartzEndpointConfigureTest method testConfigureName.

@Test
public void testConfigureName() throws Exception {
    QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myName");
    Scheduler scheduler = endpoint.getComponent().getScheduler();
    TriggerKey triggerKey = endpoint.getTriggerKey();
    JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
    assertEquals("getName()", "myName", triggerKey.getName());
    assertEquals("getGroup()", "Camel_" + context.getManagementName(), triggerKey.getGroup());
    assertEquals("getJobName", "myName", jobDetail.getKey().getName());
    assertEquals("getJobGroup", "Camel_" + context.getManagementName(), jobDetail.getKey().getGroup());
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) Scheduler(org.quartz.Scheduler) Test(org.junit.Test)

Example 94 with JobDetail

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

the class QuartzEndpointConfigureTest method testConfigureAnotherCronExpression.

@Test
public void testConfigureAnotherCronExpression() throws Exception {
    QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myGroup/myTimerName?cron=0+0+*+*+*+?");
    Scheduler scheduler = endpoint.getComponent().getScheduler();
    TriggerKey triggerKey = endpoint.getTriggerKey();
    Trigger trigger = scheduler.getTrigger(triggerKey);
    JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
    assertEquals("getName()", "myTimerName", triggerKey.getName());
    assertEquals("getGroup()", "myGroup", triggerKey.getGroup());
    assertEquals("getJobName", "myTimerName", jobDetail.getKey().getName());
    assertEquals("getJobGroup", "myGroup", jobDetail.getKey().getGroup());
    assertIsInstanceOf(CronTrigger.class, trigger);
    CronTrigger cronTrigger = (CronTrigger) trigger;
    assertEquals("cron expression", "0 0 * * * ?", cronTrigger.getCronExpression());
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) Scheduler(org.quartz.Scheduler) Test(org.junit.Test)

Example 95 with JobDetail

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

the class ScheduledRoutePolicy method scheduleRoute.

public void scheduleRoute(Action action, Route route) throws Exception {
    JobDetail jobDetail = createJobDetail(action, route);
    Trigger trigger = createTrigger(action, route);
    updateScheduledRouteDetails(action, jobDetail, trigger, route);
    loadCallbackDataIntoSchedulerContext(jobDetail, action, route);
    boolean isClustered = route.getRouteContext().getCamelContext().getComponent("quartz2", QuartzComponent.class).isClustered();
    if (isClustered) {
        // check to see if the same job has already been setup through another node of the cluster
        JobDetail existingJobDetail = getScheduler().getJobDetail(jobDetail.getKey());
        if (jobDetail.equals(existingJobDetail)) {
            if (LOG.isInfoEnabled()) {
                LOG.info("Skipping to schedule the job: {} for action: {} on route {} as the job: {} already existing inside the cluster", new Object[] { jobDetail.getKey(), action, route.getId(), existingJobDetail.getKey() });
            }
            // skip scheduling the same job again as one is already existing for the same routeId and action
            return;
        }
    }
    getScheduler().scheduleJob(jobDetail, trigger);
    if (LOG.isInfoEnabled()) {
        LOG.info("Scheduled trigger: {} for action: {} on route {}", new Object[] { trigger.getKey(), action, route.getId() });
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent)

Aggregations

JobDetail (org.quartz.JobDetail)131 SchedulerException (org.quartz.SchedulerException)59 Trigger (org.quartz.Trigger)51 Scheduler (org.quartz.Scheduler)34 Test (org.junit.Test)30 CronTrigger (org.quartz.CronTrigger)26 JobKey (org.quartz.JobKey)22 SimpleTrigger (org.quartz.SimpleTrigger)22 JobDataMap (org.quartz.JobDataMap)20 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 TriggerKey (org.quartz.TriggerKey)10 HashMap (java.util.HashMap)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)5 Command (org.openhab.core.types.Command)5 IOException (java.io.IOException)4 Serializable (java.io.Serializable)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4