Search in sources :

Example 1 with SchedulerFactoryBean

use of org.springframework.scheduling.quartz.SchedulerFactoryBean in project herd by FINRAOS.

the class ServiceSpringModuleConfig method quartzScheduler.

/**
 * Gets a Quartz scheduler factory bean that can return a Quartz scheduler.
 *
 * @return the Quartz scheduler factory bean.
 * @throws Exception if the bean couldn't be created.
 */
@Bean
public SchedulerFactoryBean quartzScheduler() throws Exception {
    SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
    AutowiringQuartzSpringBeanJobFactory jobFactory = new AutowiringQuartzSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    quartzScheduler.setJobFactory(jobFactory);
    // Name our scheduler.
    quartzScheduler.setSchedulerName("herdScheduler");
    // Setup the scheduler to use Spring’s dataSource and transactionManager.
    quartzScheduler.setDataSource(herdDataSource);
    quartzScheduler.setTransactionManager(herdTransactionManager);
    // Create the Quartz tables for JUnits.
    if (shouldCreateQuartzTables()) {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("createQuartzTables.sql"));
        // This is what the DataSourceInitializer does.
        DatabasePopulatorUtils.execute(resourceDatabasePopulator, herdDataSource);
    }
    // Set quartz properties. Please note that Spring uses LocalDataSourceJobStore extension of JobStoreCMT.
    Properties quartzProperties = new Properties();
    quartzScheduler.setQuartzProperties(quartzProperties);
    // Configure Main Main Scheduler Properties. The "instance" parameters are needed to manage cluster instances.
    quartzProperties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "herdSystemJobScheduler");
    quartzProperties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, StdSchedulerFactory.AUTO_GENERATE_INSTANCE_ID);
    // Configure ThreadPool.
    quartzProperties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, "org.quartz.simpl.SimpleThreadPool");
    quartzProperties.setProperty("org.quartz.threadPool.threadCount", configurationHelper.getProperty(ConfigurationValue.SYSTEM_JOBS_THREAD_POOL_THREAD_COUNT));
    quartzProperties.setProperty("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
    // Configure JobStore.
    quartzProperties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
    quartzProperties.setProperty("org.quartz.jobStore.isClustered", "true");
    quartzProperties.setProperty(ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS.getKey(), getQuartzDatabaseDelegateClass());
    // Build a list of system jobs to be scheduled.
    Map<String, AbstractSystemJob> systemJobs = applicationContext.getBeansOfType(AbstractSystemJob.class);
    List<JobDetail> jobDetails = new ArrayList<>();
    List<CronTrigger> triggers = new ArrayList<>();
    for (Map.Entry<String, AbstractSystemJob> entry : systemJobs.entrySet()) {
        // Prepare job detail and trigger for the system job.
        String jobName = entry.getKey();
        AbstractSystemJob systemJob = entry.getValue();
        JobDetail jobDetail = newJob(systemJob.getClass()).withIdentity(jobName).storeDurably().requestRecovery().build();
        TriggerKey jobTriggerKey = TriggerKey.triggerKey(jobName + AbstractSystemJob.CRON_TRIGGER_SUFFIX);
        CronTrigger trigger = newTrigger().withIdentity(jobTriggerKey).forJob(jobName).usingJobData(systemJob.getJobDataMap()).withSchedule(cronSchedule(systemJob.getCronExpression())).build();
        // Add this system job to the list of jobs/triggers to be scheduled.
        jobDetails.add(jobDetail);
        triggers.add(trigger);
    }
    // Schedule the system jobs and set a flag to overwrite already registered jobs that would be read in from the database (persistent job store).
    quartzScheduler.setOverwriteExistingJobs(true);
    quartzScheduler.setJobDetails(jobDetails.toArray(new JobDetail[jobDetails.size()]));
    quartzScheduler.setTriggers(triggers.toArray(new CronTrigger[triggers.size()]));
    return quartzScheduler;
}
Also used : CronTrigger(org.quartz.CronTrigger) AbstractSystemJob(org.finra.herd.service.systemjobs.AbstractSystemJob) ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) ArrayList(java.util.ArrayList) AutowiringQuartzSpringBeanJobFactory(org.finra.herd.core.AutowiringQuartzSpringBeanJobFactory) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Map(java.util.Map) HashMap(java.util.HashMap) ProcessEngineFactoryBean(org.activiti.spring.ProcessEngineFactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with SchedulerFactoryBean

use of org.springframework.scheduling.quartz.SchedulerFactoryBean in project syncope by apache.

the class SchedulerShutdown method destroy.

@Override
public void destroy() throws Exception {
    SchedulerFactoryBean scheduler = ctx.getBean(SchedulerFactoryBean.class);
    scheduler.getScheduler().shutdown();
}
Also used : SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean)

Example 3 with SchedulerFactoryBean

use of org.springframework.scheduling.quartz.SchedulerFactoryBean in project motech by motech.

the class MotechSchedulerFactoryBean method init.

/**
 * Creates the Spring {@code SchedulerFactoryBean}.
 */
@PostConstruct
public void init() {
    schedulerFactoryBean = new SchedulerFactoryBean();
    schedulerFactoryBean.setQuartzProperties(schedulerProperties);
    schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(getBooleanWithDefault(schedulerProperties.getProperty("scheduler.waitForJobsToCompleteOnShutdown"), true));
    schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
    schedulerFactoryBean.setApplicationContext(applicationContext);
    try {
        schedulerFactoryBean.afterPropertiesSet();
        schedulerFactoryBean.getScheduler().start();
    } catch (Exception e) {
        throw new SchedulerInstantiationException("Failed to instantiate scheduler with configuration from quartz.properties", e);
    }
}
Also used : SchedulerInstantiationException(org.motechproject.scheduler.exception.SchedulerInstantiationException) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) SchedulerInstantiationException(org.motechproject.scheduler.exception.SchedulerInstantiationException) SchedulerException(org.quartz.SchedulerException) SchedulerShutdownException(org.motechproject.scheduler.exception.SchedulerShutdownException) PostConstruct(javax.annotation.PostConstruct)

Example 4 with SchedulerFactoryBean

use of org.springframework.scheduling.quartz.SchedulerFactoryBean in project springBoot-learn-demo by nbfujx.

the class SchedulerConfig method schedulerFactoryBean.

@Bean(name = "SchedulerFactory")
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setQuartzProperties(quartzProperties());
    return factory;
}
Also used : SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Bean(org.springframework.context.annotation.Bean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean)

Example 5 with SchedulerFactoryBean

use of org.springframework.scheduling.quartz.SchedulerFactoryBean in project netxms by netxms.

the class AppContextConfig method quartzScheduler.

@Bean
public SchedulerFactoryBean quartzScheduler() {
    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
    final Properties mergedProperties = new Properties();
    mergedProperties.putAll(loadProperties("org/quartz/quartz.properties"));
    ServerSettings.DataSourceConfig dataSourceConfig = settings.getDataSourceConfig(ServerSettings.DC_ID_SCHEDULER);
    Properties properties = new Properties();
    properties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
    properties.put("org.quartz.jobStore.driverDelegateClass", dataSourceConfig.getQuartzDriverDelegate());
    properties.put("org.quartz.jobStore.dataSource", "myDS");
    properties.put("org.quartz.dataSource.myDS.driver", dataSourceConfig.getDriver());
    properties.put("org.quartz.dataSource.myDS.URL", dataSourceConfig.getUrl());
    properties.put("org.quartz.dataSource.myDS.user", dataSourceConfig.getUsername());
    properties.put("org.quartz.dataSource.myDS.password", dataSourceConfig.getPassword());
    properties.put("org.quartz.dataSource.myDS.maxConnections", "20");
    properties.put("org.quartz.scheduler.skipUpdateCheck", "true");
    mergedProperties.putAll(properties);
    schedulerFactory.setQuartzProperties(mergedProperties);
    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    schedulerFactory.setJobFactory(jobFactory);
    return schedulerFactory;
}
Also used : ServerSettings(com.radensolutions.reporting.service.ServerSettings) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Properties(java.util.Properties) Bean(org.springframework.context.annotation.Bean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean)

Aggregations

SchedulerFactoryBean (org.springframework.scheduling.quartz.SchedulerFactoryBean)28 Bean (org.springframework.context.annotation.Bean)20 PropertiesFactoryBean (org.springframework.beans.factory.config.PropertiesFactoryBean)7 ClassPathResource (org.springframework.core.io.ClassPathResource)6 IOException (java.io.IOException)5 Properties (java.util.Properties)5 Test (org.junit.jupiter.api.Test)3 JobDetail (org.quartz.JobDetail)3 Trigger (org.quartz.Trigger)3 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)2 Map (java.util.Map)2 PostConstruct (javax.annotation.PostConstruct)2 SchedulerException (org.quartz.SchedulerException)2 ServerSettings (com.radensolutions.reporting.service.ServerSettings)1 Job (com.weicoder.frame.quartz.Job)1 SchedulerManager (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulerManager)1 DefaultSchedulerManager (eu.bcvsolutions.idm.core.scheduler.service.impl.DefaultSchedulerManager)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1