added TaskScheduler interface and Trigger abstraction; added ConcurrentTaskScheduler and ThreadPoolTaskScheduler; added CommonJ TimerManagerTaskScheduler; added CronTrigger implementation for cron expression support
This commit is contained in:
parent
26f0671250
commit
df99929e21
|
|
@ -0,0 +1,163 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.commonj;
|
||||||
|
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
|
||||||
|
import commonj.timers.TimerManager;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.context.Lifecycle;
|
||||||
|
import org.springframework.jndi.JndiLocatorSupport;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for classes that are accessing a CommonJ {@link commonj.timers.TimerManager}
|
||||||
|
* Defines common configuration settings and common lifecycle handling.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see commonj.timers.TimerManager
|
||||||
|
*/
|
||||||
|
public abstract class TimerManagerAccessor extends JndiLocatorSupport
|
||||||
|
implements InitializingBean, DisposableBean, Lifecycle {
|
||||||
|
|
||||||
|
private TimerManager timerManager;
|
||||||
|
|
||||||
|
private String timerManagerName;
|
||||||
|
|
||||||
|
private boolean shared = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify the CommonJ TimerManager to delegate to.
|
||||||
|
* <p>Note that the given TimerManager's lifecycle will be managed
|
||||||
|
* by this FactoryBean.
|
||||||
|
* <p>Alternatively (and typically), you can specify the JNDI name
|
||||||
|
* of the target TimerManager.
|
||||||
|
* @see #setTimerManagerName
|
||||||
|
*/
|
||||||
|
public void setTimerManager(TimerManager timerManager) {
|
||||||
|
this.timerManager = timerManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the JNDI name of the CommonJ TimerManager.
|
||||||
|
* <p>This can either be a fully qualified JNDI name, or the JNDI name relative
|
||||||
|
* to the current environment naming context if "resourceRef" is set to "true".
|
||||||
|
* @see #setTimerManager
|
||||||
|
* @see #setResourceRef
|
||||||
|
*/
|
||||||
|
public void setTimerManagerName(String timerManagerName) {
|
||||||
|
this.timerManagerName = timerManagerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify whether the TimerManager obtained by this FactoryBean
|
||||||
|
* is a shared instance ("true") or an independent instance ("false").
|
||||||
|
* The lifecycle of the former is supposed to be managed by the application
|
||||||
|
* server, while the lifecycle of the latter is up to the application.
|
||||||
|
* <p>Default is "false", i.e. managing an independent TimerManager instance.
|
||||||
|
* This is what the CommonJ specification suggests that application servers
|
||||||
|
* are supposed to offer via JNDI lookups, typically declared as a
|
||||||
|
* <code>resource-ref</code> of type <code>commonj.timers.TimerManager</code>
|
||||||
|
* in <code>web.xml<code>, with <code>res-sharing-scope</code> set to 'Unshareable'.
|
||||||
|
* <p>Switch this flag to "true" if you are obtaining a shared TimerManager,
|
||||||
|
* typically through specifying the JNDI location of a TimerManager that
|
||||||
|
* has been explicitly declared as 'Shareable'. Note that WebLogic's
|
||||||
|
* cluster-aware Job Scheduler is a shared TimerManager too.
|
||||||
|
* <p>The sole difference between this FactoryBean being in shared or
|
||||||
|
* non-shared mode is that it will only attempt to suspend / resume / stop
|
||||||
|
* the underlying TimerManager in case of an independent (non-shared) instance.
|
||||||
|
* This only affects the {@link org.springframework.context.Lifecycle} support
|
||||||
|
* as well as application context shutdown.
|
||||||
|
* @see #stop()
|
||||||
|
* @see #start()
|
||||||
|
* @see #destroy()
|
||||||
|
* @see commonj.timers.TimerManager
|
||||||
|
*/
|
||||||
|
public void setShared(boolean shared) {
|
||||||
|
this.shared = shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void afterPropertiesSet() throws NamingException {
|
||||||
|
if (this.timerManager == null) {
|
||||||
|
if (this.timerManagerName == null) {
|
||||||
|
throw new IllegalArgumentException("Either 'timerManager' or 'timerManagerName' must be specified");
|
||||||
|
}
|
||||||
|
this.timerManager = lookup(this.timerManagerName, TimerManager.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final TimerManager getTimerManager() {
|
||||||
|
return this.timerManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Implementation of Lifecycle interface
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resumes the underlying TimerManager (if not shared).
|
||||||
|
* @see commonj.timers.TimerManager#resume()
|
||||||
|
*/
|
||||||
|
public void start() {
|
||||||
|
if (!this.shared) {
|
||||||
|
this.timerManager.resume();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suspends the underlying TimerManager (if not shared).
|
||||||
|
* @see commonj.timers.TimerManager#suspend()
|
||||||
|
*/
|
||||||
|
public void stop() {
|
||||||
|
if (!this.shared) {
|
||||||
|
this.timerManager.suspend();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Considers the underlying TimerManager as running if it is
|
||||||
|
* neither suspending nor stopping.
|
||||||
|
* @see commonj.timers.TimerManager#isSuspending()
|
||||||
|
* @see commonj.timers.TimerManager#isStopping()
|
||||||
|
*/
|
||||||
|
public boolean isRunning() {
|
||||||
|
return (!this.timerManager.isSuspending() && !this.timerManager.isStopping());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Implementation of DisposableBean interface
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the underlying TimerManager (if not shared).
|
||||||
|
* @see commonj.timers.TimerManager#stop()
|
||||||
|
*/
|
||||||
|
public void destroy() {
|
||||||
|
// Stop the entire TimerManager, if necessary.
|
||||||
|
if (!this.shared) {
|
||||||
|
// May return early, but at least we already cancelled all known Timers.
|
||||||
|
this.timerManager.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -27,7 +27,6 @@ import org.springframework.beans.factory.DisposableBean;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.context.Lifecycle;
|
import org.springframework.context.Lifecycle;
|
||||||
import org.springframework.jndi.JndiLocatorSupport;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.beans.factory.FactoryBean} that retrieves a
|
* {@link org.springframework.beans.factory.FactoryBean} that retrieves a
|
||||||
|
|
@ -52,71 +51,14 @@ import org.springframework.jndi.JndiLocatorSupport;
|
||||||
* @see commonj.timers.TimerManager
|
* @see commonj.timers.TimerManager
|
||||||
* @see commonj.timers.TimerListener
|
* @see commonj.timers.TimerListener
|
||||||
*/
|
*/
|
||||||
public class TimerManagerFactoryBean extends JndiLocatorSupport
|
public class TimerManagerFactoryBean extends TimerManagerAccessor
|
||||||
implements FactoryBean<TimerManager>, InitializingBean, DisposableBean, Lifecycle {
|
implements FactoryBean<TimerManager>, InitializingBean, DisposableBean, Lifecycle {
|
||||||
|
|
||||||
private TimerManager timerManager;
|
|
||||||
|
|
||||||
private String timerManagerName;
|
|
||||||
|
|
||||||
private boolean shared = false;
|
|
||||||
|
|
||||||
private ScheduledTimerListener[] scheduledTimerListeners;
|
private ScheduledTimerListener[] scheduledTimerListeners;
|
||||||
|
|
||||||
private final List<Timer> timers = new LinkedList<Timer>();
|
private final List<Timer> timers = new LinkedList<Timer>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Specify the CommonJ TimerManager to delegate to.
|
|
||||||
* <p>Note that the given TimerManager's lifecycle will be managed
|
|
||||||
* by this FactoryBean.
|
|
||||||
* <p>Alternatively (and typically), you can specify the JNDI name
|
|
||||||
* of the target TimerManager.
|
|
||||||
* @see #setTimerManagerName
|
|
||||||
*/
|
|
||||||
public void setTimerManager(TimerManager timerManager) {
|
|
||||||
this.timerManager = timerManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the JNDI name of the CommonJ TimerManager.
|
|
||||||
* <p>This can either be a fully qualified JNDI name, or the JNDI name relative
|
|
||||||
* to the current environment naming context if "resourceRef" is set to "true".
|
|
||||||
* @see #setTimerManager
|
|
||||||
* @see #setResourceRef
|
|
||||||
*/
|
|
||||||
public void setTimerManagerName(String timerManagerName) {
|
|
||||||
this.timerManagerName = timerManagerName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Specify whether the TimerManager obtained by this FactoryBean
|
|
||||||
* is a shared instance ("true") or an independent instance ("false").
|
|
||||||
* The lifecycle of the former is supposed to be managed by the application
|
|
||||||
* server, while the lifecycle of the latter is up to the application.
|
|
||||||
* <p>Default is "false", i.e. managing an independent TimerManager instance.
|
|
||||||
* This is what the CommonJ specification suggests that application servers
|
|
||||||
* are supposed to offer via JNDI lookups, typically declared as a
|
|
||||||
* <code>resource-ref</code> of type <code>commonj.timers.TimerManager</code>
|
|
||||||
* in <code>web.xml<code>, with <code>res-sharing-scope</code> set to 'Unshareable'.
|
|
||||||
* <p>Switch this flag to "true" if you are obtaining a shared TimerManager,
|
|
||||||
* typically through specifying the JNDI location of a TimerManager that
|
|
||||||
* has been explicitly declared as 'Shareable'. Note that WebLogic's
|
|
||||||
* cluster-aware Job Scheduler is a shared TimerManager too.
|
|
||||||
* <p>The sole difference between this FactoryBean being in shared or
|
|
||||||
* non-shared mode is that it will only attempt to suspend / resume / stop
|
|
||||||
* the underlying TimerManager in case of an independent (non-shared) instance.
|
|
||||||
* This only affects the {@link org.springframework.context.Lifecycle} support
|
|
||||||
* as well as application context shutdown.
|
|
||||||
* @see #stop()
|
|
||||||
* @see #start()
|
|
||||||
* @see #destroy()
|
|
||||||
* @see commonj.timers.TimerManager
|
|
||||||
*/
|
|
||||||
public void setShared(boolean shared) {
|
|
||||||
this.shared = shared;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a list of ScheduledTimerListener objects with the TimerManager
|
* Register a list of ScheduledTimerListener objects with the TimerManager
|
||||||
* that this FactoryBean creates. Depending on each ScheduledTimerListener's settings,
|
* that this FactoryBean creates. Depending on each ScheduledTimerListener's settings,
|
||||||
|
|
@ -135,28 +77,22 @@ public class TimerManagerFactoryBean extends JndiLocatorSupport
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
public void afterPropertiesSet() throws NamingException {
|
public void afterPropertiesSet() throws NamingException {
|
||||||
if (this.timerManager == null) {
|
super.afterPropertiesSet();
|
||||||
if (this.timerManagerName == null) {
|
|
||||||
throw new IllegalArgumentException("Either 'timerManager' or 'timerManagerName' must be specified");
|
|
||||||
}
|
|
||||||
this.timerManager = lookup(this.timerManagerName, TimerManager.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.scheduledTimerListeners != null) {
|
if (this.scheduledTimerListeners != null) {
|
||||||
|
TimerManager timerManager = getTimerManager();
|
||||||
for (ScheduledTimerListener scheduledTask : this.scheduledTimerListeners) {
|
for (ScheduledTimerListener scheduledTask : this.scheduledTimerListeners) {
|
||||||
Timer timer = null;
|
Timer timer;
|
||||||
if (scheduledTask.isOneTimeTask()) {
|
if (scheduledTask.isOneTimeTask()) {
|
||||||
timer = this.timerManager.schedule(scheduledTask.getTimerListener(), scheduledTask.getDelay());
|
timer = timerManager.schedule(scheduledTask.getTimerListener(), scheduledTask.getDelay());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (scheduledTask.isFixedRate()) {
|
if (scheduledTask.isFixedRate()) {
|
||||||
timer = this.timerManager
|
timer = timerManager.scheduleAtFixedRate(
|
||||||
.scheduleAtFixedRate(scheduledTask.getTimerListener(), scheduledTask.getDelay(),
|
scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod());
|
||||||
scheduledTask.getPeriod());
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
timer = this.timerManager.schedule(scheduledTask.getTimerListener(), scheduledTask.getDelay(),
|
timer = timerManager.schedule(
|
||||||
scheduledTask.getPeriod());
|
scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.timers.add(timer);
|
this.timers.add(timer);
|
||||||
|
|
@ -170,11 +106,12 @@ public class TimerManagerFactoryBean extends JndiLocatorSupport
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
public TimerManager getObject() {
|
public TimerManager getObject() {
|
||||||
return this.timerManager;
|
return getTimerManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<? extends TimerManager> getObjectType() {
|
public Class<? extends TimerManager> getObjectType() {
|
||||||
return (this.timerManager != null ? this.timerManager.getClass() : TimerManager.class);
|
TimerManager timerManager = getTimerManager();
|
||||||
|
return (timerManager != null ? timerManager.getClass() : TimerManager.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSingleton() {
|
public boolean isSingleton() {
|
||||||
|
|
@ -182,41 +119,6 @@ public class TimerManagerFactoryBean extends JndiLocatorSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
// Implementation of Lifecycle interface
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resumes the underlying TimerManager (if not shared).
|
|
||||||
* @see commonj.timers.TimerManager#resume()
|
|
||||||
*/
|
|
||||||
public void start() {
|
|
||||||
if (!this.shared) {
|
|
||||||
this.timerManager.resume();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Suspends the underlying TimerManager (if not shared).
|
|
||||||
* @see commonj.timers.TimerManager#suspend()
|
|
||||||
*/
|
|
||||||
public void stop() {
|
|
||||||
if (!this.shared) {
|
|
||||||
this.timerManager.suspend();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Considers the underlying TimerManager as running if it is
|
|
||||||
* neither suspending nor stopping.
|
|
||||||
* @see commonj.timers.TimerManager#isSuspending()
|
|
||||||
* @see commonj.timers.TimerManager#isStopping()
|
|
||||||
*/
|
|
||||||
public boolean isRunning() {
|
|
||||||
return (!this.timerManager.isSuspending() && !this.timerManager.isStopping());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Implementation of DisposableBean interface
|
// Implementation of DisposableBean interface
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
@ -227,6 +129,7 @@ public class TimerManagerFactoryBean extends JndiLocatorSupport
|
||||||
* @see commonj.timers.Timer#cancel()
|
* @see commonj.timers.Timer#cancel()
|
||||||
* @see commonj.timers.TimerManager#stop()
|
* @see commonj.timers.TimerManager#stop()
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
// Cancel all registered timers.
|
// Cancel all registered timers.
|
||||||
for (Timer timer : this.timers) {
|
for (Timer timer : this.timers) {
|
||||||
|
|
@ -239,11 +142,8 @@ public class TimerManagerFactoryBean extends JndiLocatorSupport
|
||||||
}
|
}
|
||||||
this.timers.clear();
|
this.timers.clear();
|
||||||
|
|
||||||
// Stop the entire TimerManager, if necessary.
|
// Stop the TimerManager itself.
|
||||||
if (!this.shared) {
|
super.destroy();
|
||||||
// May return early, but at least we already cancelled all known Timers.
|
|
||||||
this.timerManager.stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,161 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.commonj;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.Delayed;
|
||||||
|
import java.util.concurrent.FutureTask;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import commonj.timers.Timer;
|
||||||
|
import commonj.timers.TimerListener;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
import org.springframework.scheduling.Trigger;
|
||||||
|
import org.springframework.scheduling.support.SimpleTriggerContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of Spring's {@link TaskScheduler} interface, wrapping
|
||||||
|
* a CommonJ {@link commonj.timers.TimerManager}.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class TimerManagerTaskScheduler extends TimerManagerAccessor implements TaskScheduler {
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Trigger trigger) {
|
||||||
|
return new ReschedulingTimerListener(task, trigger).schedule();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Date startTime) {
|
||||||
|
TimerScheduledFuture futureTask = new TimerScheduledFuture(task);
|
||||||
|
Timer timer = getTimerManager().schedule(futureTask, startTime);
|
||||||
|
futureTask.setTimer(timer);
|
||||||
|
return futureTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period) {
|
||||||
|
TimerScheduledFuture futureTask = new TimerScheduledFuture(task);
|
||||||
|
Timer timer = getTimerManager().scheduleAtFixedRate(futureTask, startTime, period);
|
||||||
|
futureTask.setTimer(timer);
|
||||||
|
return futureTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) {
|
||||||
|
TimerScheduledFuture futureTask = new TimerScheduledFuture(task);
|
||||||
|
Timer timer = getTimerManager().scheduleAtFixedRate(futureTask, 0, period);
|
||||||
|
futureTask.setTimer(timer);
|
||||||
|
return futureTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
|
||||||
|
TimerScheduledFuture futureTask = new TimerScheduledFuture(task);
|
||||||
|
Timer timer = getTimerManager().schedule(futureTask, startTime, delay);
|
||||||
|
futureTask.setTimer(timer);
|
||||||
|
return futureTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) {
|
||||||
|
TimerScheduledFuture futureTask = new TimerScheduledFuture(task);
|
||||||
|
Timer timer = getTimerManager().schedule(futureTask, 0, delay);
|
||||||
|
futureTask.setTimer(timer);
|
||||||
|
return futureTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ScheduledFuture adapter that wraps a CommonJ Timer.
|
||||||
|
*/
|
||||||
|
private static class TimerScheduledFuture extends FutureTask<Object> implements TimerListener, ScheduledFuture<Object> {
|
||||||
|
|
||||||
|
protected transient Timer timer;
|
||||||
|
|
||||||
|
protected transient boolean cancelled = false;
|
||||||
|
|
||||||
|
public TimerScheduledFuture(Runnable runnable) {
|
||||||
|
super(runnable, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimer(Timer timer) {
|
||||||
|
this.timer = timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void timerExpired(Timer timer) {
|
||||||
|
runAndReset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
boolean result = super.cancel(mayInterruptIfRunning);
|
||||||
|
this.timer.cancel();
|
||||||
|
this.cancelled = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDelay(TimeUnit unit) {
|
||||||
|
return unit.convert(System.currentTimeMillis() - this.timer.getScheduledExecutionTime(), TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compareTo(Delayed other) {
|
||||||
|
if (this == other) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long diff = getDelay(TimeUnit.MILLISECONDS) - other.getDelay(TimeUnit.MILLISECONDS);
|
||||||
|
return (diff == 0 ? 0 : ((diff < 0)? -1 : 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ScheduledFuture adapter for trigger-based rescheduling.
|
||||||
|
*/
|
||||||
|
private class ReschedulingTimerListener extends TimerScheduledFuture {
|
||||||
|
|
||||||
|
private final Trigger trigger;
|
||||||
|
|
||||||
|
private final SimpleTriggerContext triggerContext = new SimpleTriggerContext();
|
||||||
|
|
||||||
|
private volatile Date scheduledExecutionTime;
|
||||||
|
|
||||||
|
public ReschedulingTimerListener(Runnable runnable, Trigger trigger) {
|
||||||
|
super(runnable);
|
||||||
|
this.trigger = trigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture schedule() {
|
||||||
|
this.scheduledExecutionTime = this.trigger.nextExecutionTime(this.triggerContext);
|
||||||
|
if (this.scheduledExecutionTime == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
setTimer(getTimerManager().schedule(this, this.scheduledExecutionTime));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void timerExpired(Timer timer) {
|
||||||
|
Date actualExecutionTime = new Date();
|
||||||
|
super.timerExpired(timer);
|
||||||
|
Date completionTime = new Date();
|
||||||
|
this.triggerContext.update(this.scheduledExecutionTime, actualExecutionTime, completionTime);
|
||||||
|
if (!this.cancelled) {
|
||||||
|
schedule();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,130 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Task scheduler interface that abstracts the scheduling of
|
||||||
|
* {@link Runnable Runnables} based on different kinds of triggers.
|
||||||
|
*
|
||||||
|
* <p>This interface is separate from {@link SchedulingTaskExecutor} since it
|
||||||
|
* usually represents for a different kind of backend, i.e. a thread pool with
|
||||||
|
* different characteristics and capabilities. Implementations may implement
|
||||||
|
* both interfaces if they can handle both kinds of execution characteristics.
|
||||||
|
*
|
||||||
|
* <p>The 'default' implementation is
|
||||||
|
* {@link org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler},
|
||||||
|
* wrapping a native {@link java.util.concurrent.ScheduledExecutorService}
|
||||||
|
* and adding extended trigger capabilities.
|
||||||
|
*
|
||||||
|
* <p>This interface is roughly equivalent to a JSR-236
|
||||||
|
* <code>ManagedScheduledExecutorService</code> as supported in Java EE 6
|
||||||
|
* environments. However, at the time of the Spring 3.0 release, the
|
||||||
|
* JSR-236 interfaces have not been released in official form yet.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see org.springframework.core.task.TaskExecutor
|
||||||
|
* @see java.util.concurrent.ScheduledExecutorService
|
||||||
|
* @see org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
|
||||||
|
*/
|
||||||
|
public interface TaskScheduler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, invoking it whenever the trigger
|
||||||
|
* indicates a next execution time.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param trigger an implementation of the {@link Trigger} interface,
|
||||||
|
* e.g. a {@link org.springframework.scheduling.support.CronTrigger} object
|
||||||
|
* wrapping a cron expression
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task,
|
||||||
|
* or <code>null</code> if the given Trigger object never fires (i.e. returns
|
||||||
|
* <code>null</code> from {@link Trigger#nextExecutionTime})
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
* @see org.springframework.scheduling.support.CronTrigger
|
||||||
|
*/
|
||||||
|
ScheduledFuture schedule(Runnable task, Trigger trigger);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, invoking it at the specified execution time.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param startTime the desired execution time for the task
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
*/
|
||||||
|
ScheduledFuture schedule(Runnable task, Date startTime);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, invoking it at the specified execution time
|
||||||
|
* and subsequently with the given period.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param startTime the desired first execution time for the task
|
||||||
|
* @param period the interval between successive executions of the task
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
*/
|
||||||
|
ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, starting as soon as possible and
|
||||||
|
* invoking it with the given period.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param period the interval between successive executions of the task
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
*/
|
||||||
|
ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, invoking it at the specified execution time
|
||||||
|
* and subsequently with the given delay between the completion of one execution
|
||||||
|
* and the start of the next.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param startTime the desired first execution time for the task
|
||||||
|
* @param delay the delay between the completion of one execution and the start
|
||||||
|
* of the next
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
*/
|
||||||
|
ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule the given {@link Runnable}, starting as soon as possible and
|
||||||
|
* invoking it with the given delay between the completion of one execution
|
||||||
|
* and the start of the next.
|
||||||
|
* <p>Execution will end once the scheduler shuts down or the returned
|
||||||
|
* {@link ScheduledFuture} gets cancelled.
|
||||||
|
* @param task the Runnable to execute whenever the trigger fires
|
||||||
|
* @param delay the interval between successive executions of the task
|
||||||
|
* @return a {@link ScheduledFuture} representing pending completion of the task
|
||||||
|
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
|
||||||
|
*/
|
||||||
|
ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common interface for trigger objects that determine the next execution time
|
||||||
|
* of a task that they get associated with.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see TaskScheduler#schedule(Runnable, Trigger)
|
||||||
|
* @see org.springframework.scheduling.support.CronTrigger
|
||||||
|
*/
|
||||||
|
public interface Trigger {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the next execution time according to the given trigger context.
|
||||||
|
* @param triggerContext context object encapsulating last execution times
|
||||||
|
* and last completion time
|
||||||
|
* @return the next execution time as defined by the trigger,
|
||||||
|
* or <code>null</code> if the trigger won't fire anymore
|
||||||
|
*/
|
||||||
|
Date nextExecutionTime(TriggerContext triggerContext);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context object encapsulating last execution times and last completion time
|
||||||
|
* of a given task.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public interface TriggerContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last <i>scheduled</i> execution time of the task,
|
||||||
|
* or <code>null</code> if not scheduled before.
|
||||||
|
*/
|
||||||
|
Date lastScheduledExecutionTime();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last <i>actual</i> execution time of the task,
|
||||||
|
* or <code>null</code> if not scheduled before.
|
||||||
|
*/
|
||||||
|
Date lastActualExecutionTime();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last completion time of the task,
|
||||||
|
* or <code>null</code> if not scheduled before.
|
||||||
|
*/
|
||||||
|
Date lastCompletionTime();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,162 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.concurrent;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.springframework.core.task.TaskRejectedException;
|
||||||
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
import org.springframework.scheduling.Trigger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapter that takes a JDK 1.5 <code>java.util.concurrent.ScheduledExecutorService</code>
|
||||||
|
* and exposes a Spring {@link org.springframework.scheduling.TaskScheduler} for it.
|
||||||
|
* Extends {@link ConcurrentTaskExecutor} in order to implement the
|
||||||
|
* {@link org.springframework.scheduling.SchedulingTaskExecutor} interface as well.
|
||||||
|
*
|
||||||
|
* <p>Note that there is a pre-built {@link ThreadPoolTaskScheduler} that allows for
|
||||||
|
* defining a JDK 1.5 {@link java.util.concurrent.ScheduledThreadPoolExecutor} in bean style,
|
||||||
|
* exposing it as a Spring {@link org.springframework.scheduling.TaskScheduler} directly.
|
||||||
|
* This is a convenient alternative to a raw ScheduledThreadPoolExecutor definition with
|
||||||
|
* a separate definition of the present adapter class.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see java.util.concurrent.ScheduledExecutorService
|
||||||
|
* @see java.util.concurrent.ScheduledThreadPoolExecutor
|
||||||
|
* @see java.util.concurrent.Executors
|
||||||
|
* @see ThreadPoolTaskScheduler
|
||||||
|
*/
|
||||||
|
public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements TaskScheduler {
|
||||||
|
|
||||||
|
private ScheduledExecutorService scheduledExecutor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ConcurrentTaskScheduler,
|
||||||
|
* using a single thread executor as default.
|
||||||
|
* @see java.util.concurrent.Executors#newSingleThreadScheduledExecutor()
|
||||||
|
*/
|
||||||
|
public ConcurrentTaskScheduler() {
|
||||||
|
super();
|
||||||
|
setScheduledExecutor(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ConcurrentTaskScheduler,
|
||||||
|
* using the given JDK 1.5 executor as shared delegate.
|
||||||
|
* @param scheduledExecutor the JDK 1.5 scheduled executor to delegate to
|
||||||
|
* for {@link org.springframework.scheduling.SchedulingTaskExecutor} as well
|
||||||
|
* as {@link TaskScheduler} invocations
|
||||||
|
*/
|
||||||
|
public ConcurrentTaskScheduler(ScheduledExecutorService scheduledExecutor) {
|
||||||
|
super(scheduledExecutor);
|
||||||
|
setScheduledExecutor(scheduledExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ConcurrentTaskScheduler,
|
||||||
|
* using the given JDK 1.5 executors as delegates.
|
||||||
|
* @param concurrentExecutor the JDK 1.5 concurrent executor to delegate to
|
||||||
|
* for {@link org.springframework.scheduling.SchedulingTaskExecutor} invocations
|
||||||
|
* @param scheduledExecutor the JDK 1.5 scheduled executor to delegate to
|
||||||
|
* for {@link TaskScheduler} invocations
|
||||||
|
*/
|
||||||
|
public ConcurrentTaskScheduler(Executor concurrentExecutor, ScheduledExecutorService scheduledExecutor) {
|
||||||
|
super(concurrentExecutor);
|
||||||
|
setScheduledExecutor(scheduledExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify the JDK 1.5 scheduled executor to delegate to.
|
||||||
|
* <p>Note: This will only apply to {@link TaskScheduler} invocations.
|
||||||
|
* If you want the given executor to apply to
|
||||||
|
* {@link org.springframework.scheduling.SchedulingTaskExecutor} invocations
|
||||||
|
* as well, pass the same executor reference to {@link #setConcurrentExecutor}.
|
||||||
|
* @see #setConcurrentExecutor
|
||||||
|
*/
|
||||||
|
public final void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
|
||||||
|
this.scheduledExecutor =
|
||||||
|
(scheduledExecutor != null ? scheduledExecutor : Executors.newSingleThreadScheduledExecutor());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Trigger trigger) {
|
||||||
|
try {
|
||||||
|
return new ReschedulingRunnable(task, trigger, this.scheduledExecutor).schedule();
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Date startTime) {
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return this.scheduledExecutor.schedule(task, initialDelay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period) {
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return this.scheduledExecutor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) {
|
||||||
|
try {
|
||||||
|
return this.scheduledExecutor.scheduleAtFixedRate(task, 0, period, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return this.scheduledExecutor.scheduleWithFixedDelay(task, initialDelay, delay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) {
|
||||||
|
try {
|
||||||
|
return this.scheduledExecutor.scheduleWithFixedDelay(task, 0, delay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,115 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.concurrent;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.Delayed;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.Trigger;
|
||||||
|
import org.springframework.scheduling.support.DelegatingExceptionProofRunnable;
|
||||||
|
import org.springframework.scheduling.support.SimpleTriggerContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal adapter that reschedules an underlying {@link Runnable} according
|
||||||
|
* to the next execution time suggested by a given {@link Trigger}.
|
||||||
|
*
|
||||||
|
* <p>Necessary because a native {@link ScheduledExecutorService} supports
|
||||||
|
* delay-driven execution only. The flexibility of the {@link Trigger} interface
|
||||||
|
* will be translated onto a delay for the next execution time (repeatedly).
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
class ReschedulingRunnable extends DelegatingExceptionProofRunnable implements ScheduledFuture<Object> {
|
||||||
|
|
||||||
|
private final Trigger trigger;
|
||||||
|
|
||||||
|
private final SimpleTriggerContext triggerContext = new SimpleTriggerContext();
|
||||||
|
|
||||||
|
private final ScheduledExecutorService executor;
|
||||||
|
|
||||||
|
private volatile ScheduledFuture currentFuture;
|
||||||
|
|
||||||
|
private volatile Date scheduledExecutionTime;
|
||||||
|
|
||||||
|
|
||||||
|
public ReschedulingRunnable(Runnable delegate, Trigger trigger, ScheduledExecutorService executor) {
|
||||||
|
super(delegate);
|
||||||
|
this.trigger = trigger;
|
||||||
|
this.executor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ScheduledFuture schedule() {
|
||||||
|
this.scheduledExecutionTime = this.trigger.nextExecutionTime(this.triggerContext);
|
||||||
|
if (this.scheduledExecutionTime == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
long initialDelay = this.scheduledExecutionTime.getTime() - System.currentTimeMillis();
|
||||||
|
this.currentFuture = this.executor.schedule(this, initialDelay, TimeUnit.MILLISECONDS);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Date actualExecutionTime = new Date();
|
||||||
|
getDelegate().run();
|
||||||
|
Date completionTime = new Date();
|
||||||
|
this.triggerContext.update(this.scheduledExecutionTime, actualExecutionTime, completionTime);
|
||||||
|
if (!this.currentFuture.isCancelled()) {
|
||||||
|
schedule();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return this.currentFuture.cancel(mayInterruptIfRunning);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return this.currentFuture.isCancelled();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDone() {
|
||||||
|
return this.currentFuture.isDone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get() throws InterruptedException, ExecutionException {
|
||||||
|
return this.currentFuture.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
return this.currentFuture.get(timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDelay(TimeUnit unit) {
|
||||||
|
return this.currentFuture.getDelay(unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compareTo(Delayed other) {
|
||||||
|
if (this == other) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long diff = getDelay(TimeUnit.MILLISECONDS) - other.getDelay(TimeUnit.MILLISECONDS);
|
||||||
|
return (diff == 0 ? 0 : ((diff < 0)? -1 : 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,206 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.concurrent;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.springframework.core.task.TaskRejectedException;
|
||||||
|
import org.springframework.scheduling.SchedulingTaskExecutor;
|
||||||
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
import org.springframework.scheduling.Trigger;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of Spring's {@link TaskScheduler} interface, wrapping
|
||||||
|
* a native {@link java.util.concurrent.ScheduledThreadPoolExecutor}.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see #setPoolSize
|
||||||
|
* @see #setThreadFactory
|
||||||
|
*/
|
||||||
|
public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
|
implements TaskScheduler, SchedulingTaskExecutor {
|
||||||
|
|
||||||
|
private int poolSize = 1;
|
||||||
|
|
||||||
|
private ScheduledExecutorService scheduledExecutor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the ScheduledExecutorService's pool size.
|
||||||
|
* Default is 1.
|
||||||
|
*/
|
||||||
|
public void setPoolSize(int poolSize) {
|
||||||
|
Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher");
|
||||||
|
this.poolSize = poolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected ExecutorService initializeExecutor(
|
||||||
|
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||||
|
|
||||||
|
this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
||||||
|
return this.scheduledExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ScheduledExecutorService} instance.
|
||||||
|
* <p>The default implementation creates a {@link ScheduledThreadPoolExecutor}.
|
||||||
|
* Can be overridden in subclasses to provide custom {@link ScheduledExecutorService} instances.
|
||||||
|
* @param poolSize the specified pool size
|
||||||
|
* @param threadFactory the ThreadFactory to use
|
||||||
|
* @param rejectedExecutionHandler the RejectedExecutionHandler to use
|
||||||
|
* @return a new ScheduledExecutorService instance
|
||||||
|
* @see #afterPropertiesSet()
|
||||||
|
* @see java.util.concurrent.ScheduledThreadPoolExecutor
|
||||||
|
*/
|
||||||
|
protected ScheduledExecutorService createExecutor(
|
||||||
|
int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||||
|
|
||||||
|
return new ScheduledThreadPoolExecutor(poolSize, threadFactory, rejectedExecutionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the underlying ScheduledExecutorService for native access.
|
||||||
|
* @return the underlying ScheduledExecutorService (never <code>null</code>)
|
||||||
|
* @throws IllegalStateException if the ThreadPoolTaskScheduler hasn't been initialized yet
|
||||||
|
*/
|
||||||
|
public ScheduledExecutorService getScheduledExecutor() throws IllegalStateException {
|
||||||
|
Assert.state(this.scheduledExecutor != null, "ThreadPoolTaskScheduler not initialized");
|
||||||
|
return this.scheduledExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SchedulingTaskExecutor implementation
|
||||||
|
|
||||||
|
public void execute(Runnable task) {
|
||||||
|
Executor executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
executor.execute(task);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Runnable task, long startTimeout) {
|
||||||
|
execute(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Future<?> submit(Runnable task) {
|
||||||
|
ExecutorService executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
return executor.submit(task);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Future<T> submit(Callable<T> task) {
|
||||||
|
ExecutorService executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
return executor.submit(task);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean prefersShortLivedTasks() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TaskScheduler implementation
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Trigger trigger) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
return new ReschedulingRunnable(task, trigger, executor).schedule();
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture schedule(Runnable task, Date startTime) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return executor.schedule(task, initialDelay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
return executor.scheduleAtFixedRate(task, 0, period, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
long initialDelay = startTime.getTime() - System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
return executor.scheduleWithFixedDelay(task, initialDelay, delay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) {
|
||||||
|
ScheduledExecutorService executor = getScheduledExecutor();
|
||||||
|
try {
|
||||||
|
return executor.scheduleWithFixedDelay(task, 0, delay, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
catch (RejectedExecutionException ex) {
|
||||||
|
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,286 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.support;
|
||||||
|
|
||||||
|
import java.util.BitSet;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date sequence generator for a <a
|
||||||
|
* href="http://www.manpagez.com/man/5/crontab/">Crontab pattern</a> allowing
|
||||||
|
* client to specify a pattern that the sequence matches. The pattern is a list
|
||||||
|
* of 6 single space separated fields representing (second, minute, hour, day,
|
||||||
|
* month, weekday). Month and weekday names can be given as the first three
|
||||||
|
* letters of the English names.<br/>
|
||||||
|
* <br/>
|
||||||
|
*
|
||||||
|
* Example patterns
|
||||||
|
* <ul>
|
||||||
|
* <li>"0 0 * * * *" = the top of every hour of every day.</li>
|
||||||
|
* <li>"*/10 * * * * *" = every ten seconds.</li>
|
||||||
|
* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>
|
||||||
|
* <li>"0 0 8-10/30 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.</li>
|
||||||
|
* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>
|
||||||
|
* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @author Dave Syer
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see CronTrigger
|
||||||
|
*/
|
||||||
|
public class CronSequenceGenerator {
|
||||||
|
|
||||||
|
private final BitSet seconds = new BitSet(60);
|
||||||
|
|
||||||
|
private final BitSet minutes = new BitSet(60);
|
||||||
|
|
||||||
|
private final BitSet hours = new BitSet(24);
|
||||||
|
|
||||||
|
private final BitSet daysOfWeek = new BitSet(7);
|
||||||
|
|
||||||
|
private final BitSet daysOfMonth = new BitSet(31);
|
||||||
|
|
||||||
|
private final BitSet months = new BitSet(12);
|
||||||
|
|
||||||
|
private final String expression;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a {@link CronSequenceGenerator} from the pattern provided.
|
||||||
|
* @param expression a space-separated list of time fields
|
||||||
|
* @throws IllegalArgumentException if the pattern cannot be parsed
|
||||||
|
*/
|
||||||
|
public CronSequenceGenerator(String expression) {
|
||||||
|
this.expression = expression;
|
||||||
|
parse(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next {@link Date} in the sequence matching the Cron pattern and
|
||||||
|
* after the value provided. The return value will have a whole number of
|
||||||
|
* seconds, and will be after the input value.
|
||||||
|
* @param date a seed value
|
||||||
|
* @return the next value matching the pattern
|
||||||
|
*/
|
||||||
|
public Date next(Date date) {
|
||||||
|
Calendar calendar = new GregorianCalendar();
|
||||||
|
calendar.setTime(date);
|
||||||
|
|
||||||
|
// Truncate to the next whole second
|
||||||
|
calendar.add(Calendar.SECOND, 1);
|
||||||
|
calendar.set(Calendar.MILLISECOND, 0);
|
||||||
|
|
||||||
|
int second = calendar.get(Calendar.SECOND);
|
||||||
|
findNext(this.seconds, second, 60, calendar, Calendar.SECOND);
|
||||||
|
|
||||||
|
int minute = calendar.get(Calendar.MINUTE);
|
||||||
|
findNext(this.minutes, minute, 60, calendar, Calendar.MINUTE, Calendar.SECOND);
|
||||||
|
|
||||||
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
|
findNext(this.hours, hour, 24, calendar, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND);
|
||||||
|
|
||||||
|
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
||||||
|
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
|
findNextDay(calendar, this.daysOfMonth, dayOfMonth, daysOfWeek, dayOfWeek, 366);
|
||||||
|
|
||||||
|
int month = calendar.get(Calendar.MONTH);
|
||||||
|
findNext(this.months, month, 12, calendar, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY,
|
||||||
|
Calendar.MINUTE, Calendar.SECOND);
|
||||||
|
|
||||||
|
return calendar.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void findNextDay(
|
||||||
|
Calendar calendar, BitSet daysOfMonth, int dayOfMonth, BitSet daysOfWeek, int dayOfWeek, int max) {
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
// the DAY_OF_WEEK values in java.util.Calendar start with 1 (Sunday),
|
||||||
|
// but in the cron pattern, they start with 0, so we subtract 1 here
|
||||||
|
while ((!daysOfMonth.get(dayOfMonth) || !daysOfWeek.get(dayOfWeek-1)) && count++ < max) {
|
||||||
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||||
|
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
|
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
||||||
|
reset(calendar, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND);
|
||||||
|
}
|
||||||
|
if (count > max) {
|
||||||
|
throw new IllegalStateException("Overflow in day for expression=" + this.expression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search the bits provided for the next set bit after the value provided,
|
||||||
|
* and reset the calendar.
|
||||||
|
* @param bits a {@link BitSet} representing the allowed values of the field
|
||||||
|
* @param value the current value of the field
|
||||||
|
* @param max the largest value that the field can have
|
||||||
|
* @param calendar the calendar to increment as we move through the bits
|
||||||
|
* @param field the field to increment in the calendar (@see
|
||||||
|
* {@link Calendar} for the static constants defining valid fields)
|
||||||
|
* @param lowerOrders the Calendar field ids that should be reset (i.e. the
|
||||||
|
* ones of lower significance than the field of interest)
|
||||||
|
* @return the value of the calendar field that is next in the sequence
|
||||||
|
*/
|
||||||
|
private void findNext(BitSet bits, int value, int max, Calendar calendar, int field, int... lowerOrders) {
|
||||||
|
int nextValue = bits.nextSetBit(value);
|
||||||
|
//roll over if needed
|
||||||
|
if (nextValue == -1) {
|
||||||
|
calendar.add(field, max - value);
|
||||||
|
nextValue = bits.nextSetBit(0);
|
||||||
|
}
|
||||||
|
if (nextValue != value) {
|
||||||
|
calendar.set(field, nextValue);
|
||||||
|
reset(calendar, lowerOrders);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the calendar setting all the fields provided to zero.
|
||||||
|
*/
|
||||||
|
private void reset(Calendar calendar, int... fields) {
|
||||||
|
for (int field : fields) {
|
||||||
|
calendar.set(field, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Parsing logic invoked by the constructor.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the given pattern expression.
|
||||||
|
*/
|
||||||
|
private void parse(String expression) throws IllegalArgumentException {
|
||||||
|
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
|
||||||
|
if (fields.length != 6) {
|
||||||
|
throw new IllegalArgumentException(String.format(""
|
||||||
|
+ "cron expression must consist of 6 fields (found %d in %s)", fields.length, expression));
|
||||||
|
}
|
||||||
|
setNumberHits(this.seconds, fields[0], 60);
|
||||||
|
setNumberHits(this.minutes, fields[1], 60);
|
||||||
|
setNumberHits(this.hours, fields[2], 24);
|
||||||
|
setDaysOfMonth(this.daysOfMonth, fields[3], 31);
|
||||||
|
setNumberHits(this.months, replaceOrdinals(fields[4], "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"), 12);
|
||||||
|
setDays(this.daysOfWeek, replaceOrdinals(fields[5], "SUN,MON,TUE,WED,THU,FRI,SAT"), 8);
|
||||||
|
if (this.daysOfWeek.get(7)) {
|
||||||
|
// Sunday can be represented as 0 or 7
|
||||||
|
this.daysOfWeek.set(0);
|
||||||
|
this.daysOfWeek.clear(7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the values in the commaSeparatedList (case insensitive) with
|
||||||
|
* their index in the list.
|
||||||
|
* @return a new string with the values from the list replaced
|
||||||
|
*/
|
||||||
|
private String replaceOrdinals(String value, String commaSeparatedList) {
|
||||||
|
String[] list = StringUtils.commaDelimitedListToStringArray(commaSeparatedList);
|
||||||
|
for (int i = 0; i < list.length; i++) {
|
||||||
|
String item = list[i].toUpperCase();
|
||||||
|
value = StringUtils.replace(value.toUpperCase(), item, "" + i);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDaysOfMonth(BitSet bits, String field, int max) {
|
||||||
|
// Days of month start with 1 (in Cron and Calendar) so add one
|
||||||
|
setDays(bits, field, max+1);
|
||||||
|
// ... and remove it from the front
|
||||||
|
bits.clear(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDays(BitSet bits, String field, int max) {
|
||||||
|
if (field.contains("?")) {
|
||||||
|
field = "*";
|
||||||
|
}
|
||||||
|
setNumberHits(bits, field, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNumberHits(BitSet bits, String value, int max) {
|
||||||
|
String[] fields = StringUtils.delimitedListToStringArray(value, ",");
|
||||||
|
for (String field : fields) {
|
||||||
|
if (!field.contains("/")) {
|
||||||
|
// Not an incrementer so it must be a range (possibly empty)
|
||||||
|
int[] range = getRange(field, max);
|
||||||
|
bits.set(range[0], range[1] + 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String[] split = StringUtils.delimitedListToStringArray(field, "/");
|
||||||
|
if (split.length > 2) {
|
||||||
|
throw new IllegalArgumentException("Incrementer has more than two fields: " + field);
|
||||||
|
}
|
||||||
|
int[] range = getRange(split[0], max);
|
||||||
|
if (!split[0].contains("-")) {
|
||||||
|
range[1] = max - 1;
|
||||||
|
}
|
||||||
|
int delta = Integer.valueOf(split[1]);
|
||||||
|
for (int i = range[0]; i <= range[1]; i += delta) {
|
||||||
|
bits.set(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] getRange(String field, int max) {
|
||||||
|
int[] result = new int[2];
|
||||||
|
if (field.contains("*")) {
|
||||||
|
result[0] = 0;
|
||||||
|
result[1] = max-1;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (!field.contains("-")) {
|
||||||
|
result[0] = result[1] = Integer.valueOf(field);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String[] split = StringUtils.delimitedListToStringArray(field, "-");
|
||||||
|
if (split.length > 2) {
|
||||||
|
throw new IllegalArgumentException("Range has more than two fields: " + field);
|
||||||
|
}
|
||||||
|
result[0] = Integer.valueOf(split[0]);
|
||||||
|
result[1] = Integer.valueOf(split[1]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (!(obj instanceof CronSequenceGenerator)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
CronSequenceGenerator cron = (CronSequenceGenerator) obj;
|
||||||
|
return cron.months.equals(months) && cron.daysOfMonth.equals(daysOfMonth) && cron.daysOfWeek.equals(daysOfWeek)
|
||||||
|
&& cron.hours.equals(hours) && cron.minutes.equals(minutes) && cron.seconds.equals(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return 37 + 17 * months.hashCode() + 29 * daysOfMonth.hashCode() + 37 * daysOfWeek.hashCode() + 41
|
||||||
|
* hours.hashCode() + 53 * minutes.hashCode() + 61 * seconds.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getClass().getSimpleName() + ": " + expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.support;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.Trigger;
|
||||||
|
import org.springframework.scheduling.TriggerContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Trigger} implementation for cron expressions.
|
||||||
|
* Wraps a {@link CronSequenceGenerator}.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
* @see CronSequenceGenerator
|
||||||
|
*/
|
||||||
|
public class CronTrigger implements Trigger {
|
||||||
|
|
||||||
|
private final CronSequenceGenerator sequenceGenerator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a {@link CronTrigger} from the pattern provided.
|
||||||
|
* @param cronExpression a space-separated list of time fields,
|
||||||
|
* following cron expression conventions
|
||||||
|
*/
|
||||||
|
public CronTrigger(String cronExpression) {
|
||||||
|
this.sequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Date nextExecutionTime(TriggerContext triggerContext) {
|
||||||
|
Date date = triggerContext.lastCompletionTime();
|
||||||
|
if (date == null) {
|
||||||
|
date = new Date();
|
||||||
|
}
|
||||||
|
return this.sequenceGenerator.next(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (this == obj ||
|
||||||
|
(obj instanceof CronTrigger &&
|
||||||
|
this.sequenceGenerator.equals(((CronTrigger) obj).sequenceGenerator)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return this.sequenceGenerator.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.support;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.TriggerContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple data holder implementation of the {@link TriggerContext} interface.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class SimpleTriggerContext implements TriggerContext {
|
||||||
|
|
||||||
|
private volatile Date lastScheduledExecutionTime;
|
||||||
|
|
||||||
|
private volatile Date lastActualExecutionTime;
|
||||||
|
|
||||||
|
private volatile Date lastCompletionTime;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update this holder's state with the latest time values.
|
||||||
|
* @param lastScheduledExecutionTime last <i>scheduled</i> execution time
|
||||||
|
* @param lastActualExecutionTime last <i>actual</i> execution time
|
||||||
|
* @param lastCompletionTime last completion time
|
||||||
|
*/
|
||||||
|
public void update(Date lastScheduledExecutionTime, Date lastActualExecutionTime, Date lastCompletionTime) {
|
||||||
|
this.lastScheduledExecutionTime = lastScheduledExecutionTime;
|
||||||
|
this.lastActualExecutionTime = lastActualExecutionTime;
|
||||||
|
this.lastCompletionTime = lastCompletionTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Date lastScheduledExecutionTime() {
|
||||||
|
return this.lastScheduledExecutionTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date lastActualExecutionTime() {
|
||||||
|
return this.lastActualExecutionTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date lastCompletionTime() {
|
||||||
|
return this.lastCompletionTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue