Skip to content

When @Configuration class implements SchedulingConfigurer #115

Closed

Description

@bjornharvold

I cannot implement this functional interface with ShedLock. It throws an error complaining about trying to inject a proxy.

@Override
    public void configureTasks(ScheduledTaskRegistrar registrar) {
        registrar.setTaskScheduler(taskScheduler());
    }

My setup:

@Bean(name = "taskExecutor")
    @Primary
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setThreadNamePrefix("async-");
        executor.setAllowCoreThreadTimeOut(true);
        executor.setWaitForTasksToCompleteOnShutdown(true);

        return executor;
    }

    @Bean(name = "taskScheduler")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(50);
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setErrorHandler(t -> log.error(
                "Unknown error occurred while executing task.", t
        ));
        scheduler.setRejectedExecutionHandler(
                (r, e) -> log.error(
                        "Execution of task {} was rejected for unknown reasons.", r
                )
        );

        return scheduler;
    }

    @Bean(name = "scheduledLockConfiguration")
    public ScheduledLockConfiguration scheduledLockConfiguration(TaskScheduler taskScheduler, LockProvider lockProvider) {
        return ScheduledLockConfigurationBuilder
                .withLockProvider(lockProvider)
                .withTaskScheduler(taskScheduler)
                .withDefaultLockAtMostFor(Duration.ofMinutes(10))
                .build();
    }

    @Bean(name = "lockProvider")
    public LockProvider lockProvider(MongoClient mongo) {
        return new MongoLockProvider(mongo, "my_db");
    }

Activity

  1. lukas-krecan commented on Dec 18, 2018

    @lukas-krecan
    Owner

    Hi, can you please try newest version 2.2.0 with annotation based config (as described in readme)? It should work better than ScheduledLockConfiguration. Please, let me know if it helps.

  2. bjornharvold commented on Dec 19, 2018

    @bjornharvold
    Author

    Hi @lukas-krecan

    I actually had that annotation on there already but I also had the ScheduledLockConfiguration bean definition.

    This still fails:

    @Configuration
    @EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
    @Slf4j
    public class SchedulerConfig implements SchedulingConfigurer {
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar registrar) {
            registrar.setTaskScheduler(taskScheduler()); (Line: 45)
        }
    
        @Bean(name = "taskExecutor")
        @Primary
        public TaskExecutor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(20);
            executor.setThreadNamePrefix("traveliko-async-");
            executor.setAllowCoreThreadTimeOut(true);
            executor.setWaitForTasksToCompleteOnShutdown(true);
    
            return executor;
        }
    
        @Bean(name = "taskScheduler")
        public ThreadPoolTaskScheduler taskScheduler() {
            ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
            scheduler.setPoolSize(50);
            scheduler.setWaitForTasksToCompleteOnShutdown(true);
            scheduler.setErrorHandler(t -> log.error(
                    "Unknown error occurred while executing task.", t
            ));
            scheduler.setRejectedExecutionHandler(
                    (r, e) -> log.error(
                            "Execution of task {} was rejected for unknown reasons.", r
                    )
            );
    
            return scheduler;
        }
    }
    
    
    java.lang.IllegalStateException: @Bean method SchedulerConfig.taskScheduler called as bean reference for type [org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy264]. Overriding bean of same name declared in: class path resource [com/traveliko/platform/core/config/SchedulerConfig.class]
    	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:418) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    	at com.traveliko.platform.core.config.SchedulerConfig$$EnhancerBySpringCGLIB$$22cac23f.taskScheduler(<generated>) ~[classes/:na]
    	at com.traveliko.platform.core.config.SchedulerConfig.configureTasks(SchedulerConfig.java:45) ~[classes/:na]
    
  3. lukas-krecan commented on Dec 19, 2018

    @lukas-krecan
    Owner

    You are right, it's indeed a bug. I will take a look at it in next few days. Thanks for reporting.

  4. lukas-krecan commented on Dec 19, 2018

    @lukas-krecan
    Owner

    Actually, there is an easy workaround, return type of taskScheduler method should be TaskScheduler and not ThreadPoolTaskScheduler.

  5. bjornharvold commented on Dec 19, 2018

    @bjornharvold
    Author

    Hi @lukas-krecan

    That was indeed an oversight on my side. Not even a workaround ;-) That's the proper way to do it!

    Thank you.

  6. sanderdewinter commented on May 8, 2019

    @sanderdewinter

    Hi, I'm sorry to interrupt to use this closed issue.

    I think I found some related issue on Stackoverflow which we do run into as well.

    It might be due to the usage of the messageBrokerTaskScheduler bean defined in AbstractMessageBrokerConfiguration, or check the docs.

    Could you please shed some light on this?

  7. lukas-krecan commented on May 8, 2019

    @lukas-krecan
    Owner
  8. sanderdewinter commented on May 8, 2019

    @sanderdewinter

    @lukas-krecan Thanks for you help! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions