Search code examples
jakarta-eeejbschedule

EJB @Schedule annotation set dynamic values


I have a @Schedule annotation added to a method.

@Schedule(minute = "1", hour = "*", info="Every 1 minute")
public void getStandingOrdersTimer() {
        
}

Here the method will run every minute in every hour. I want to pass number of minute using a variable. With out hardcoding it here in the annotation. How can I achieve this?


Solution

  • You need to create a Timer with the TimerService.

    @Singleton
    @Startup
    @LocalBean
    public class TimerBean {
    
        @Resource
        TimerService timerService;
    
        @PostConstruct
        public void init(){
            timerService.createIntervalTimer(new Date(), getMinute() * 60000
                            new TimerConfig("MyTimer", false));
        }
    
        private int getMinute(){
            //here your code to get the minute value from somewhere
        }
    
        @Timeout
        public void getStandingOrdersTimer() {
            
        }
    }