SpringBoot-定时任务
springboot-定时任务
everthing will be okay....
实现结果如下:
- 添加依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
-
添加相关注释
@SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
-
定时任务实现(CRON表达式与自定义时间)
package com.empirefree.component; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * @ProjectName: SchedulerTask * @Package: com.empirefree.component * @Description: * @Author: huyuqiao * @CreateDate: 2020/10/12 16:01 */ @Component public class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?") private void process(){ System.out.println("this is scheduler task runing "+(count++)); } }
package com.empirefree.component; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * @ProjectName: Scheduler2Task * @Package: com.empirefree.component * @Description: * @Author: huyuqiao * @CreateDate: 2020/10/12 16:01 */ @Component public class Scheduler2Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000) public void reportCurrentTime() { System.out.println("现在时间:" + dateFormat.format(new Date())); } }
我曾七次鄙视自己的灵魂:
第一次,当它本可进取时,却故作谦卑;
第二次,当它在空虚时,用爱欲来填充;
第三次,在困难和容易之间,它选择了容易;
第四次,它犯了错,却借由别人也会犯错来宽慰自己;
第五次,它自由软弱,却把它认为是生命的坚韧;
第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副;
第七次,它侧身于生活的污泥中,虽不甘心,却又畏首畏尾。