Example usage for org.springframework.scheduling.annotation SchedulingConfigurer configureTasks

List of usage examples for org.springframework.scheduling.annotation SchedulingConfigurer configureTasks

Introduction

In this page you can find the example usage for org.springframework.scheduling.annotation SchedulingConfigurer configureTasks.

Prototype

void configureTasks(ScheduledTaskRegistrar taskRegistrar);

Source Link

Document

Callback allowing a org.springframework.scheduling.TaskScheduler TaskScheduler and specific org.springframework.scheduling.config.Task Task instances to be registered against the given the ScheduledTaskRegistrar .

Usage

From source file:org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.java

private void finishRegistration() {
    if (this.scheduler != null) {
        this.registrar.setScheduler(this.scheduler);
    }/*ww  w  .  j  a v a 2 s  . c o  m*/

    if (this.beanFactory instanceof ListableBeanFactory) {
        Map<String, SchedulingConfigurer> beans = ((ListableBeanFactory) this.beanFactory)
                .getBeansOfType(SchedulingConfigurer.class);
        List<SchedulingConfigurer> configurers = new ArrayList<>(beans.values());
        AnnotationAwareOrderComparator.sort(configurers);
        for (SchedulingConfigurer configurer : configurers) {
            configurer.configureTasks(this.registrar);
        }
    }

    if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
        try {
            // Search for TaskScheduler bean...
            this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, false));
        } catch (NoUniqueBeanDefinitionException ex) {
            logger.debug("Could not find unique TaskScheduler bean", ex);
            try {
                this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, true));
            } catch (NoSuchBeanDefinitionException ex2) {
                if (logger.isInfoEnabled()) {
                    logger.info("More than one TaskScheduler bean exists within the context, and "
                            + "none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' "
                            + "(possibly as an alias); or implement the SchedulingConfigurer interface and call "
                            + "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: "
                            + ex.getBeanNamesFound());
                }
            }
        } catch (NoSuchBeanDefinitionException ex) {
            logger.debug("Could not find default TaskScheduler bean", ex);
            // Search for ScheduledExecutorService bean next...
            try {
                this.registrar
                        .setScheduler(resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, false));
            } catch (NoUniqueBeanDefinitionException ex2) {
                logger.debug("Could not find unique ScheduledExecutorService bean", ex2);
                try {
                    this.registrar.setScheduler(
                            resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, true));
                } catch (NoSuchBeanDefinitionException ex3) {
                    if (logger.isInfoEnabled()) {
                        logger.info(
                                "More than one ScheduledExecutorService bean exists within the context, and "
                                        + "none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' "
                                        + "(possibly as an alias); or implement the SchedulingConfigurer interface and call "
                                        + "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: "
                                        + ex2.getBeanNamesFound());
                    }
                }
            } catch (NoSuchBeanDefinitionException ex2) {
                logger.debug("Could not find default ScheduledExecutorService bean", ex2);
                // Giving up -> falling back to default scheduler within the registrar...
                logger.info("No TaskScheduler/ScheduledExecutorService bean found for scheduled processing");
            }
        }
    }

    this.registrar.afterPropertiesSet();
}