0

I need to run a daily job which will run once a day and get some data from a different database. The task is developed as an EJB. I tried the @Schedule with EJB, and it is working fine. But the issue is if there is a change in the schedule, the code has to be changed and the app to be redeployed. Is there a way to avoid this? May be by using configuration files etc. I am using JSF 2.2, glassfish3.4.2 in CentOS.

jpr
  • 185
  • 3
  • 6
  • 20
  • No, once deployed the schedule cannot be changed. Take look to [this][1]. [1]:http://stackoverflow.com/questions/21679760/dynamically-change-timeout-interval-in-timer – Gabriel Aramburu Feb 26 '14 at 11:43

2 Answers2

1

You can do this by creating your timer programmatically something like this

....
    private static final String TIMER_NAME = "SOME_TIMER_NAME";
....
    @Resource
    TimerService timerService;    
....
    public void createTimer(String days, String hours, String minutes) {
        removeTimer();
        ScheduleExpression scheduleExpression = new ScheduleExpression();       
        if (days != null) {           
            scheduleExpression.dayOfMonth(datys);
        }
        if (hours != null) {           
            scheduleExpression.hour(hours);
        }
        if (minutes != null) {           
            scheduleExpression.minute(minutes);
        }
        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setInfo(TIMER_NAME);
        Timer timer = timerService.createCalendarTimer(scheduleExpression, timerConfig);
    }


    private void removeTimer() {
        for (Timer timer : timerService.getTimers()) {
            if (TIMER_NAME.equals(timer.getInfo())) {
                timer.cancel();
            }
        }
    }

    @Timeout
    public void retrieveData() {
        // retrieve data from DBs
    }

now calling the createTimer method like this

createTimer("*", "3", null);

retrieveData() method will be executed at 3:00 AM each day. If you want to change the schedule you should call the method with appropriate values. You may tune it to meet your requirements like add input validation, define parameters differently etc. You may want to look at ScheduleExpression to find what is best for you. My code just shows an idea.

Fotis Grigorakis
  • 363
  • 1
  • 3
  • 16
jjd
  • 2,158
  • 2
  • 18
  • 31
  • Thank you very much. This should lead to a solution. – jpr Mar 01 '14 at 22:55
  • I took this approach finally. I created a UI to accept the days, hours, and minutes and called the above method in EJB from the backing bean. So now I can change the job schedule without changing the code and redeploying the app. – jpr Mar 03 '14 at 02:11
-1

Did you consider using cron job? here are some examples: Cron examples

Nebojsa
  • 1
  • 2