Reputation: 47
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!
Upvotes: 0
Views: 935
Reputation: 5658
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
Upvotes: 2