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