Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When @Configuration class implements SchedulingConfigurer #115

Closed
bjornharvold opened this issue Dec 18, 2018 · 8 comments
Closed

When @Configuration class implements SchedulingConfigurer #115

bjornharvold opened this issue Dec 18, 2018 · 8 comments

Comments

@bjornharvold
Copy link

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");
    }
@lukas-krecan
Copy link
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.

@bjornharvold
Copy link
Author

bjornharvold commented Dec 19, 2018

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]

@lukas-krecan
Copy link
Owner

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

@lukas-krecan
Copy link
Owner

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

@bjornharvold
Copy link
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.

@sanderdewinter
Copy link

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?

@lukas-krecan
Copy link
Owner

lukas-krecan commented May 8, 2019

@sanderdewinter
Copy link

@lukas-krecan Thanks for you help! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants