spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.25k stars 37.98k forks source link

Non immutable SchedulerFactoryBean [SPR-196] #4927

Closed spring-projects-issues closed 20 years ago

spring-projects-issues commented 20 years ago

Hugues L opened SPR-196 and commented

This is a minor adition to the SchedulerFactoryBean class. For the sake of our project, we need to register programatically Quartz triggers, calendars and jobDetails to an existing scheduler.

As we maintain a central scheduler with Spring, wrapped in a Spring managed SchedulerFactoryBean instance, we are proposing a patch to add individual triggers, calendars and jobDetails to the ones managed by SchedulerFactoryBean.

Maybe we are not using the correct way of doing things (maybe we should not use SchedulerFactoryBean?), but as we want to leverage all the benefits of Spring, this what we thought we could use.

Thanks for reviewing, and congratulations for the nice framework,

Hugues.

I copy the aded code below :

/**
 * Add a single job to the underlying scheduler. This method allows the
 * programmatic addition of <code>JobDetail</code> s after the initial
 * population of the <code>JobDetail</code> s list has been perfomed by
 * the Spring factory.
 * 
 * @param jobDetail
 *            the JobDetail to add
 */
public void addJobDetail(JobDetail jobDetail) throws SchedulerException {
    this.jobDetails.add(jobDetail);
    this.scheduler.addJob(jobDetail, true);
}

/**
 * Add a single Quartz Calendar object to the underlying scheduler. This
 * method allows the programmatic addition of <code>Calendar</code> s
 * after the initial population of the <code>Calendar</code> s map has
 * been perfomed by the Spring factory.
 * 
 * @param calendarName
 *            the name of the calendar
 * @param calendar
 *            the Calendar to add
 */
public void addCalendar(String calendarName, Calendar calendar)
        throws SchedulerException {
    if (this.calendars != null)
        this.calendars = new HashMap();
    this.scheduler.addCalendar(calendarName, calendar, true);
}

/**
 * Add a single Quartz Trigger object to the underlying scheduler. This
 * method allows the programmatic addition of <code>Trigger</code> s after
 * the initial population of the <code>Trigger</code> s list has been
 * perfomed by the Spring factory.
 * 
 * If the Trigger determines the corresponding JobDetail itself, the job
 * will be automatically registered with the Scheduler.
 * 
 * @param trigger
 *            the Trigger to add
 */
public void addTrigger(Trigger trigger) throws SchedulerException {
    if (this.triggers != null)
        this.triggers = new ArrayList();

    if (trigger instanceof JobDetailAwareTrigger) {
        JobDetail jobDetail = ((JobDetailAwareTrigger) trigger)
                .getJobDetail();
        if (!this.jobDetails.contains(jobDetail)) {
            // automatically register the JobDetail too
            this.jobDetails.add(jobDetail);
            this.scheduler.addJob(jobDetail, true);
        }
    }
    this.scheduler.scheduleJob(trigger);
}

Affects: 1.0.2

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

The SchedulerFactoryBean itself is not meant to be accessed directly. Rather, when you refer to it via a bean reference, it will return the Quartz Scheduler that it built - because it is a FactoryBean.

\ ... \< /bean>

Scheduler scheduler = (Scheduler) ctx.getBean("scheduler)

Of course, this also works via bean references: \ will return a Scheduler object.

Use that org.quartz.Scheduler instance to register further jobs and triggers; that's what I'm doing myself regularly. SchedulerFactoryBean itself doesn't need to know about those.

Juergen

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

Considered as resolved: available by accessing the created Scheduler rather than the SchedulerFactoryBean itself.

Juergen