Java Code Examples for org.springframework.scheduling.quartz.SchedulerFactoryBean#setTriggers()

The following examples show how to use org.springframework.scheduling.quartz.SchedulerFactoryBean#setTriggers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QuartzSchedulerConfig.java    From mojito with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the scheduler with triggers/jobs defined in spring beans.
 * <p>
 * The spring beans should use the default group so that it is easy to keep track of new or removed triggers/jobs.
 * <p>
 * In {@link #startScheduler()} triggers/jobs present in Quartz but without a matching spring bean will be
 * removed.
 * <p>
 * Other job and trigger created dynamically must not used the default group else they'll be removed.
 *
 * @return
 * @throws SchedulerException
 */
@Bean
public SchedulerFactoryBean scheduler() throws SchedulerException {

    logger.info("Create SchedulerFactoryBean");

    Properties quartzProperties = quartzPropertiesConfig.getQuartzProperties();

    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();

    String dataSource = quartzProperties.getProperty("org.quartz.jobStore.dataSource");
    schedulerFactory.setQuartzProperties(quartzProperties);
    schedulerFactory.setJobFactory(springBeanJobFactory());
    schedulerFactory.setOverwriteExistingJobs(true);
    schedulerFactory.setTriggers(triggers.toArray(new Trigger[]{}));
    schedulerFactory.setAutoStartup(false);

    return schedulerFactory;
}
 
Example 2
Source File: SpringQrtzScheduler.java    From tutorials with MIT License 6 votes vote down vote up
@Bean
public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job, DataSource quartzDataSource) {

    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
    schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));

    logger.debug("Setting the Scheduler up");
    schedulerFactory.setJobFactory(springBeanJobFactory());
    schedulerFactory.setJobDetails(job);
    schedulerFactory.setTriggers(trigger);

    // Comment the following line to use the default Quartz job store.
    schedulerFactory.setDataSource(quartzDataSource);

    return schedulerFactory;
}
 
Example 3
Source File: ApiBootQuartzAutoConfiguration.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SchedulerFactoryBean quartzScheduler() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
    jobFactory.setApplicationContext(this.applicationContext);
    schedulerFactoryBean.setJobFactory(jobFactory);
    if (this.properties.getSchedulerName() != null) {
        schedulerFactoryBean.setSchedulerName(this.properties.getSchedulerName());
    }

    schedulerFactoryBean.setAutoStartup(this.properties.isAutoStartup());
    schedulerFactoryBean.setStartupDelay((int) this.properties.getStartupDelay().getSeconds());
    schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(this.properties.isWaitForJobsToCompleteOnShutdown());
    schedulerFactoryBean.setOverwriteExistingJobs(this.properties.isOverwriteExistingJobs());
    if (!this.properties.getProperties().isEmpty()) {
        schedulerFactoryBean.setQuartzProperties(this.asProperties(this.properties.getProperties()));
    }

    if (this.jobDetails != null && this.jobDetails.length > 0) {
        schedulerFactoryBean.setJobDetails(this.jobDetails);
    }

    if (this.calendars != null && !this.calendars.isEmpty()) {
        schedulerFactoryBean.setCalendars(this.calendars);
    }

    if (this.triggers != null && this.triggers.length > 0) {
        schedulerFactoryBean.setTriggers(this.triggers);
    }

    this.customize(schedulerFactoryBean);
    return schedulerFactoryBean;
}
 
Example 4
Source File: JobQuartzAutoConfiguration.java    From spring-boot-starter-micro-job with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SchedulerFactoryBean quartzScheduler() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
    jobFactory.setApplicationContext(this.applicationContext);
    schedulerFactoryBean.setJobFactory(jobFactory);
    if (this.properties.getSchedulerName() != null) {
        schedulerFactoryBean.setSchedulerName(this.properties.getSchedulerName());
    }

    schedulerFactoryBean.setAutoStartup(this.properties.isAutoStartup());
    schedulerFactoryBean.setStartupDelay((int) this.properties.getStartupDelay().getSeconds());
    schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(this.properties.isWaitForJobsToCompleteOnShutdown());
    schedulerFactoryBean.setOverwriteExistingJobs(this.properties.isOverwriteExistingJobs());
    if (!this.properties.getProperties().isEmpty()) {
        schedulerFactoryBean.setQuartzProperties(this.asProperties(this.properties.getProperties()));
    }

    if (this.jobDetails != null && this.jobDetails.length > 0) {
        schedulerFactoryBean.setJobDetails(this.jobDetails);
    }

    if (this.calendars != null && !this.calendars.isEmpty()) {
        schedulerFactoryBean.setCalendars(this.calendars);
    }

    if (this.triggers != null && this.triggers.length > 0) {
        schedulerFactoryBean.setTriggers(this.triggers);
    }

    this.customize(schedulerFactoryBean);
    return schedulerFactoryBean;
}
 
Example 5
Source File: QuartzConfigration.java    From Tbed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) {
    SchedulerFactoryBean bean = new SchedulerFactoryBean();
    // 延时启动,应用启动1秒后
    bean.setStartupDelay(1);
    // 注册触发器
    bean.setTriggers(firstTrigger, secondTrigger);
    return bean;
}
 
Example 6
Source File: ApiBootQuartzAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SchedulerFactoryBean quartzScheduler() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
    jobFactory.setApplicationContext(this.applicationContext);
    schedulerFactoryBean.setJobFactory(jobFactory);
    if (this.properties.getSchedulerName() != null) {
        schedulerFactoryBean.setSchedulerName(this.properties.getSchedulerName());
    }

    schedulerFactoryBean.setAutoStartup(this.properties.isAutoStartup());
    schedulerFactoryBean.setStartupDelay((int) this.properties.getStartupDelay().getSeconds());
    schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(this.properties.isWaitForJobsToCompleteOnShutdown());
    schedulerFactoryBean.setOverwriteExistingJobs(this.properties.isOverwriteExistingJobs());
    if (!this.properties.getProperties().isEmpty()) {
        schedulerFactoryBean.setQuartzProperties(this.asProperties(this.properties.getProperties()));
    }

    if (this.jobDetails != null && this.jobDetails.length > 0) {
        schedulerFactoryBean.setJobDetails(this.jobDetails);
    }

    if (this.calendars != null && !this.calendars.isEmpty()) {
        schedulerFactoryBean.setCalendars(this.calendars);
    }

    if (this.triggers != null && this.triggers.length > 0) {
        schedulerFactoryBean.setTriggers(this.triggers);
    }

    this.customize(schedulerFactoryBean);
    return schedulerFactoryBean;
}
 
Example 7
Source File: PacmanQuartzConfiguration.java    From pacbot with Apache License 2.0 5 votes vote down vote up
@Bean
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JobFactory jobFactory) throws IOException {
	SchedulerFactoryBean factory = new SchedulerFactoryBean();
	factory.setOverwriteExistingJobs(true);
	factory.setDataSource(dataSource);
	factory.setJobFactory(jobFactory);
	factory.setQuartzProperties(quartzProperties());

	// Here we will set all the trigger beans we have defined.
	if (!NotificationUtil.isObjectEmpty(listOfTrigger)) {
		factory.setTriggers(listOfTrigger.toArray(new Trigger[listOfTrigger.size()]));
	}

	return factory;
}
 
Example 8
Source File: QuartzConfigration.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Bean(name = "scheduler")  
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {  
    SchedulerFactoryBean bean = new SchedulerFactoryBean();
    //设置是否任意一个已定义的Job会覆盖现在的Job。默认为false,即已定义的Job不会覆盖现有的Job。
    bean.setOverwriteExistingJobs(true);  
    // 延时启动,应用启动5秒后  ,定时器才开始启动
    bean.setStartupDelay(5);  
    // 注册定时触发器  
    bean.setTriggers(cronJobTrigger);  
    return bean;  
}
 
Example 9
Source File: SchedulerConfig.java    From quartz-manager with Apache License 2.0 5 votes vote down vote up
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory,
		@Qualifier("jobTrigger") Trigger sampleJobTrigger) throws IOException {
	SchedulerFactoryBean factory = new SchedulerFactoryBean();
	factory.setJobFactory(jobFactory);
	factory.setQuartzProperties(quartzProperties());
	factory.setTriggers(sampleJobTrigger);
	factory.setAutoStartup(false);
	return factory;
}
 
Example 10
Source File: QuartzSchedulerAutoConfiguration.java    From spring-boot-starter-quartz with Apache License 2.0 4 votes vote down vote up
@Bean(name = QUARTZ_SCHEDULER_FACTORY_BEAN_NAME)
@ConditionalOnMissingBean
public SchedulerFactoryBean autoSchedulerFactory(ApplicationContext applicationContext, JobFactory jobFactory,
		@Autowired(required=false) QuartzSchedulerProperties properties,
		@Qualifier(QUARTZ_PROPERTIES_BEAN_NAME) Properties quartzProperties,
		@Autowired(required=false) List<TriggerListener> triggerListeners,
		@Autowired(required=false) List<JobListener> jobListeners,
		@Autowired(required=false) List<SchedulerListener> schedulerListeners) {
	
	if (null == properties) {
		LOGGER.warn("no QuartzSchedulerProperties found, consider to set quartz.enabled=true in properties");
		return null;
	}
	
	LOGGER.debug("creating SchedulerFactory");
	 
       SchedulerFactory factorySettings = properties.getSchedulerFactory();
	SchedulerRepository schedulerRepo = SchedulerRepository.getInstance();
	if (schedulerRepo.remove(QUARTZ_SCHEDULER_FACTORY_BEAN_NAME)) {
		LOGGER.debug("removed scheduler from SchedulerRepository with name: " + QUARTZ_SCHEDULER_FACTORY_BEAN_NAME);
	}
	if (null != factorySettings.getSchedulerName() && schedulerRepo.remove(factorySettings.getSchedulerName())) {
		LOGGER.debug("removed scheduler from SchedulerRepository with name: " + factorySettings.getSchedulerName());
	}
	
	SchedulerFactoryBean factory = BeanUtils.instantiateClass(SchedulerFactoryBean.class);
	
       factory.setApplicationContext(applicationContext);
       factory.setJobFactory(jobFactory);
       
       Persistence persistenceSettings = properties.getPersistence();
       if (persistenceSettings.isPersisted()) {
       	factory.setDataSource(getDataSource(applicationContext, persistenceSettings));
       	if (persistenceSettings.isUsePlatformTxManager()) {
       		PlatformTransactionManager txManager = getTransactionManager(applicationContext, persistenceSettings.getPlatformTxManagerBeanName());
           	if (null != txManager) {
               	factory.setTransactionManager(txManager);
               }
       	}
       }
       
       if (!StringUtils.isEmpty(factorySettings.getSchedulerName())) {
       	factory.setSchedulerName(factorySettings.getSchedulerName());
       } else {
       	LOGGER.debug("no SchedulerName configured, using bean name: " + QUARTZ_SCHEDULER_FACTORY_BEAN_NAME);
       }
       factory.setPhase(factorySettings.getPhase());
       factory.setStartupDelay(factorySettings.getStartupDelay());
       factory.setAutoStartup(factorySettings.isAutoStartup());
       factory.setWaitForJobsToCompleteOnShutdown(factorySettings.isWaitForJobsToCompleteOnShutdown());
       factory.setOverwriteExistingJobs(factorySettings.isOverwriteExistingJobs());
       factory.setExposeSchedulerInRepository(factorySettings.isExposeSchedulerInRepository());
       
       factory.setQuartzProperties(quartzProperties);
       
       if (!CollectionUtils.isEmpty(jobListeners)) {
       	LOGGER.info("configuring " + jobListeners.size() + " job listeners");
       	factory.setGlobalJobListeners(jobListeners.toArray(new JobListener[]{}));
       }
       if (!CollectionUtils.isEmpty(triggerListeners)) {
       	LOGGER.info("configuring " + triggerListeners.size() + " trigger listeners");
       	factory.setGlobalTriggerListeners(triggerListeners.toArray(new TriggerListener[]{}));
       }
       if (!CollectionUtils.isEmpty(schedulerListeners)) {
       	LOGGER.info("configuring " + schedulerListeners.size() + " scheduler listeners");
       	factory.setSchedulerListeners(schedulerListeners.toArray(new SchedulerListener[]{}));
       }
       
       Collection<Trigger> triggers = getTriggers(applicationContext);
       if (null != triggers && !triggers.isEmpty()) {
       	factory.setTriggers(triggers.toArray(new Trigger[triggers.size()]));
       	LOGGER.info("staring scheduler factory with " + triggers.size() + " job triggers");
       } else {
       	LOGGER.info("staring scheduler factory with 0 job triggers");
       }
       
       QuartzSchedulerFactoryOverrideHook hook = getQuartzSchedulerFactoryOverrideHook(applicationContext);
       if (null != hook) {
       	factory = hook.override(factory, properties, quartzProperties);
       }
       
	return factory;
}