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 019import org.springframework.scheduling.support.CronTrigger; 020 021/** 022 * {@link TriggerTask} implementation defining a {@code Runnable} to be executed according 023 * to a {@linkplain org.springframework.scheduling.support.CronSequenceGenerator standard 024 * cron expression}. 025 * 026 * @author Chris Beams 027 * @since 3.2 028 * @see org.springframework.scheduling.annotation.Scheduled#cron() 029 * @see ScheduledTaskRegistrar#setCronTasksList(java.util.List) 030 * @see org.springframework.scheduling.TaskScheduler 031 */ 032public class CronTask extends TriggerTask { 033 034 private final String expression; 035 036 037 /** 038 * Create a new {@code CronTask}. 039 * @param runnable the underlying task to execute 040 * @param expression cron expression defining when the task should be executed 041 */ 042 public CronTask(Runnable runnable, String expression) { 043 this(runnable, new CronTrigger(expression)); 044 } 045 046 /** 047 * Create a new {@code CronTask}. 048 * @param runnable the underlying task to execute 049 * @param cronTrigger the cron trigger defining when the task should be executed 050 */ 051 public CronTask(Runnable runnable, CronTrigger cronTrigger) { 052 super(runnable, cronTrigger); 053 this.expression = cronTrigger.getExpression(); 054 } 055 056 057 public String getExpression() { 058 return this.expression; 059 } 060 061}