英文:
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<Void> cachePosEvict() {
log.info("Evicting cache: {}", CACHE_NAME);
}
答案3
得分: 0
你定期从缓存中清除所有条目。要么你有一个非常特定的用例,要么你做错了。
我假设你正在使用Caffeine实现。在这种情况下,请考虑通过专用属性配置缓存过期时间:
spring.cache.caffeine.spec=expireAfterWrite=5s
更多信息请查看这里:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论