springboot定时任务
简单使用:
类上加上@EnableScheduling注解
方法上加上 @Scheduled注解(参数会指定定时时间,corn表达式...)
springboot定时任务学习 @EnableScheduling @Scheduled 简单例子
@EnableScheduling
1 @Target(ElementType.TYPE) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Import(SchedulingConfiguration.class) 4 @Documented 5 public @interface EnableScheduling { 6 7 }
@EnableScheduling 要求在类上使用。
@EnableScheduling 引入了 SchedulingConfiguration 类,属于 org.springframework.context 模块。
@Scheduled
1 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @Repeatable(Schedules.class) 5 public @interface Scheduled {
@Scheduled 要求在方法、注解中使用。
@Scheduled 要求方法参数为空,返回值类型为void。
@Scheduled 参数:corn...
@Scheduled 源码:
1 /* 2 * Copyright 2002-2016 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.scheduling.annotation; 18 19 import java.lang.annotation.Documented; 20 import java.lang.annotation.ElementType; 21 import java.lang.annotation.Repeatable; 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 import java.lang.annotation.Target; 25 26 /** 27 * An annotation that marks a method to be scheduled. Exactly one of 28 * the {@link #cron()}, {@link #fixedDelay()}, or {@link #fixedRate()} 29 * attributes must be specified. 30 * 31 * <p>The annotated method must expect no arguments. It will typically have 32 * a {@code void} return type; if not, the returned value will be ignored 33 * when called through the scheduler. 34 * 35 * <p>Processing of {@code @Scheduled} annotations is performed by 36 * registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be 37 * done manually or, more conveniently, through the {@code <task:annotation-driven/>} 38 * element or @{@link EnableScheduling} annotation. 39 * 40 * <p>This annotation may be used as a <em>meta-annotation</em> to create custom 41 * <em>composed annotations</em> with attribute overrides. 42 * 43 * @author Mark Fisher 44 * @author Dave Syer 45 * @author Chris Beams 46 * @since 3.0 47 * @see EnableScheduling 48 * @see ScheduledAnnotationBeanPostProcessor 49 * @see Schedules 50 */ 51 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) 52 @Retention(RetentionPolicy.RUNTIME) 53 @Documented 54 @Repeatable(Schedules.class) 55 public @interface Scheduled { 56 57 /** 58 * A cron-like expression, extending the usual UN*X definition to include 59 * triggers on the second as well as minute, hour, day of month, month 60 * and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on 61 * weekdays (at the top of the minute - the 0th second). 62 * @return an expression that can be parsed to a cron schedule 63 * @see org.springframework.scheduling.support.CronSequenceGenerator 64 */ 65 String cron() default ""; 66 67 /** 68 * A time zone for which the cron expression will be resolved. By default, this 69 * attribute is the empty String (i.e. the server's local time zone will be used). 70 * @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)}, 71 * or an empty String to indicate the server's default time zone 72 * @since 4.0 73 * @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone) 74 * @see java.util.TimeZone 75 */ 76 String zone() default ""; 77 78 /** 79 * Execute the annotated method with a fixed period in milliseconds between the 80 * end of the last invocation and the start of the next. 81 * @return the delay in milliseconds 82 */ 83 long fixedDelay() default -1; 84 85 /** 86 * Execute the annotated method with a fixed period in milliseconds between the 87 * end of the last invocation and the start of the next. 88 * @return the delay in milliseconds as a String value, e.g. a placeholder 89 * @since 3.2.2 90 */ 91 String fixedDelayString() default ""; 92 93 /** 94 * Execute the annotated method with a fixed period in milliseconds between 95 * invocations. 96 * @return the period in milliseconds 97 */ 98 long fixedRate() default -1; 99 100 /** 101 * Execute the annotated method with a fixed period in milliseconds between 102 * invocations. 103 * @return the period in milliseconds as a String value, e.g. a placeholder 104 * @since 3.2.2 105 */ 106 String fixedRateString() default ""; 107 108 /** 109 * Number of milliseconds to delay before the first execution of a 110 * {@link #fixedRate()} or {@link #fixedDelay()} task. 111 * @return the initial delay in milliseconds 112 * @since 3.2 113 */ 114 long initialDelay() default -1; 115 116 /** 117 * Number of milliseconds to delay before the first execution of a 118 * {@link #fixedRate()} or {@link #fixedDelay()} task. 119 * @return the initial delay in milliseconds as a String value, e.g. a placeholder 120 * @since 3.2.2 121 */ 122 String initialDelayString() default ""; 123 124 }
简单例子
启动类:
1 package com.wang.pp; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class PPApplicationStarter { 8 9 public static void main(String[] args) { 10 SpringApplication.run(PPApplicationStarter.class, args); 11 } 12 }
配置类:
@Configuration @EnableScheduling public class PpSchedualTask { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Scheduled(cron = "${limitTime.corn}") public void runTask(){ try { Date ss = new Date(); System.out.println(sdf.format(ss)+":"+"定时任务执行"); } catch (Exception e) { e.printStackTrace(); } } }
配置文件:5秒定时执行
limitTime.corn=*/5 * * * * ?
结果打印:
2020-12-18 15:02:55:定时任务执行 2020-12-18 15:03:00:定时任务执行 2020-12-18 15:03:05:定时任务执行 2020-12-18 15:03:10:定时任务执行

浙公网安备 33010602011771号