Spring Boot不运行计划任务 @Scheduled

huangapple go评论50阅读模式
英文:

Spring boot doesn't run scheduler @Scheduled

问题

我在我的项目配置中使用了一个用于缓存清除的配置bean,但它没有运行。我在另一个项目中使用了同样的类,而且运行得很好,但我现在不知道问题出在哪里。

问题可能是与这个配置bean的其他地方有关,因为当我使用以下代码时也没有日志输出:

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

我查看了TRACE级别的Spring日志,没有错误,该类位于类路径中。我不知道问题可能出在哪里。

我在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}"
}

我使用Java 11和以下的主类:

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

编辑: 我发现问题出在我的配置中:

main:
    lazy-initialization: true

我以为bean会在调度程序激活时创建。

英文:

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.

答案1

得分: 1

解决方案在禁用功能中:

spring:
   main:
     lazy-initialization: false
英文:

Solution was in disable feature:

spring:
   main:
     lazy-initialization: false

答案2

得分: 0

看起来你正在使用WebFlux,你可能需要返回一个可订阅的发布者(Mono或Flux),例如:

public Mono<Void> cachePosEvict() {
    log.info("清除缓存: {}", CACHE_NAME);
}
英文:

Looks like you are using a webflux, you might need to return a publisher (Mono or Flux) that can be subscribed to, for example:

public Mono&lt;Void&gt; cachePosEvict() {
    log.info(&quot;Evicting cache: {}&quot;, CACHE_NAME);
}

答案3

得分: 0

你定期从缓存中清除所有条目。要么你有一个非常特定的用例,要么你做错了。

我假设你正在使用Caffeine实现。在这种情况下,请考虑通过专用属性配置缓存过期时间:

spring.cache.caffeine.spec=expireAfterWrite=5s

更多信息请查看这里:

https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#cache-properties

英文:

You periodically evict all entries from your cache. Either you have a very specific use case or you're doing it wrong.

I assume that you use Caffeine implementation. In this case consider configuring cache expiration through a dedicated property:

spring.cache.caffeine.spec=expireAfterWrite=5s

More information here:

https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#cache-properties

huangapple
  • 本文由 发表于 2020年7月22日 20:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定