SpringBoot定时任务(Spring Schedule )实现方法
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); fdf.format(new Date())
import java.util.Date; import org.apache.commons.lang3.time.FastDateFormat; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.dudu.constant.Constant; @Component public class ScheduleJobs { public final static long SECOND = 1 * 1000; FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); /** * 固定等待时间 @Scheduled(fixedDelay = 时间间隔 ) * @throws InterruptedException */ @Scheduled(fixedDelay = SECOND * 2) public void fixedDelayJob() throws InterruptedException { Constant.LOGGER.info("[FixedDelayJob Execute]"+fdf.format(new Date())); } /** * 固定间隔时间 @Scheduled(fixedRate = 时间间隔 ) */ @Scheduled(fixedRate = SECOND * 4) public void fixedRateJob() { Constant.LOGGER.info("[FixedRateJob Execute]"+fdf.format(new Date())); } /** * Corn表达式 @Scheduled(cron = Corn表达式) */ @Scheduled(cron = "0/4 * * * * ?") public void cronJob() { Constant.LOGGER.info("[CronJob Execute]"+fdf.format(new Date())); } }
看完打开支付宝扫一扫领个红包吧!