如何在Spring Web Flux中写下Cron作业(计划任务)?
我正在在春季Web Flux应用程序中实现忘记密码功能,并在此处发送的OTP,前期为5分钟。因此,我想通过每2秒执行CRON作业来使它们无效。
尽管这是一个网络通量项目,但我想通过将Hello World作为Cron工作来测试。
我在入门班级中添加了 @enablescheduling ,我写了一个类似的测试课。
@Scheduled(cron = "*/2 * * * * *")
public Disposable invalidateOtp(){
return Mono.fromCallable(() -> {
return "Hello";
}).subscribe();
}
但是我要说的是,每2秒钟就不会执行此方法。谁能建议我为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是关于如何使用cron表达式或固定速率或固定延迟运行的计划任务的非常好的文章: @scheduled注释在春季。这应该给您所有您需要的东西。但是,在某个时候,我觉得弹簧靴中提供的所有选项都不足够令人信服,尤其是对于人类的参数可读性。您必须以毫秒或克朗表达方式指定时间,这不是很容易读取的时间。因此,我希望能够指定时间9分钟9分钟或3小时等时间,等等。因此,我编写了自己的背景任务调度程序,可以轻松与Spring Boot一起使用。我在名为Mgntutils的开源库中发布了它,该库是由我编写和维护的。这是这解释了我的想法以及如何使用该功能。 (这是Mgntutils库Javadoc的一部分)您可以将图书馆作为Maven Artifact 或在 github (随附Javadoc和source-code)。在github上,您可以在软件包中查看:
com.mgnt.lifecycle.management.backgroundrunner.example
。该软件包包含2个计划任务的完整工作示例。如果您下载源,则可以将它们运送出来,看看它们的工作原理Here is very good article about how to run Scheduled tasks with cron expression or with fixed rate or fixed delay: The @Scheduled Annotation in Spring. This should give you all you need. However, At some point I felt that all options provided in Spring boot were not convinient enough especially for human readability of the params. You have to specify time in milliseconds or with cron-expression which is not very easily readable. So, I wanted to be able specify time like "9m" for 9 minutes or "3h" for 3 hours and so forth. So, I wrote my own background task scheduler that can easily be used with Spring boot. I published it in Open source library called MgntUtils which is written and maintained by me. Here is a Javadoc page that explains my idea and how to use the feature. (This is part of MgntUtils library javadoc) You can get the library as Maven artifact here or on the github (with javadoc and source-code included). On the github you can look in the package:
com.mgnt.lifecycle.management.backgroundrunner.example
. That package contains complete working example of 2 scheduled tasks. If you download the source you can just run them out of the box and see how they work我终于找到了答案。我忘记注释我的服务课@service。否则,春天就找不到任何豆。
I finally found the answer. I forget to annotate my service class @Service. Otherwise spring will not found any bean.