简体   繁体   中英

Error while fetching value from properties file for @Scheduled fixedRate

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);
            }
        }
    }       
}

The value for MILLISECONDS_OF_12_HOURS is being set in the external properties file. And I am trying to set that value to FixedRateString. But its throwing error saying "The value for annotation attribute Scheduled.fixedRateString must be a constant expression" Any help would be appreciated.. Thanks!

You can inject values from the context. Something like

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

and have the property file (application.properties YAML) defined outside

propertykey:
    myRate: 5000

Keep in mind that fixedRate in the example above takes a long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM