Search code examples
javaquartz-schedulerquartz

Does SchedularFactory returns same instance with new method


I am using quartz schedular. In method 1 I am initializing my schedular like this

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.scheduleJob(someJob,someTrigger);

In method 2 if I do

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

Does the above call to the schedular factory will return the same instance that I have created in method 1 or will it return another instance and then I need to link all my jobs and trigger with that.


Solution

  • Since you're creating 2 factories, they won't return the same scheduler. You should use a single factory if you want to use the same scheduler.

    If you're using a single StdSchedulerFactory, the scheduler (if it exists) will be reused if it's not shut down:

    public Scheduler getScheduler() throws SchedulerException {
        if (cfg == null) {
            initialize();
        }
    
        SchedulerRepository schedRep = SchedulerRepository.getInstance();
    
        Scheduler sched = schedRep.lookup(getSchedulerName());
    
        if (sched != null) {
            if (sched.isShutdown()) {
                schedRep.remove(getSchedulerName());
            } else {
                return sched;
            }
        }
    
        sched = instantiate();
    
        return sched;
    }