Search in sources :

Example 1 with SchedulerFactory

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

the class QuartzSupportTests method schedulerFactoryBeanWithApplicationContext.

@Test
public void schedulerFactoryBeanWithApplicationContext() throws Exception {
    TestBean tb = new TestBean("tb", 99);
    StaticApplicationContext ac = new StaticApplicationContext();
    final Scheduler scheduler = mock(Scheduler.class);
    SchedulerContext schedulerContext = new SchedulerContext();
    given(scheduler.getContext()).willReturn(schedulerContext);
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {

        @Override
        protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) {
            return scheduler;
        }
    };
    schedulerFactoryBean.setJobFactory(null);
    Map<String, Object> schedulerContextMap = new HashMap<>();
    schedulerContextMap.put("testBean", tb);
    schedulerFactoryBean.setSchedulerContextAsMap(schedulerContextMap);
    schedulerFactoryBean.setApplicationContext(ac);
    schedulerFactoryBean.setApplicationContextSchedulerContextKey("appCtx");
    try {
        schedulerFactoryBean.afterPropertiesSet();
        schedulerFactoryBean.start();
        Scheduler returnedScheduler = schedulerFactoryBean.getObject();
        assertEquals(tb, returnedScheduler.getContext().get("testBean"));
        assertEquals(ac, returnedScheduler.getContext().get("appCtx"));
    } finally {
        schedulerFactoryBean.destroy();
    }
    verify(scheduler).start();
    verify(scheduler).shutdown(false);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HashMap(java.util.HashMap) Scheduler(org.quartz.Scheduler) SchedulerContext(org.quartz.SchedulerContext) SchedulerFactory(org.quartz.SchedulerFactory) Test(org.junit.Test)

Example 2 with SchedulerFactory

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

the class QuartzComponent method createSchedulerFactory.

private 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");
        prop.put("org.terracotta.quartz.skipUpdateCheck", "true");
        // camel context name will be a suffix to use one scheduler per context
        if (isPrefixInstanceName()) {
            String instName = createInstanceName(prop);
            prop.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, instName);
        }
        if (isInterruptJobsOnShutdown()) {
            prop.setProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN, "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);
    } 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
        if (isPrefixInstanceName()) {
            // 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");
        prop.put("org.terracotta.quartz.skipUpdateCheck", "true");
        if (isInterruptJobsOnShutdown()) {
            prop.setProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN, "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) Properties(java.util.Properties) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Example 3 with SchedulerFactory

use of org.quartz.SchedulerFactory in project deltaspike by apache.

the class AbstractQuartzScheduler method start.

@Override
public void start() {
    if (this.scheduler != null) {
        throw new UnsupportedOperationException("the scheduler is started already");
    }
    SchedulerFactory schedulerFactory = null;
    try {
        Properties properties = new Properties();
        properties.put(StdSchedulerFactory.PROP_SCHED_JOB_FACTORY_CLASS, CdiAwareJobFactory.class.getName());
        try {
            ResourceBundle config = loadCustomQuartzConfig();
            Enumeration<String> keys = config.getKeys();
            String key;
            while (keys.hasMoreElements()) {
                key = keys.nextElement();
                properties.put(key, config.getString(key));
            }
        } catch (Exception e1) {
            LOG.info("no custom quartz-config file found. falling back to the default config provided by quartz.");
            InputStream inputStream = null;
            try {
                inputStream = ClassUtils.getClassLoader(null).getResourceAsStream("org/quartz/quartz.properties");
                properties.load(inputStream);
            } catch (Exception e2) {
                LOG.warning("failed to load quartz default-config");
                schedulerFactory = new StdSchedulerFactory();
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        }
        if (schedulerFactory == null) {
            schedulerFactory = new StdSchedulerFactory(properties);
        }
    } catch (Exception e) {
        LOG.log(Level.WARNING, "fallback to default scheduler-factory", e);
        schedulerFactory = new StdSchedulerFactory();
    }
    try {
        this.scheduler = schedulerFactory.getScheduler();
        if (SchedulerBaseConfig.LifecycleIntegration.START_SCOPES_PER_JOB) {
            this.scheduler.getListenerManager().addJobListener(new InjectionAwareJobListener());
        }
        if (!this.scheduler.isStarted()) {
            this.scheduler.startDelayed(SchedulerBaseConfig.LifecycleIntegration.DELAYED_START_IN_SECONDS);
        }
    } catch (SchedulerException e) {
        throw ExceptionUtils.throwAsRuntimeException(e);
    }
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerException(org.quartz.SchedulerException) InputStream(java.io.InputStream) ResourceBundle(java.util.ResourceBundle) Properties(java.util.Properties) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Example 4 with SchedulerFactory

use of org.quartz.SchedulerFactory in project searchcode-server by boyter.

the class Singleton method getScheduler.

public static synchronized Scheduler getScheduler() {
    try {
        if (scheduler == null || scheduler.isShutdown()) {
            try {
                SchedulerFactory sf = new StdSchedulerFactory();
                scheduler = sf.getScheduler();
            } catch (SchedulerException ex) {
                Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
            }
        }
    } catch (SchedulerException e) {
    }
    return scheduler;
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerException(org.quartz.SchedulerException) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Example 5 with SchedulerFactory

use of org.quartz.SchedulerFactory in project engine by craftercms.

the class SchedulingUtils method createScheduler.

public static Scheduler createScheduler(String schedulerName) throws SchedulerException {
    Properties props = new Properties();
    props.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, schedulerName);
    props.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
    props.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
    SchedulerFactory schedulerFactory = new StdSchedulerFactory(props);
    return schedulerFactory.getScheduler();
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Properties(java.util.Properties) SimpleThreadPool(org.quartz.simpl.SimpleThreadPool) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Aggregations

SchedulerFactory (org.quartz.SchedulerFactory)9 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)8 SchedulerException (org.quartz.SchedulerException)4 InputStream (java.io.InputStream)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 Scheduler (org.quartz.Scheduler)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)1 ResourceBundle (java.util.ResourceBundle)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerContext (org.quartz.SchedulerContext)1 RemoteScheduler (org.quartz.impl.RemoteScheduler)1 SimpleThreadPool (org.quartz.simpl.SimpleThreadPool)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 TestBean (org.springframework.tests.sample.beans.TestBean)1