I am working on one application where we need to process some business logic before some specific time of the event's [it's application's entity] start time [field of an event entity which contains start time of an event]
My application picks up these events and creates timer task and assigns that timer task [user defined] to timer so java can execute at the given time [x mins/hours before the event's start time].
Now, I am facing issue that if server/spring boot service gets down so timer tasks and timers will be destroyed and I won't be able to process the business logic.
My Solution Approach (didn't work well):
To overcome to that issue , I have tried below logic,
- I am adding records in new table of eligible events and
- marking them "to be processed" before I create timer task
- once they are processed, we are marking them as "done".
Working with Single server instance: Now if server/service gets down, I will have records in new table with "to be processed" which I can pick up and process on server start up event. This will work for single server instance but we have multi server instances in our infrastructure.
Problem with multiple server instances: So main issue is how to handle this scenario in multiple server instances? How to make sure that only one server instance will pick up the record and will execute as there are chances that other server can also pick up the record and process it so there are chances of duplicate timer tasks of same event in multiple server instances and can create an issue which I need to avoid.