【问题标题】:Error while fetching value from properties file for @Scheduled fixedRate从@Scheduled fixedRate 的属性文件中获取值时出错
【发布时间】:2020-02-19 23:03:24
【问题描述】:
private static final String CONSTANT = (p.getProperty("MILLISECONDS_OF_12_HOURS"));

@Scheduled(fixedRateString = CONSTANT)
public void clearCache() throws IOException {


    if(!rrcodeService.cachedAccessGroups.isEmpty()) {
        for (Entry<String, CachedAccessGroups> entry : rrcodeService.cachedAccessGroups.entrySet()) {
            String key = entry.getKey();
            CachedAccessGroups accessGroups = rrcodeService.cachedAccessGroups.get(key);
            if(now.getTime() - accessGroups.getCachedDate().getTime()> Integer.parseInt(p.getProperty("EVICT_CACHE"))) {
                rrcodeService.cachedAccessGroups.remove(key);
            }
        }
    }       
}

在外部属性文件中设置 MILLISECONDS_OF_12_HOURS 的值。我正在尝试将该值设置为 FixedRateString。但它的抛出错误说“注释属性 Scheduled.fixedRateString 的值必须是一个常量表达式” 任何帮助将不胜感激。谢谢!

【问题讨论】:

标签: java scheduled-tasks


【解决方案1】:

您可以从上下文中注入值。类似的东西

@Scheduled(fixedRate = "${propertykey.myRate}", initialDelay=1000)
public void clearCache() throws IOException {
   .....
}

并在外部定义属性文件 (application.properties YAML)

propertykey:
    myRate: 5000

请记住,上面示例中的fixedRate 采用long

【讨论】:

  • 是的,这是针对 Spring 启动的。我们没有使用引导。需要找到简单弹簧的解决方案。
猜你喜欢
  • 2023-04-10
  • 2013-02-08
  • 2015-04-04
  • 2020-08-29
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多