Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No qualifying bean of type LockProvider while configuring Spring Scheduler with Shedlock

I am trying to integrate Shedlock to make scheduled job on my Spring boot application run seamlessly in multi pod deployments.

Main class is as follows:

@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "60m", defaultLockAtLeastFor = "15m")
@ComponentScan
public class MyService {

  public static void main(String[] args) {
    SconeApp.run(MyService.class, args);
  }
}

For configuring scheduler with Shedlock added following class:

import com.salesforce.tm.scheduler.MyScheduler;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class LockProviderConfiguration {

  @Bean
  public LockProvider lockProvider(MyDbDataSource dataSource) {
    return new JdbcTemplateLockProvider(
        JdbcTemplateLockProvider.Configuration.builder()
            .withJdbcTemplate(new JdbcTemplate(dataSource))
            .build()
    );
  }

  @Bean
  public MyScheduler myScheduler(LockProvider lockProvider) {
    return new MyScheduler();
  }
}

MyDbDataSource class is as follows:

@Component
public class MyDbDataSource implements InitializingBean {

  public TcDbDataSource(..) {
    super();
    ...
  }

  @Override
  public void afterPropertiesSet() {
     ...
  }
}

My scheduler class is as follows:

public class MyScheduler {

  @Scheduled(cron = "*/1 * * * * *")
  @SchedulerLock(name = "myTask",
      lockAtMostFor = "${scheduling.my-job.lock-at-most}",
      lockAtLeastFor = "${scheduling.my-joblock-at-least}")
  public void myCronJob() throws InterruptedException {
    LockAssert.assertLocked();
    while(true) {
      System.out.println("Test");
      Thread.sleep(1000L);
    }
  }
}

While launching the application, I am getting following error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.javacrumbs.shedlock.core.LockProvider' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1714)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1270)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:884)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788)

I cannot understand what I am missing here.

like image 372
Joy Avatar asked Nov 27 '25 10:11

Joy


1 Answers

try redefining your LockProvider bean

@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}

like in this guide https://www.baeldung.com/shedlock-spring

like image 191
asgarov1 Avatar answered Nov 28 '25 23:11

asgarov1