0
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!

Anuja Patil
  • 47
  • 1
  • 10
  • Take a look at this question: [How to supply value to an annotation from a Constant java](https://stackoverflow.com/questions/2065937/how-to-supply-value-to-an-annotation-from-a-constant-java). – andrewJames Feb 19 '20 at 15:00

1 Answers1

2

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

Saif Asif
  • 5,516
  • 3
  • 31
  • 48