当调度程序在 Spring Boot 应用程序中启动时,我可以获得以毫秒为单位的时间吗

发布于 2025-01-16 22:40:45 字数 135 浏览 2 评论 0原文

我有一个方法,它将以 2 小时的间隔运行,并在 Spring boot 应用程序中的 Scheduler 的 cron 中提到。 现在我的问题是我可以获得调度程序运行时的时间(以毫秒为单位)吗?

我试图获取时间,但在调度程序运行时无法获取它。

I have a method which will be running at an interval of 2 hrs and is mentioned in cron of Scheduler in Spring boot application.
Now my question is can I get the time in milliseconds when the scheduler ran?

I tried to get the time but not able to fetch it when the scheduler ran.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2025-01-23 22:40:46

Spring调度器指南给出了类似的例子,请参阅链接:https://spring.io /guides/gs/scheduling-tasks/

您可以将时间记录到日志文件中,但正如托马斯提到的,确切的执行时间可能是在几毫秒之后(与机器和线程调度程序以及线程执行日志任务或打印到输出流所需的时间有关) 。

import java.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

@Scheduled(fixedRate = 1000)
public void reportCurrentTime() {
    log.info("The time is now {}", LocalDateTime.now());
    // System.out.println(LocalDateTime.now());
}
}

The Spring guide of scheduler give a similar example , see the link : https://spring.io/guides/gs/scheduling-tasks/ .

You can log the time to a logFile but as Thomas mention the exact time of execution may be after some millisonconds (related with the machine and the thread scheduler and the time needed by the thread to do the task of log or print to the outputStream).

import java.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

@Scheduled(fixedRate = 1000)
public void reportCurrentTime() {
    log.info("The time is now {}", LocalDateTime.now());
    // System.out.println(LocalDateTime.now());
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文