Spring Boot Schedule Task at Specific Date With Data

40 Views Asked by At

We have an event resource that has a ZoneDateTime that represents its start timestamp as well as a unique identifier (Primary Key). Users can check into an event. The goal is to schedule a task that gets executed an hour before the events start timestamp in order to notify the users that are checked into this event with our Push Notification service.

The solution needs to be scalable as well as have the ability for the task to reference the corresponding eventId so we can determine the users that are checked in. The task also needs to be scheduled programmatically as every event has a different start time.

I am looking for a good resource or tool to use. Unless I am missing something, it seems like Spring Boot's native scheduler is not capable of performing this task. I also have looked into Quartz but I am unsure how to handle the requirement of storing the specific event id with the scheduled task.

As this is my first time scheduling something like this, I would appreciate any resources or thoughts you can give me.

1

There are 1 best solutions below

0
SimpleJack On

You need a more abstract approach to your problem.

You don't need to schedule every event, you only need to schedule the next event. If the scheduler triggers & executes you have to do a little more work:

  • Execute your task with whatever he is supposed to do
  • After this is done, request your DB what the next event is, and start from the beginning

With this approach you don't have a scaling problem, because you only schedule exactly 1 event.


NOTE: This is a very simple approach. You may adapt this to your specific problem:

  • What happens if an event gets re-scheduled?
    • Nothing if it does not become the next event, but if it does, you need to cancel the current scheduling and start this new one.
  • What happens if an event gets canceled?
    • Nothing again, if it was not the next event, but if it was then again cancel the scheduler and take the next one
  • What happens if there are many events close to each other (i am talking about nano/milliseconds), because the execution simply needs time, therefore you may miss some events.
    • This is more difficult. You may no longer work with single events, but with groups of events. You would still have only 1 scheduler, but he would execute multiple events. You had to adapt all problems from above to this new behavior.