【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
之前我们已经简单实现了一下SpringBoot 整合Quartz
能感觉到,确实有些复杂, 不太好用【Spring 来了】
【开启定时任务】
【做一个定时任务】
package com.dingjiaxiong.quartz;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/*** ClassName: MyBean* date: 2022/10/22 14:08** @author DingJiaxiong*/@Component
public class MyBean {//表明这个方法我要让它定时执行@Scheduled(cron = "0/1 * * * * ?") //每秒执行一次,任意分钟、小时、日、月、星期public void print(){System.out.println("Spring task run ...");}}
这就已经可以了
现在我们的工程中就有两个定时任务了,直接启动服务器!
两个任务笔者都设置的每秒,感觉就在交替执行了
【就是这样!我超!牛逼!!!!!!】
回顾一下
可以加上线程前缀,方便调试,看看效果吧
spring:task:scheduling:thread-name-prefix: spring_task
修改一下定时任务
package com.dingjiaxiong.quartz;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/*** ClassName: MyBean* date: 2022/10/22 14:08** @author DingJiaxiong*/@Component
public class MyBean {//表明这个方法我要让它定时执行@Scheduled(cron = "0/1 * * * * ?") //每秒执行一次,任意分钟、小时、日、月、星期public void print(){System.out.println(Thread.currentThread().getName() + "Spring task run ...");}}
直接启动
就是这个效果,1代表第几个线程