

java | 定时任务
source link: https://benpaodewoniu.github.io/2023/01/03/java166/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java | 定时任务 | 犀牛的博客
姑苏城外一茅屋,万树梅花月满天
让程序每周四 18:00:00
定时执行任务。
package com.redisc;
import lombok.extern.slf4j.Slf4j;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Slf4j(topic = "c.Test")
public class Run {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
// 获取当前时间
LocalDateTime now = LocalDateTime.now();
LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
// 如果当前时间 > 本周四,找下周时间
if (now.compareTo(time) > 0) {
time = time.plusWeeks(1);
}
// initialDelay 代表时间和周四的时间差
long initailDelay = Duration.between(now, time).toMillis();
long period = 1000 * 60 * 60 * 24 * 7;
pool.scheduleWithFixedDelay(() -> {
System.out.println("running...");
}, initailDelay, period, TimeUnit.MILLISECONDS);
}
}
Recommend
-
102
Linux系统的定时任务是由cron(crond)这个系统服务来控制的。Linux系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。另外,由于使用者自己也可以设置计划任务,所以,Linux系统也提供了使用者控制计划任务的命令:crontab命令。一、crond简介crond...
-
233
威联通的机器本来就是基于 Linux 定义的,所以想要定时任务就会想到crontab,在威联通中使用 crontab 必须使用 SSH 登录。 然后基本使用 crontab -l 查看当前 qnap 中已经存在的定时任务。 在大...
-
14
定时任务应用非常广泛,Java提供的现有解决方案有很多。 本次主要讲schedule、quartz、xxl-job、shedlock等相关的代码实践。 一、SpringBoot使用Schedule 核心代码: @Component...
-
11
Quartz 概念Quartz 是任务调度的开源项目。Quartz 就是基于 Java 实现的任务调度框架。 Quartz 运行环境Quartz 可以运行嵌入在两一个独立式应用程序 Quartz 可以在应用程序服务器(或 Sevelet 容器) 内被实例化,并参...
-
9
爱码爱生活 java定时任务Spring Boot提供了@EnableSch...
-
9
本文适用语言:java 序章:定时任务实现方式 当下,java编码过程中,实现定时任务的方式主要以以下两种为主 spring框架的@Scheduled quzrtz框...
-
5
学过定时任务,但是我忘了,忘得一干二净,害怕,一直听别人说: 你写一个定时任务就好了。 写个定时任务让他去爬取就行了。 我不会,所以现在得补回来了,欠下的终究要还的,/(ㄒoㄒ)/~~
-
4
用xxl-job做后台任务管理, 主要是快速解决定时任务的HA问题, 项目代码量不大, 功能精简, 没有特殊依赖. 因为产品中用到了这个项目, 上午花了点时间研究了一下运行机制. 把看到的记一下. <!-- http://repo1.maven...
-
11
V2EX › 程序员 Java 项目,定时任务越来越多
-
19
V2EX › 程序员 Java 定时任务求教 luf...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK