执行计划任务
首先定义要执行的类
@scheulde加在方法上,代表此方法是计划任务
加在类上,代表所有的方法都是计划任务
package com.example.demo.qin.scheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; /** * @program: springboot_test * @description: 计划任务类 * @author: Mr.qin * @create: 2018-12-19 23:24 **/ @Service public class SchedulingTaskService { private static final SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void currentTime(){ System.out.println("每隔五秒执行一次任务"+sd.format(new Date())); } }
之后在启动类上加 上@enableschuduling
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
原文地址:https://www.cnblogs.com/qin-up/p/10152964.html
时间: 2024-10-29 00:26:21