⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timertaskexecutor.java

📁 Spring API核心源代码 Spring API核心源代码 Spring API核心源代码
💻 JAVA
字号:
/*
 * Copyright 2002-2006 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.timer;

import java.util.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.SchedulingTaskExecutor;
import org.springframework.util.Assert;

/**
 * {@link org.springframework.core.task.TaskExecutor} implementation that uses a
 * single {@link Timer} for executing all tasks, effectively resulting in
 * serialized asynchronous execution on a single thread.
 *
 * @author Juergen Hoeller
 * @since 2.0
 * @see java.util.Timer
 */
public class TimerTaskExecutor implements SchedulingTaskExecutor, InitializingBean, DisposableBean {

    /**
     * The shared {@link Log} instance. 
     */
    protected final Log logger = LogFactory.getLog(getClass());

	private Timer timer;

	private int delay = 0;

	private boolean usingAnInternalTimer = false;


	/**
	 * Create a new TimerTaskExecutor that needs to be further
	 * configured and initialized.
	 * @see #setTimer
	 * @see #afterPropertiesSet
	 */
	public TimerTaskExecutor() {
	}

	/**
	 * Create a new TimerTaskExecutor for the given {@link Timer}.
	 * @param timer the {@link Timer} to wrap
	 * @throws IllegalArgumentException if the supplied {@link Timer} is <code>null</code>
	 */
	public TimerTaskExecutor(Timer timer) {
		Assert.notNull(timer, "Timer must not be null");
		this.timer = timer;
	}

	/**
	 * Set the {@link Timer} to use for this {@link TimerTaskExecutor},
	 * for example a shared {@link Timer} instance defined by a
     * {@link TimerFactoryBean}.
	 * <p>If not specified, a default {@link Timer} instance will be used.
     * @param timer the {@link Timer} to use for this {@link TimerTaskExecutor} (may be <code>null</code>) 
	 * @see TimerFactoryBean
	 */
	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	/**
	 * Set the delay to use for scheduling tasks passed into the
	 * <code>execute</code> method. Default is 0.
     * @param delay the delay in milliseconds before the task is to be executed
	 */
	public void setDelay(int delay) {
		this.delay = delay;
	}


	public void afterPropertiesSet() {
		if (this.timer == null) {
			logger.info("Initializing Timer");
			this.timer = createTimer();
			this.usingAnInternalTimer = true;
		}
	}

	/**
	 * Create a new {@link Timer} instance. Called by <code>afterPropertiesSet</code>
	 * if no {@link Timer} has been specified explicitly.
	 * <p>Default implementation creates a plain daemon {@link Timer}.
     * <p>If overridden, subclasses must take care to ensure that a non-null
     * {@link Timer} is returned from the execution of this method.
	 * @return a new {@link Timer} instance
	 * @see #afterPropertiesSet
	 * @see java.util.Timer#Timer(boolean)
	 */
	protected Timer createTimer() {
		return new Timer(true);
	}


	/**
	 * Schedules the given {@link Runnable} on this executor's {@link Timer} instance,
	 * wrapping it in a {@link DelegatingTimerTask}.
     * @param task the task to be executed
	 * @throws IllegalArgumentException if the supplied {@link Runnable task} is <code>null</code>,
     * or if the {@link Timer} encapsulated by this instance has not been set, or
     * if the {@link #setDelay(int) configured delay} is negative.
	 */
	public void execute(Runnable task) {
		Assert.notNull(this.timer, "timer is required");
		this.timer.schedule(new DelegatingTimerTask(task), this.delay);
	}

	/**
	 * This task executor prefers short-lived work units.
	 */
	public boolean prefersShortLivedTasks() {
		return true;
	}


	/**
	 * Cancel the {@link Timer} on bean factory shutdown, stopping all scheduled tasks.
	 * @see java.util.Timer#cancel()
	 */
	public void destroy() {
		if (this.usingAnInternalTimer) {
			logger.info("Cancelling Timer");
			this.timer.cancel();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -