GoogleCloudPlatform / spring-cloud-gcp

New home for Spring Cloud GCP development starting with version 2.0.
Apache License 2.0
415 stars 307 forks source link

More than one TaskScheduler when using pub/sub and scheduling together #2454

Closed alexandreBaronZnk closed 5 months ago

alexandreBaronZnk commented 9 months ago

With the following dependencies :

When using the dependency spring-cloud-gcp-starter-pubsub and activate the scheduling with this Spring annotation @EnableScheduling, the following log is displayed in the console :

2023-12-13T15:08:54.997+01:00  INFO 25360 --- [my-app] [           main] [                                                 ] o.s.s.config.TaskSchedulerRouter         : 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: [pubsubPublisherThreadPool, globalPubSubSubscriberThreadPoolScheduler]

With the debug activated on Spring Boot startup, the following explaination is displayed.

   TaskSchedulingConfigurations.TaskSchedulerConfiguration:
      Did not match:
         - @ConditionalOnMissingBean (types: org.springframework.scheduling.TaskScheduler,java.util.concurrent.ScheduledExecutorService; SearchStrategy: all) found beans of type 'org.springframework.scheduling.TaskScheduler' pubsubPublisherThreadPool (OnBeanCondition)

IMHO, a bean named taskScheduler must be created even if spring-cloud-gcp-starter-pubsub is present. And thread pools for scheduling and pub/sub interactions should be distincts. Perhaps, just add the annotation @AutoConfigureAfter(TaskSchedulingAutoConfiguration.class) to the GcpPubSubAutoConfiguration should be enough to have all thread pools created.

mpeddada1 commented 9 months ago

@alexandreBaronZnk Thanks for creating this issue! Do you happen to have a reproducer that we can try out to observe this behavior?

alexandreBaronZnk commented 8 months ago

@mpeddada1 I've create this project that reproduce the log (cf. https://github.com/alexandreBaronZnk/GoogleCloudPlatform-spring-cloud-gcp-issues-2454).

meltsufin commented 5 months ago

@alexandreBaronZnk Have you tried creating your own bean called taskScheduler? It seems to work with your sample project.

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(1);
        scheduler.setThreadNamePrefix("cron");
        scheduler.setDaemon(true);
        return scheduler;
    }
meltsufin commented 5 months ago

Closing for now, but please re-open, if you think a change should be made in Spring Cloud GCP.