001/*
002 * Copyright 2002-2012 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.commonj;
018
019import java.util.LinkedList;
020import java.util.List;
021import javax.naming.NamingException;
022
023import commonj.timers.Timer;
024import commonj.timers.TimerManager;
025
026import org.springframework.beans.factory.DisposableBean;
027import org.springframework.beans.factory.FactoryBean;
028import org.springframework.beans.factory.InitializingBean;
029import org.springframework.context.Lifecycle;
030
031/**
032 * {@link org.springframework.beans.factory.FactoryBean} that retrieves a
033 * CommonJ {@link commonj.timers.TimerManager} and exposes it for bean references.
034 *
035 * <p><b>This is the central convenience class for setting up a
036 * CommonJ TimerManager in a Spring context.</b>
037 *
038 * <p>Allows for registration of ScheduledTimerListeners. This is the main
039 * purpose of this class; the TimerManager itself could also be fetched
040 * from JNDI via {@link org.springframework.jndi.JndiObjectFactoryBean}.
041 * In scenarios that just require static registration of tasks at startup,
042 * there is no need to access the TimerManager itself in application code.
043 *
044 * <p>Note that the TimerManager uses a TimerListener instance that is
045 * shared between repeated executions, in contrast to Quartz which
046 * instantiates a new Job for each execution.
047 *
048 * @author Juergen Hoeller
049 * @since 2.0
050 * @see ScheduledTimerListener
051 * @see commonj.timers.TimerManager
052 * @see commonj.timers.TimerListener
053 */
054public class TimerManagerFactoryBean extends TimerManagerAccessor
055                implements FactoryBean<TimerManager>, InitializingBean, DisposableBean, Lifecycle {
056
057        private ScheduledTimerListener[] scheduledTimerListeners;
058
059        private final List<Timer> timers = new LinkedList<Timer>();
060
061
062        /**
063         * Register a list of ScheduledTimerListener objects with the TimerManager
064         * that this FactoryBean creates. Depending on each ScheduledTimerListener's settings,
065         * it will be registered via one of TimerManager's schedule methods.
066         * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long)
067         * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
068         * @see commonj.timers.TimerManager#scheduleAtFixedRate(commonj.timers.TimerListener, long, long)
069         */
070        public void setScheduledTimerListeners(ScheduledTimerListener[] scheduledTimerListeners) {
071                this.scheduledTimerListeners = scheduledTimerListeners;
072        }
073
074
075        //---------------------------------------------------------------------
076        // Implementation of InitializingBean interface
077        //---------------------------------------------------------------------
078
079        @Override
080        public void afterPropertiesSet() throws NamingException {
081                super.afterPropertiesSet();
082                if (this.scheduledTimerListeners != null) {
083                        TimerManager timerManager = getTimerManager();
084                        for (ScheduledTimerListener scheduledTask : this.scheduledTimerListeners) {
085                                Timer timer;
086                                if (scheduledTask.isOneTimeTask()) {
087                                        timer = timerManager.schedule(scheduledTask.getTimerListener(), scheduledTask.getDelay());
088                                }
089                                else {
090                                        if (scheduledTask.isFixedRate()) {
091                                                timer = timerManager.scheduleAtFixedRate(
092                                                                scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod());
093                                        }
094                                        else {
095                                                timer = timerManager.schedule(
096                                                                scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod());
097                                        }
098                                }
099                                this.timers.add(timer);
100                        }
101                }
102        }
103
104
105        //---------------------------------------------------------------------
106        // Implementation of FactoryBean interface
107        //---------------------------------------------------------------------
108
109        @Override
110        public TimerManager getObject() {
111                return getTimerManager();
112        }
113
114        @Override
115        public Class<? extends TimerManager> getObjectType() {
116                TimerManager timerManager = getTimerManager();
117                return (timerManager != null ? timerManager.getClass() : TimerManager.class);
118        }
119
120        @Override
121        public boolean isSingleton() {
122                return true;
123        }
124
125
126        //---------------------------------------------------------------------
127        // Implementation of DisposableBean interface
128        //---------------------------------------------------------------------
129
130        /**
131         * Cancels all statically registered Timers on shutdown,
132         * and stops the underlying TimerManager (if not shared).
133         * @see commonj.timers.Timer#cancel()
134         * @see commonj.timers.TimerManager#stop()
135         */
136        @Override
137        public void destroy() {
138                // Cancel all registered timers.
139                for (Timer timer : this.timers) {
140                        try {
141                                timer.cancel();
142                        }
143                        catch (Throwable ex) {
144                                logger.warn("Could not cancel CommonJ Timer", ex);
145                        }
146                }
147                this.timers.clear();
148
149                // Stop the TimerManager itself.
150                super.destroy();
151        }
152
153}