Spring boot 配置的cron上基于Groovy的Spring引导任务阻塞
不确定这是纯Spring引导问题,纯Groovy问题,还是使用Groovy构建Spring引导应用程序时出现的问题。我有一个Spring Boot后台任务,在生产中,我希望每小时运行一次:Spring boot 配置的cron上基于Groovy的Spring引导任务阻塞,spring-boot,groovy,Spring Boot,Groovy,不确定这是纯Spring引导问题,纯Groovy问题,还是使用Groovy构建Spring引导应用程序时出现的问题。我有一个Spring Boot后台任务,在生产中,我希望每小时运行一次: @Component class MyTask { @Scheduled(cron = "${tasks.mytask.cron}") void doSomething() { // blah whatever } } 在我的application.yml文件中,我有
@Component
class MyTask {
@Scheduled(cron = "${tasks.mytask.cron}")
void doSomething() {
// blah whatever
}
}
在我的application.yml文件中,我有:
logging:
config: 'logback.groovy'
server:
port: 9200
error:
whitelabel:
enabled: false
spring:
cache:
type: none
myapp:
detailsMode: ${detailsMode:Terse}
verification: 5
tasks:
mytask:
cron: '0 0/1 * 1/1 * ? *'
但是,对于本地开发,我希望能够更改用于测试等的cron表达式。。当我开始编译时,我得到:
Expected '$tasks.mytask.cron' to be an inline constant of type java.lang.String in @org.springframework.scheduling.annotation.Scheduled
@ line 31, column 23.
@Scheduled(cron = "${tasks.mytask.cron}")
你知道我需要做什么来解决这个问题吗?我需要一个外部可配置的值,比如tasks.mytask.cron,我可以在我的应用程序属性/YAML中定义它
myapp:
detailsMode: ${detailsMode:Terse}
verification: 5
tasks:
mytask:
cron: '0 0/1 * 1/1 * ?'
或
还要注意,您的cron格式不正确谢谢@dawid+1,但这是编译器错误,不是运行时错误。我在想也许Groovy不喜欢注释中的-style表达式?对,Groovy然后@Scheduledcron='${myapp.tasks.mytask.cron}基本上是指gstring-因为gstring$是Groovy变量-你不想要Groovy变量-你想要Spring属性-这就是为什么需要用正则字符串替换它-在Groovy中-'someting'
@Scheduled(cron = '${myapp.tasks.mytask.cron}')