001/* 002 * Copyright 2002-2013 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.config; 018 019/** 020 * {@link Task} implementation defining a {@code Runnable} to be executed at a given 021 * millisecond interval which may be treated as fixed-rate or fixed-delay depending on 022 * context. 023 * 024 * @author Chris Beams 025 * @since 3.2 026 * @see org.springframework.scheduling.annotation.Scheduled#fixedRate() 027 * @see org.springframework.scheduling.annotation.Scheduled#fixedDelay() 028 * @see ScheduledTaskRegistrar#setFixedRateTasksList(java.util.List) 029 * @see ScheduledTaskRegistrar#setFixedDelayTasksList(java.util.List) 030 * @see org.springframework.scheduling.TaskScheduler 031 */ 032public class IntervalTask extends Task { 033 034 private final long interval; 035 036 private final long initialDelay; 037 038 039 /** 040 * Create a new {@code IntervalTask}. 041 * @param runnable the underlying task to execute 042 * @param interval how often in milliseconds the task should be executed 043 * @param initialDelay initial delay before first execution of the task 044 */ 045 public IntervalTask(Runnable runnable, long interval, long initialDelay) { 046 super(runnable); 047 this.interval = interval; 048 this.initialDelay = initialDelay; 049 } 050 051 /** 052 * Create a new {@code IntervalTask} with no initial delay. 053 * @param runnable the underlying task to execute 054 * @param interval how often in milliseconds the task should be executed 055 */ 056 public IntervalTask(Runnable runnable, long interval) { 057 this(runnable, interval, 0); 058 } 059 060 061 public long getInterval() { 062 return this.interval; 063 } 064 065 public long getInitialDelay() { 066 return this.initialDelay; 067 } 068 069}