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 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#addCronTask(CronTask) 030 */ 031public class CronTask extends TriggerTask { 032 033 private final String expression; 034 035 036 /** 037 * Create a new {@code CronTask}. 038 * @param runnable the underlying task to execute 039 * @param expression the cron expression defining when the task should be executed 040 */ 041 public CronTask(Runnable runnable, String expression) { 042 this(runnable, new CronTrigger(expression)); 043 } 044 045 /** 046 * Create a new {@code CronTask}. 047 * @param runnable the underlying task to execute 048 * @param cronTrigger the cron trigger defining when the task should be executed 049 */ 050 public CronTask(Runnable runnable, CronTrigger cronTrigger) { 051 super(runnable, cronTrigger); 052 this.expression = cronTrigger.getExpression(); 053 } 054 055 056 /** 057 * Return the cron expression defining when the task should be executed. 058 */ 059 public String getExpression() { 060 return this.expression; 061 } 062 063}