001/*
002 * Copyright 2002-2017 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
019import java.util.concurrent.ScheduledFuture;
020
021import org.springframework.lang.Nullable;
022
023/**
024 * A representation of a scheduled task at runtime,
025 * used as a return value for scheduling methods.
026 *
027 * @author Juergen Hoeller
028 * @since 4.3
029 * @see ScheduledTaskRegistrar#scheduleCronTask(CronTask)
030 * @see ScheduledTaskRegistrar#scheduleFixedRateTask(FixedRateTask)
031 * @see ScheduledTaskRegistrar#scheduleFixedDelayTask(FixedDelayTask)
032 */
033public final class ScheduledTask {
034
035        private final Task task;
036
037        @Nullable
038        volatile ScheduledFuture<?> future;
039
040
041        ScheduledTask(Task task) {
042                this.task = task;
043        }
044
045
046        /**
047         * Return the underlying task (typically a {@link CronTask},
048         * {@link FixedRateTask} or {@link FixedDelayTask}).
049         * @since 5.0.2
050         */
051        public Task getTask() {
052                return this.task;
053        }
054
055        /**
056         * Trigger cancellation of this scheduled task.
057         */
058        public void cancel() {
059                ScheduledFuture<?> future = this.future;
060                if (future != null) {
061                        future.cancel(true);
062                }
063        }
064
065        @Override
066        public String toString() {
067                return this.task.toString();
068        }
069
070}