Search in sources :

Example 6 with SimpleTrigger

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

the class SimpleTriggerFactoryBeanTests method createWithoutJobDetail.

@Test
public void createWithoutJobDetail() throws ParseException {
    SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
    factory.setName("myTrigger");
    factory.setRepeatCount(5);
    factory.setRepeatInterval(1000L);
    factory.afterPropertiesSet();
    SimpleTrigger trigger = factory.getObject();
    assertEquals(5, trigger.getRepeatCount());
    assertEquals(1000L, trigger.getRepeatInterval());
}
Also used : SimpleTrigger(org.quartz.SimpleTrigger) Test(org.junit.Test)

Example 7 with SimpleTrigger

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

the class QuartzComponent method createEndpoint.

@Override
protected QuartzEndpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters) throws Exception {
    // lets split the remaining into a group/name
    URI u = new URI(uri);
    String path = ObjectHelper.after(u.getPath(), "/");
    String host = u.getHost();
    String cron = getAndRemoveParameter(parameters, "cron", String.class);
    boolean fireNow = getAndRemoveParameter(parameters, "fireNow", Boolean.class, Boolean.FALSE);
    Integer startDelayedSeconds = getAndRemoveParameter(parameters, "startDelayedSeconds", Integer.class);
    if (startDelayedSeconds != null) {
        if (scheduler.isStarted()) {
            LOG.warn("A Quartz job is already started. Cannot apply the 'startDelayedSeconds' configuration!");
        } else if (this.startDelayedSeconds != 0 && !(this.startDelayedSeconds == startDelayedSeconds)) {
            LOG.warn("A Quartz job is already configured with a different 'startDelayedSeconds' configuration! " + "All Quartz jobs must share the same 'startDelayedSeconds' configuration! Cannot apply the 'startDelayedSeconds' configuration!");
        } else {
            this.startDelayedSeconds = startDelayedSeconds;
        }
    }
    // host can be null if the uri did contain invalid host characters such as an underscore
    if (host == null) {
        host = ObjectHelper.before(remaining, "/");
        if (host == null) {
            host = remaining;
        }
    }
    // group can be optional, if so set it to Camel
    String name;
    String group;
    if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(host)) {
        group = host;
        name = path;
    } else {
        group = "Camel";
        name = host;
    }
    Map<String, Object> triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
    Map<String, Object> jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
    Trigger trigger;
    boolean stateful = "true".equals(parameters.get("stateful"));
    // if we're starting up and not running in Quartz clustered mode or not stateful then check for a name conflict.
    if (!isClustered() && !stateful) {
        // check to see if this trigger already exists
        trigger = getScheduler().getTrigger(name, group);
        if (trigger != null) {
            String msg = "A Quartz job already exists with the name/group: " + name + "/" + group;
            throw new IllegalArgumentException(msg);
        }
    }
    // create the trigger either cron or simple
    if (ObjectHelper.isNotEmpty(cron)) {
        cron = encodeCronExpression(cron);
        trigger = createCronTrigger(cron);
    } else {
        trigger = new SimpleTrigger();
        if (fireNow) {
            String intervalString = (String) triggerParameters.get("repeatInterval");
            if (intervalString != null) {
                long interval = EndpointHelper.resolveParameter(getCamelContext(), intervalString, Long.class);
                trigger.setStartTime(new Date(System.currentTimeMillis() - interval));
            }
        }
    }
    QuartzEndpoint answer = new QuartzEndpoint(uri, this);
    answer.setGroupName(group);
    answer.setTimerName(name);
    answer.setCron(cron);
    answer.setFireNow(fireNow);
    if (startDelayedSeconds != null) {
        answer.setStartDelayedSeconds(startDelayedSeconds);
    }
    if (triggerParameters != null && !triggerParameters.isEmpty()) {
        answer.setTriggerParameters(triggerParameters);
    }
    if (jobParameters != null && !jobParameters.isEmpty()) {
        answer.setJobParameters(jobParameters);
        setProperties(answer.getJobDetail(), jobParameters);
    }
    // enrich job data map with trigger information
    if (cron != null) {
        answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "cron");
        answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION, cron);
        String timeZone = EndpointHelper.resolveParameter(getCamelContext(), (String) triggerParameters.get("timeZone"), String.class);
        if (timeZone != null) {
            answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE, timeZone);
        }
    } else {
        answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_TYPE, "simple");
        Long interval = EndpointHelper.resolveParameter(getCamelContext(), (String) triggerParameters.get("repeatInterval"), Long.class);
        if (interval != null) {
            triggerParameters.put("repeatInterval", interval);
            answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_INTERVAL, interval);
        }
        Integer counter = EndpointHelper.resolveParameter(getCamelContext(), (String) triggerParameters.get("repeatCount"), Integer.class);
        if (counter != null) {
            triggerParameters.put("repeatCount", counter);
            answer.getJobDetail().getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_COUNTER, counter);
        }
    }
    setProperties(trigger, triggerParameters);
    trigger.setName(name);
    trigger.setGroup(group);
    answer.setTrigger(trigger);
    return answer;
}
Also used : URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger)

Example 8 with SimpleTrigger

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

the class QuartzComponent method hasTriggerChanged.

private static boolean hasTriggerChanged(Trigger oldTrigger, Trigger newTrigger) {
    if (newTrigger instanceof CronTrigger && oldTrigger instanceof CronTrigger) {
        CronTrigger newCron = (CronTrigger) newTrigger;
        CronTrigger oldCron = (CronTrigger) oldTrigger;
        return !newCron.getCronExpression().equals(oldCron.getCronExpression());
    } else if (newTrigger instanceof SimpleTrigger && oldTrigger instanceof SimpleTrigger) {
        SimpleTrigger newSimple = (SimpleTrigger) newTrigger;
        SimpleTrigger oldSimple = (SimpleTrigger) oldTrigger;
        return newSimple.getRepeatInterval() != oldSimple.getRepeatInterval() || newSimple.getRepeatCount() != oldSimple.getRepeatCount();
    } else {
        return !newTrigger.getClass().equals(oldTrigger.getClass()) || !newTrigger.equals(oldTrigger);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger)

Example 9 with SimpleTrigger

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

the class SpringQuartzPersistentStoreRestartAppChangeOptionsTest method testRestartAppChangeTriggerType.

@Test
public void testRestartAppChangeTriggerType() throws Exception {
    // Test creates application context twice with different simple trigger options in configuration xml.
    // Both times it retrieves back the option, accessing it via trigger (so, using value stored in DB).
    // After that it asserts that two options are not equal.
    // load spring app
    app = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeCronExpressionTest1.xml");
    app.start();
    CamelContext camel = app.getBean("camelContext", CamelContext.class);
    assertNotNull(camel);
    assertTrue(getTrigger(camel, "quartzRoute") instanceof CronTrigger);
    app.stop();
    log.info("Restarting ...");
    log.info("Restarting ...");
    log.info("Restarting ...");
    // load spring app
    AbstractXmlApplicationContext app2 = new ClassPathXmlApplicationContext("org/apache/camel/component/quartz/SpringQuartzPersistentStoreRestartAppChangeOptionsTest2.xml");
    app2.start();
    CamelContext camel2 = app2.getBean("camelContext", CamelContext.class);
    assertNotNull(camel2);
    assertTrue(getTrigger(camel2, "quartzRoute") instanceof SimpleTrigger);
    app2.stop();
    // we're done so let's properly close the application contexts, but close
    // the second app before the first one so that the quartz scheduler running
    // inside it can be properly shutdown
    IOHelper.close(app2, app);
}
Also used : CamelContext(org.apache.camel.CamelContext) CronTrigger(org.quartz.CronTrigger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AbstractXmlApplicationContext(org.springframework.context.support.AbstractXmlApplicationContext) SimpleTrigger(org.quartz.SimpleTrigger) Test(org.junit.Test)

Example 10 with SimpleTrigger

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

the class QuartzEndpointConfigureTest method testConfigureGroupAndName.

@Test
public void testConfigureGroupAndName() throws Exception {
    QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz://myGroup/myName?trigger.repeatCount=3");
    Trigger trigger = endpoint.getTrigger();
    assertEquals("getName()", "myName", trigger.getName());
    assertEquals("getGroup()", "myGroup", trigger.getGroup());
    // default job name
    assertEquals("getJobName", "quartz-" + endpoint.getId(), endpoint.getJobName());
    SimpleTrigger simpleTrigger = assertIsInstanceOf(SimpleTrigger.class, trigger);
    assertEquals("getRepeatCount()", 3, simpleTrigger.getRepeatCount());
}
Also used : Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger) Test(org.junit.Test)

Aggregations

SimpleTrigger (org.quartz.SimpleTrigger)37 SchedulerException (org.quartz.SchedulerException)18 JobDetail (org.quartz.JobDetail)14 CronTrigger (org.quartz.CronTrigger)11 Scheduler (org.quartz.Scheduler)11 Date (java.util.Date)8 Test (org.junit.Test)8 Trigger (org.quartz.Trigger)6 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)5 JobDataMap (org.quartz.JobDataMap)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DatasetBean (org.akaza.openclinica.bean.extract.DatasetBean)4 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)4 DatasetDAO (org.akaza.openclinica.dao.extract.DatasetDAO)4 CamelContext (org.apache.camel.CamelContext)4 AbstractXmlApplicationContext (org.springframework.context.support.AbstractXmlApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 JobExecutionException (org.quartz.JobExecutionException)3