0

Hello What I want to achieve is have a scheduler setup so that it runs every X minutes, this x minutes should be read from a configuration file. I'm getting a compilation error saying "Attribute value must be a constant". Can you suggest me the best approach on a situation like this.

I have tried out few approaches.

  1. using a constructor

    @Singleton
    @LocalBean
    public class Scheduler {
    
     final String SCHEDULER;
    
     public Scheduler() {
    
         ConfigResources configResources = ConfigResources.getConfigResources();
         SCHEDULER = configResources.getEolApiScheduler();
     }
    
     @Schedule(minute= SCHEDULER, hour="*")
     public void retryEndOfLeaseSchedule() {
    
         System.out.println("inside retryEndOfLeaseSchedule ");
    
     }
    
  2. using a static block

    @Singleton
    @LocalBean
    public class Scheduler {
    
     final static String SCHEDULER;
    
     static {
    
         ConfigResources configResources = ConfigResources.getConfigResources();
         SCHEDULER = configResources.getEolApiScheduler();
     }
    
     @Schedule(minute= SCHEDULER, hour="*")
     public void retryEndOfLeaseSchedule() {
    
         System.out.println("inside retryEndOfLeaseSchedule ");
    
     }
    
  • Please post the actual code, don't use images. [Reasons why](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – AlexT Feb 06 '21 at 11:58
  • @Alex.T, edited, any idea about the answer ? thanks in advance – Achala Yasas Piyarathna Feb 06 '21 at 12:21
  • I usually handle this scenario in `Spring` by using a `Trigger`, similarly to this [another similar question, but on spring](https://stackoverflow.com/questions/14630539/scheduling-a-job-with-spring-programmatically-with-fixedrate-set-dynamically). I'm not sure how it would be done directly in Java (or another library). – AlexT Feb 06 '21 at 19:04

0 Answers0