Mohammad Mohammad - 22 days ago 8
Java Question

how to do a job based on time that user has entered

I have some entities in database. Every entity have a field called time.
the time field has been entered from user side. I want to call a method on that time.
For example imagine a system that will call to your phone in specific time that you have set before.
I know A solution is to use a tread that checks the time field in database every second, but What is the best solution?

Answer

Have a look at Spring-Scheduler here. There you can define your own Scheduler instance. This has a method called schedule() which can be called with a specific task and a (Cron)Trigger. On the time the user saves the entity with the given time you can create a new (Cron)Trigger to schedule the task.

scheduler.schedule(task, new CronTrigger("0 15 9-17 * * MON-FRI"));