Skip to content
Advertisement

Updating CroneSchedule & Job Dynamically without stopping server

I need help in externalizing the below cron schedule or executing it dynamically. For example, in the below code it is hardcoded to perform every Saturday: cronSchedule(“0 0 12 ? * SAT”). I want the value inside the cronSchedule() to externalize so that even after the server is started, I can still change the cron schedule to Monday or every day based on my choice and it can be run. I am looking for suggestion in java and not in spring.

public void run() throws Exception {
        // Getting a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        // job will run every week at Saturday 12 Noon Server Time
        JobDetail job = newJob(CachingJob.class).withIdentity("job1", "group1").build();
        CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0 0 12 ? * SAT"))
                .build();
        Date ft = sched.scheduleJob(job, trigger);
        sched.start();
        SchedulerMetaData metaData = sched.getMetaData();
    }

Any input or suggestion is appreciated.

Advertisement

Answer

We can perform below method to reschedule the job :

cronScheduler.rescheduleJob(cronTrigger.getKey(), newTrigger().withIdentity("customTrigger", "defaultGroup")
                       .withSchedule(cronSchedule(cronExpression)).build());
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement