Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Java event on date/time based on database row

Tags:

java

listener

I'm trying to make a Listener (or something like that?) that will start a specific event when a date field from a database row is the same as the current time. Of course I can trigger every second to check if the date/time is the same as the current, but I think that is quite expansive. There should be a better alternative..

What I trying to do is the following: I have several (for example football) matches scheduled in my database. At the specific time when the match should start, I will start a event in my Java app. This could be 1 or more matches at that time.

like image 231
Danny Gloudemans Avatar asked Jan 02 '14 12:01

Danny Gloudemans


1 Answers

I understand that you are trying to schedule execution of future events in java app not in database.

You should consider using ScheduledExecutorService method schedule to delay execution of task to specific point in time.

The only problem you have to solve is how you synchronize task in database with this in schedule.

EDIT: If you keep map with taskID->ScheduledFuture object returned you can easily call cancel on the object to remove task. But you have to add some kind of last-modification column to detect new and updated tasks and query database to check if there are not any new tasks.

like image 124
Krystian Lieber Avatar answered Oct 13 '22 09:10

Krystian Lieber