Spring3.1开始让计划任务变得非常简单,只需要几个注解就能快速开启计划任务的支持。

@EnableScheduling

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {

}

@EnableScheduling、@Configuration两个同时使用开启计划任务支持。

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@Configuration
public class TaskConfiguration {

}

@Scheduled

在要使用计划任务的方法上使用Scheduled,fixedRate表示固定频率,cron即自定义执行表达式,更多用法参考注解@Scheduled参数。

@Service
public class TestTask {

    protected Logger logger = LoggerUtils.getLogger(this);

    @Scheduled(fixedRate = 5000)
    public void runPerFiveSeconds() {
        logger.info("fix");
    }

    @Scheduled(cron = "0/10 * 9 * * ?")
    public void runCron() {
        logger.info("cron");
    }

}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注