alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (TimerTaskExecutor.java)

This example Spring Framework source code file (TimerTaskExecutor.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

cancelling, delegatingtimertask, disposablebean, disposablebean, initializing, initializing, initializingbean, log, schedulingtaskexecutor, timer, timer, timertaskexecutor, timertaskexecutor, util

The Spring Framework TimerTaskExecutor.java source code

/*
 * 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
	 */
	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) 
	 * @see TimerFactoryBean
	 */
	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	/**
	 * Set the delay to use for scheduling tasks passed into the
	 * <code>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 <code>afterPropertiesSet
	 * 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,
     * 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();
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework TimerTaskExecutor.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.