如何读取@Schedule注释中的整数值?

发布于 2025-01-16 19:43:25 字数 879 浏览 3 评论 0原文

@Schedule 中使用的变量是在其他项目中定义的,但要在另一个项目中使用它 它在类中读取并在其他文件中传入@Scheduled。

该变量在类中定义为:

@Value("${cron_frequency_interval_for_logs}")
int cronFrequencyIntervalForLogs;

并用作

@Scheduled(fixedDelayString = "${cron_frequency_interval_for_logs}")

这工作正常,但所需的时间以毫秒为单位,并且在 String 类型中也是如此。

我想要并尝试的是:想要整数形式的时间并以分钟为单位:

@Value("${cron_frequency_interval_for_logs}")
int cronFrequencyIntervalForLogs;

这个语法是我从互联网复制的,但对我不起作用。

@Scheduled(fixedDelay = "#{new Integer('${cron_frequency_interval_for_logs}')}")

我希望时间以分钟为第一优先级,但通过参考下面的文章,我知道格式将是 5*60*1000 5 分钟,在这种情况下,如果我可以在变量中存储 5 并且可以进行其余的计算,因为这将是重复的。

有什么办法可以做到这一点吗? 提到了这个

The variable used in @Schedule is defined in some other project but to use it in another project
it is read in a class and the passed in @Scheduled in other file.

The variable is defined in the class as:

@Value("${cron_frequency_interval_for_logs}")
int cronFrequencyIntervalForLogs;

and used as

@Scheduled(fixedDelayString = "${cron_frequency_interval_for_logs}")

This works fine but the time it takes is in milliseconds and that too in String type.

What I want and tried is: want the time in integer form and in minutes:

@Value("${cron_frequency_interval_for_logs}")
int cronFrequencyIntervalForLogs;

This syntax I copied from internet but didn't work for me.

@Scheduled(fixedDelay = "#{new Integer('${cron_frequency_interval_for_logs}')}")

I want the time to be in minutes as first priority but by referring to the below article I got to know the format would be 5*60*1000 for 5 minutes and in such a case if I can just store 5 in the variable and can do the rest of the calculation as that is going to be repetitive.

Is there any way of doing this?
Referred this

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

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

发布评论

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

评论(1

初心未许 2025-01-23 19:43:25

假设您有一个名为 run-Frequency.mines 的属性(不是变量),其中包含一个整数,表示您希望执行某个方法的频率。有三个注释属性会派上用场:

  • initialDelayString - 第一次执行之前等待时间的值。
  • fixedDelayString - 执行之间等待时间的值。
  • timeUnit - 评估上述两个值(分钟、秒等)时使用的单位。
@Scheduled(
    initialDelayString = "${run-frequency.minutes}",
    fixedDelayString = "${run-frequency.minutes}",
    timeUnit = TimeUnit.MINUTES
)

附带说明一下,这里是该注释的 JavaDoc:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

Let's say you have a property (not a variable) called run-frequency.minutes which contains an integer of how frequently you would like a method to execute. There are three annotation properties that will come in handy:

  • initialDelayString - Value of how long to wait before the first execution.
  • fixedDelayString - Value of how long to wait between executions.
  • timeUnit - The unit to use when evaluating the above two values (MINUTES, SECONDS, etc).
@Scheduled(
    initialDelayString = "${run-frequency.minutes}",
    fixedDelayString = "${run-frequency.minutes}",
    timeUnit = TimeUnit.MINUTES
)

On a side note, here is the JavaDoc for that annotation: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文