Search code examples
javaspring-bootspring-webfluxspring-cachespring-cloud-gateway

Spring boot doesn't run scheduler @Scheduled


I used in my project configuration bean for cache evict and it's not runs. I use this some class in another projects and works fine but I don't know where is the problem now.

@Configuration
@Slf4j
public class CacheConfig {

    public static final String BANKCODE_CACHE_NAME = "cacheName";

    @CacheEvict(allEntries = true, cacheNames = { CACHE_NAME })
    @Scheduled(fixedRate = 5000)
    public void cachePosEvict() {
        log.info("Evicting cache: {}", CACHE_NAME);
    }

}

Problem is probably somewhere else with this config bean, because also when I use:

@PostConstruct
void init() {
    log.info("Init...");
}

Then is nothing in log. I looked into TRACE spring logs and there is no error, class is in the classpath. I don't know where can be a problem.

I have following dependencies in gradle:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

ext {
    set('springCloudVersion', "Hoxton.SR6")
    webfluxUiVersion = "1.3.9"
    jacksonVersion = "2.10.1"
    logbackJson = "0.1.5"
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation "org.springdoc:springdoc-openapi-webflux-ui:${webfluxUiVersion}"


    implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
    implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
    implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
    implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jacksonVersion}"

    implementation "ch.qos.logback.contrib:logback-json-classic:${logbackJson}"
    implementation "ch.qos.logback.contrib:logback-jackson:${logbackJson}"

I use Java 11 with Main class:

@ConfigurationPropertiesScan
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class MyApp{...}

EDIT: I found that problem is in my configuration:

main:
    lazy-initialization: true

I thought that bean will be created when Scheduler is active.


Solution

  • Solution was in disable feature:

    spring:
       main:
         lazy-initialization: false