Search in sources :

Example 36 with CronTrigger

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

the class QuartzEndpoint method createTrigger.

private Trigger createTrigger(JobDetail jobDetail) throws Exception {
    Trigger result;
    Date startTime = new Date();
    if (getComponent().getScheduler().isStarted()) {
        startTime = new Date(System.currentTimeMillis() + triggerStartDelay);
    }
    if (cron != null) {
        LOG.debug("Creating CronTrigger: {}", cron);
        String timeZone = (String) triggerParameters.get("timeZone");
        if (timeZone != null) {
            if (ObjectHelper.isNotEmpty(customCalendar)) {
                result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed().inTimeZone(TimeZone.getTimeZone(timeZone))).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR).build();
            } else {
                result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed().inTimeZone(TimeZone.getTimeZone(timeZone))).build();
            }
            jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE, timeZone);
        } else {
            if (ObjectHelper.isNotEmpty(customCalendar)) {
                result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed()).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR).build();
            } else {
                result = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(cronSchedule(cron).withMisfireHandlingInstructionFireAndProceed()).build();
            }
        }
        // enrich job map with details
        jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "cron");
        jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION, cron);
    } else {
        LOG.debug("Creating SimpleTrigger.");
        int repeat = SimpleTrigger.REPEAT_INDEFINITELY;
        String repeatString = (String) triggerParameters.get("repeatCount");
        if (repeatString != null) {
            repeat = EndpointHelper.resolveParameter(getCamelContext(), repeatString, Integer.class);
            // need to update the parameters
            triggerParameters.put("repeatCount", repeat);
        }
        // default use 1 sec interval
        long interval = 1000;
        String intervalString = (String) triggerParameters.get("repeatInterval");
        if (intervalString != null) {
            interval = EndpointHelper.resolveParameter(getCamelContext(), intervalString, Long.class);
            // need to update the parameters
            triggerParameters.put("repeatInterval", interval);
        }
        TriggerBuilder<SimpleTrigger> triggerBuilder;
        if (ObjectHelper.isNotEmpty(customCalendar)) {
            triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(repeat).withIntervalInMilliseconds(interval)).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
        } else {
            triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startTime).withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow().withRepeatCount(repeat).withIntervalInMilliseconds(interval));
        }
        if (fireNow) {
            triggerBuilder = triggerBuilder.startNow();
        }
        result = triggerBuilder.build();
        // enrich job map with details
        jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "simple");
        jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_COUNTER, repeat);
        jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_INTERVAL, interval);
    }
    if (triggerParameters != null && triggerParameters.size() > 0) {
        LOG.debug("Setting user extra triggerParameters {}", triggerParameters);
        setProperties(result, triggerParameters);
    }
    LOG.debug("Created trigger={}", result);
    return result;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger) Date(java.util.Date) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint)

Example 37 with CronTrigger

use of org.quartz.CronTrigger 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 38 with CronTrigger

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

the class QuartzEndpointConfigureTest method testConfigureNoDoubleSlashQuestionCron.

@Test
public void testConfigureNoDoubleSlashQuestionCron() 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 39 with CronTrigger

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

the class QuartzStatefulJobRouteTest method testQuartzCronRoute.

@Test
public void testQuartzCronRoute() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(3);
    assertMockEndpointsSatisfied();
    Trigger trigger = mock.getReceivedExchanges().get(0).getIn().getHeader("trigger", Trigger.class);
    Assert.assertThat(trigger instanceof CronTrigger, CoreMatchers.is(true));
    JobDetail detail = mock.getReceivedExchanges().get(0).getIn().getHeader("jobDetail", JobDetail.class);
    Assert.assertThat(detail.getJobClass().equals(StatefulCamelJob.class), CoreMatchers.is(true));
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 40 with CronTrigger

use of org.quartz.CronTrigger in project jforum2 by rafaelsteil.

the class POPJobStarter method startJob.

public static void startJob() throws SchedulerException {
    boolean isEnabled = SystemGlobals.getBoolValue(ConfigKeys.MAIL_POP3_INTEGRATION_ENABLED);
    if (!isStarted && isEnabled) {
        String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
        String cronExpression = SystemGlobals.getValue("org.quartz.context.mailintegration.cron.expression");
        scheduler = new StdSchedulerFactory(filename).getScheduler();
        Trigger trigger = null;
        try {
            trigger = new CronTrigger(POPListener.class.getName(), "pop3Integration", cronExpression);
            logger.info("Starting POP3 integration expression " + cronExpression);
            scheduler.scheduleJob(new JobDetail(POPListener.class.getName(), "pop3Integration", POPListener.class), trigger);
            scheduler.start();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    isStarted = true;
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) ParseException(java.text.ParseException)

Aggregations

CronTrigger (org.quartz.CronTrigger)43 JobDetail (org.quartz.JobDetail)21 Test (org.junit.Test)17 Trigger (org.quartz.Trigger)15 SchedulerException (org.quartz.SchedulerException)13 Scheduler (org.quartz.Scheduler)12 SimpleTrigger (org.quartz.SimpleTrigger)12 TriggerKey (org.quartz.TriggerKey)7 JobKey (org.quartz.JobKey)5 Date (java.util.Date)4 CamelContext (org.apache.camel.CamelContext)4 JobDataMap (org.quartz.JobDataMap)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2 JobExecutionException (org.quartz.JobExecutionException)2