首页 > 解决方案 > 以设定的重复间隔预定消息

问题描述

我在 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 表达式转换为该队列。

标签: javaspringspring-bootquartz-scheduler

解决方案


您可以将这些注释用于您的方法:

   @Scheduled(cron = "0 */1 * ? * *") // for the first method
   @Scheduled(cron = "0 */5 * ? * *") //Second method
   @Scheduled(cron = "0 0 * ? * *")   // third method

有关更多信息,您可以在此处查看示例


推荐阅读