001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.quartz;
018
019import org.quartz.Scheduler;
020import org.quartz.SchedulerException;
021import org.quartz.impl.SchedulerRepository;
022
023import org.springframework.beans.factory.BeanFactory;
024import org.springframework.beans.factory.BeanFactoryAware;
025import org.springframework.beans.factory.InitializingBean;
026import org.springframework.beans.factory.ListableBeanFactory;
027
028/**
029 * Spring bean-style class for accessing a Quartz Scheduler, i.e. for registering jobs,
030 * triggers and listeners on a given {@link org.quartz.Scheduler} instance.
031 *
032 * <p>Compatible with Quartz 2.1.4 and higher, as of Spring 4.1.
033 *
034 * @author Juergen Hoeller
035 * @since 2.5.6
036 * @see #setScheduler
037 * @see #setSchedulerName
038 */
039public class SchedulerAccessorBean extends SchedulerAccessor implements BeanFactoryAware, InitializingBean {
040
041        private String schedulerName;
042
043        private Scheduler scheduler;
044
045        private BeanFactory beanFactory;
046
047
048        /**
049         * Specify the Quartz {@link Scheduler} to operate on via its scheduler name in the Spring
050         * application context or also in the Quartz {@link org.quartz.impl.SchedulerRepository}.
051         * <p>Schedulers can be registered in the repository through custom bootstrapping,
052         * e.g. via the {@link org.quartz.impl.StdSchedulerFactory} or
053         * {@link org.quartz.impl.DirectSchedulerFactory} factory classes.
054         * However, in general, it's preferable to use Spring's {@link SchedulerFactoryBean}
055         * which includes the job/trigger/listener capabilities of this accessor as well.
056         * <p>If not specified, this accessor will try to retrieve a default {@link Scheduler}
057         * bean from the containing application context.
058         */
059        public void setSchedulerName(String schedulerName) {
060                this.schedulerName = schedulerName;
061        }
062
063        /**
064         * Specify the Quartz {@link Scheduler} instance to operate on.
065         * <p>If not specified, this accessor will try to retrieve a default {@link Scheduler}
066         * bean from the containing application context.
067         */
068        public void setScheduler(Scheduler scheduler) {
069                this.scheduler = scheduler;
070        }
071
072        /**
073         * Return the Quartz Scheduler instance that this accessor operates on.
074         */
075        @Override
076        public Scheduler getScheduler() {
077                return this.scheduler;
078        }
079
080        @Override
081        public void setBeanFactory(BeanFactory beanFactory) {
082                this.beanFactory = beanFactory;
083        }
084
085
086        @Override
087        public void afterPropertiesSet() throws SchedulerException {
088                if (this.scheduler == null) {
089                        this.scheduler = (this.schedulerName != null ? findScheduler(this.schedulerName) : findDefaultScheduler());
090                }
091                registerListeners();
092                registerJobsAndTriggers();
093        }
094
095        protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
096                if (this.beanFactory instanceof ListableBeanFactory) {
097                        ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
098                        String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
099                        for (String beanName : beanNames) {
100                                Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
101                                if (schedulerName.equals(schedulerBean.getSchedulerName())) {
102                                        return schedulerBean;
103                                }
104                        }
105                }
106                Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
107                if (schedulerInRepo == null) {
108                        throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
109                }
110                return schedulerInRepo;
111        }
112
113        protected Scheduler findDefaultScheduler() {
114                if (this.beanFactory != null) {
115                        return this.beanFactory.getBean(Scheduler.class);
116                }
117                else {
118                        throw new IllegalStateException(
119                                        "No Scheduler specified, and cannot find a default Scheduler without a BeanFactory");
120                }
121        }
122
123}