[英]Spring boot doesn't run scheduler @Scheduled
I used in my project configuration bean for cache evict and it's not runs.我在我的项目配置 bean 中使用了缓存驱逐,但它没有运行。 I use this some class in another projects and works fine but I don't know where is the problem now.
我在另一个项目中使用了一些 class 并且工作正常,但我不知道现在问题出在哪里。
@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:问题可能出在这个配置 bean 的其他地方,因为当我使用时:
@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.
我查看了 TRACE spring 日志并且没有错误,class 在类路径中。 I don't know where can be a problem.
我不知道哪里有问题。
I have following dependencies in gradle:我在 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:我使用 Java 11 和主 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.我认为当调度程序处于活动状态时会创建 bean。
Looks like you are using a webflux, you might need to return a publisher (Mono or Flux) that can be subscribed to, for example:看起来您正在使用 webflux,您可能需要返回可以订阅的发布者(Mono 或 Flux),例如:
public Mono<Void> cachePosEvict() {
log.info("Evicting cache: {}", CACHE_NAME);
}
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.我假设您使用 Caffeine 实现。 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 https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#cache-properties
Solution was in disable feature:解决方案是禁用功能:
spring:
main:
lazy-initialization: false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.