java - 以设定的重复间隔预定消息
问题描述
我在 Spring-Boot/JPA 域中有以下任务:
id: 1, task: GET:http://example.com/file.zip, schedule: every-minute
id: 2, task: DELETE:/some/path, schedule: every-5-minutes
id: 2, task: TOUCH:/some/path, schedule: every-1-hour
我有一个名为 spring 服务taskHandlerService
,它能够处理上述任务,但我一直想知道如何安排它们,以便消息taskHandlerService
以指定的时间间隔到达。
任务列表经常更改,因此这些作业不会运行很长时间。
我在这里可以想到的一种天真的方法是创建多个 Quartz 作业,为列表中的每个任务创建一个,然后该作业将负责taskHandlerService
以消息作为参数调用方法。
有没有更好的方法来解决这个问题?
更新:经过一番搜索,延迟队列似乎是一种更好的方法。但不确定如何将 Qaurtz Cron Schedule 表达式转换为该队列。
解决方案
您可以将这些注释用于您的方法:
@Scheduled(cron = "0 */1 * ? * *") // for the first method
@Scheduled(cron = "0 */5 * ? * *") //Second method
@Scheduled(cron = "0 0 * ? * *") // third method
有关更多信息,您可以在此处查看示例
推荐阅读
- php - 了解 wordpress 的权限
- laravel - 迁移和模型之间有什么关系?
- saxon - Saxon 是否提供了一种方法来维护地图中键的插入顺序?
- java - 如何在 cognito 中使用 adminGetUserRequest 获取用户属性
- pandas - iloc 的功能和简单的 [ ] 系列
- python - 为什么我得到 OSError: [Errno 5] 在实时服务器中运行 streamlit 时输入/输出错误错误?代码在我的本地主机中运行正常
- python - 有没有办法确保没有项目从随机列表中重复,如果它们都已输出,则输出一些东西?
- python - 如何在 for 循环中处理 Python 字典键,同时添加新键
- excel - 程序执行缓慢,无法识别最慢的操作
- oracle - OR 在选择 Oracle