All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.scheduling.timer.TimerTaskExecutor Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * 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 null
	 */
	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}.
	 * 

If not specified, a default {@link Timer} instance will be used. * @param timer the {@link Timer} to use for this {@link TimerTaskExecutor} (may be null) * @see TimerFactoryBean */ public void setTimer(Timer timer) { this.timer = timer; } /** * Set the delay to use for scheduling tasks passed into the * execute 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 afterPropertiesSet * if no {@link Timer} has been specified explicitly. *

Default implementation creates a plain daemon {@link Timer}. *

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 null, * 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(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy