1

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.

Joy
  • 4,197
  • 14
  • 61
  • 131
  • can you please show your import statements for `@Bean public LockProvider lockProvider(MyDbDataSource dataSource) {` – J Asgarov Aug 03 '20 at 15:23
  • @JAsgarov update OP accordingly. – Joy Aug 03 '20 at 15:30
  • could it be that your `@ComponentScan` doesn't pick up your `@Configuration` because your `MyService` class is in a lower level directory? (btw you don't need extra `@ComponentScan` because `@SpringBootApplication` includes component scan – J Asgarov Aug 03 '20 at 15:57
  • @JAsgarov I get what you are telling. But everything was working fine until I added this Scheduler related configs. So I guess I messed up with Scheduling related annotations. – Joy Aug 03 '20 at 16:01

1 Answers1

5

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

J Asgarov
  • 2,526
  • 1
  • 8
  • 18
  • 1
    Adding more on this answer, if your table name is defined under schema like XYZ, then you can rewrite your return statement as: `return new JdbcTemplateLockProvider(dataSource, "XYZ.SHEDLOCK");` – Anish Panthi Jun 08 '22 at 19:47