求一位感兴趣的,聊聊,有参考代码,所以能看懂,就好办,不白忙
你直接把你的问题贴出来,不久已经聊起来了,现在都没人回复你
如果你是springBoot项目,可以直接写一个类即可,使用到的是cron表达式,比如下面这样:
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class TimeTask {
//3.添加定时任务 这里设置每秒掉起一次
@Scheduled(cron = "0/5 * * * * ?")
//或直接指定时间间隔,例如:5秒
//@Scheduled(fixedRate=5000)
private void configureTasks() {
System.err.println("执行静态定时任务时间(TimerTask): " + LocalDateTime.now()+ "=======> 要发送邮件了");
// 这里就把你发送邮件的方法加上
}
}
如果你的框架用到了spring,一般项目都会用到,可以这样做。
首先你要在spring配置中引入task命名空间
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
然后编写一个类处理定时任务
public class TimeTask {
public void timeTask(){
System.err.println("执行静态定时任务时间(TimerTask): " + LocalDateTime.now()+ "=======> 要发送邮件了");
// 这里就把你发送邮件的方法加上
}
}
再把这个类注册到spring中,这里有两种方式,一种是单线程,一种是多线程,择一即可,我这里使用多线程的。
<task:scheduler id="scheduler" pool-size="7" />
<bean id="timeTask" class="com.xxc.controller.TimeTask"/>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="timeTask" method="timeTask" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
试试工具类呢 https://hutool.cn/docs/#/