Search in sources :

Example 36 with StdSchedulerFactory

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

the class SchedulerFactoryBean method initSchedulerFactory.

/**
	 * Load and/or apply Quartz properties to the given SchedulerFactory.
	 * @param schedulerFactory the SchedulerFactory to initialize
	 */
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {
    if (!(schedulerFactory instanceof StdSchedulerFactory)) {
        if (this.configLocation != null || this.quartzProperties != null || this.taskExecutor != null || this.dataSource != null) {
            throw new IllegalArgumentException("StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);
        }
        // Otherwise assume that no initialization is necessary...
        return;
    }
    Properties mergedProps = new Properties();
    if (this.resourceLoader != null) {
        mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS, ResourceLoaderClassLoadHelper.class.getName());
    }
    if (this.taskExecutor != null) {
        mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, LocalTaskExecutorThreadPool.class.getName());
    } else {
        // Set necessary default properties here, as Quartz will not apply
        // its default configuration when explicitly given properties.
        mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
        mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
    }
    if (this.configLocation != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading Quartz config from [" + this.configLocation + "]");
        }
        PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
    }
    CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
    if (this.dataSource != null) {
        mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
    }
    // Make sure to set the scheduler name as configured in the Spring configuration.
    if (this.schedulerName != null) {
        mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
    }
    ((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Properties(java.util.Properties) SimpleThreadPool(org.quartz.simpl.SimpleThreadPool)

Example 37 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project midpoint by Evolveum.

the class LocalNodeManager method initializeScheduler.

/*
    * =============== SCHEDULER-LEVEL ACTIONS ===============
    *
    * (used internally by TaskManager)
    */
/**
     * Prepares Quartz scheduler. Configures its properties (based on Task Manager configuration) and creates the instance.
     * Does not start the scheduler, because this is done during post initialization.
     *
     * @throws com.evolveum.midpoint.task.api.TaskManagerInitializationException
     */
void initializeScheduler() throws TaskManagerInitializationException {
    TaskManagerConfiguration configuration = taskManager.getConfiguration();
    Properties quartzProperties = new Properties();
    if (configuration.isJdbcJobStore()) {
        quartzProperties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        quartzProperties.put("org.quartz.jobStore.driverDelegateClass", configuration.getJdbcDriverDelegateClass());
        createQuartzDbSchema(configuration);
        String MY_DS = "myDS";
        quartzProperties.put("org.quartz.jobStore.dataSource", MY_DS);
        if (configuration.getDataSource() != null) {
            quartzProperties.put("org.quartz.dataSource." + MY_DS + ".jndiURL", configuration.getDataSource());
        } else {
            quartzProperties.put("org.quartz.dataSource." + MY_DS + ".driver", configuration.getJdbcDriver());
            quartzProperties.put("org.quartz.dataSource." + MY_DS + ".URL", configuration.getJdbcUrl());
            quartzProperties.put("org.quartz.dataSource." + MY_DS + ".user", configuration.getJdbcUser());
            quartzProperties.put("org.quartz.dataSource." + MY_DS + ".password", configuration.getJdbcPassword());
        }
        quartzProperties.put("org.quartz.jobStore.isClustered", configuration.isClustered() ? "true" : "false");
    } else {
        quartzProperties.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");
    }
    quartzProperties.put("org.quartz.scheduler.instanceName", "midPointScheduler");
    quartzProperties.put("org.quartz.scheduler.instanceId", taskManager.getNodeId());
    quartzProperties.put("org.quartz.scheduler.skipUpdateCheck", "true");
    quartzProperties.put("org.quartz.threadPool.threadCount", Integer.toString(configuration.getThreads()));
    // in test mode we set idleWaitTime to a lower value, because on some occasions
    // the Quartz scheduler "forgots" to fire a trigger immediately after creation,
    // and the default delay of 10s is too much for most of the tests.
    int schedulerLoopTime;
    if (configuration.isTestMode()) {
        if (configuration.isJdbcJobStore()) {
            schedulerLoopTime = 5000;
        } else {
            schedulerLoopTime = 2000;
        }
    } else {
        schedulerLoopTime = 10000;
    }
    quartzProperties.put("org.quartz.scheduler.idleWaitTime", Integer.toString(schedulerLoopTime));
    quartzProperties.put("org.quartz.scheduler.jmx.export", "true");
    if (configuration.isTestMode()) {
        LOGGER.info("ReusableQuartzScheduler is set: the task manager threads will NOT be stopped on shutdown. Also, scheduler threads will run as daemon ones.");
        quartzProperties.put("org.quartz.scheduler.makeSchedulerThreadDaemon", "true");
        quartzProperties.put("org.quartz.threadPool.makeThreadsDaemons", "true");
    }
    // initialize the scheduler (without starting it)
    try {
        LOGGER.trace("Quartz scheduler properties: {}", quartzProperties);
        StdSchedulerFactory sf = new StdSchedulerFactory();
        sf.initialize(quartzProperties);
        getGlobalExecutionManager().setQuartzScheduler(sf.getScheduler());
    } catch (SchedulerException e) {
        throw new TaskManagerInitializationException("Cannot initialize the Quartz scheduler", e);
    }
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) TaskManagerConfiguration(com.evolveum.midpoint.task.quartzimpl.TaskManagerConfiguration)

Example 38 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project camel by apache.

the class QuartzComponent method createSchedulerFactory.

protected SchedulerFactory createSchedulerFactory() throws SchedulerException {
    SchedulerFactory answer;
    Properties prop = loadProperties();
    if (prop != null) {
        // force disabling update checker (will do online check over the internet)
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        // camel context name will be a suffix to use one scheduler per context
        String instName = createInstanceName(prop);
        prop.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, instName);
        // enable jmx unless configured to not do so
        if (enableJmx && !prop.containsKey("org.quartz.scheduler.jmx.export")) {
            LOG.info("Setting org.quartz.scheduler.jmx.export=true to ensure QuartzScheduler(s) will be enlisted in JMX.");
            prop.put("org.quartz.scheduler.jmx.export", "true");
        }
        answer = new StdSchedulerFactory(prop);
    } else {
        // read default props to be able to use a single scheduler per camel context
        // if we need more than one scheduler per context use setScheduler(Scheduler) 
        // or setFactory(SchedulerFactory) methods
        // must use classloader from StdSchedulerFactory to work even in OSGi
        InputStream is = StdSchedulerFactory.class.getClassLoader().getResourceAsStream("org/quartz/quartz.properties");
        if (is == null) {
            throw new SchedulerException("Quartz properties file not found in classpath: org/quartz/quartz.properties");
        }
        prop = new Properties();
        try {
            prop.load(is);
        } catch (IOException e) {
            throw new SchedulerException("Error loading Quartz properties file from classpath: org/quartz/quartz.properties", e);
        } finally {
            IOHelper.close(is);
        }
        // camel context name will be a suffix to use one scheduler per context
        String instName = createInstanceName(prop);
        prop.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, instName);
        // force disabling update checker (will do online check over the internet)
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        // enable jmx unless configured to not do so
        if (enableJmx && !prop.containsKey("org.quartz.scheduler.jmx.export")) {
            prop.put("org.quartz.scheduler.jmx.export", "true");
            LOG.info("Setting org.quartz.scheduler.jmx.export=true to ensure QuartzScheduler(s) will be enlisted in JMX.");
        }
        answer = new StdSchedulerFactory(prop);
    }
    if (LOG.isDebugEnabled()) {
        String name = prop.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME);
        LOG.debug("Creating SchedulerFactory: {} with properties: {}", name, prop);
    }
    return answer;
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerException(org.quartz.SchedulerException) InputStream(java.io.InputStream) IOException(java.io.IOException) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Example 39 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory 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 40 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory 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.setSchedulerFactory(fac);
    assertSame(fac, comp.getSchedulerFactory());
    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)

Aggregations

StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)66 SchedulerFactory (org.quartz.SchedulerFactory)29 SchedulerException (org.quartz.SchedulerException)22 Scheduler (org.quartz.Scheduler)19 JobDetail (org.quartz.JobDetail)17 Logger (org.slf4j.Logger)14 Date (java.util.Date)11 Properties (java.util.Properties)11 Trigger (org.quartz.Trigger)11 JobKey (org.quartz.JobKey)7 JobDataMap (org.quartz.JobDataMap)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 DateBuilder.nextGivenSecondDate (org.quartz.DateBuilder.nextGivenSecondDate)4 SimpleTrigger (org.quartz.SimpleTrigger)4 ParseException (java.text.ParseException)3 CronTrigger (org.quartz.CronTrigger)3 SimpleThreadPool (org.quartz.simpl.SimpleThreadPool)3 QuartzManager (com.example.quartz.QuartzManager)2