Shedule tasks question
I have a though question :)
Let's say you get some data whi开发者_JAVA百科ch is due at a certain point int the future. You get A saying my task is due in 1h and other which says his task is due in 1.5h. The information is collected on your server. In which programming language or even how would solve that? So task a will be executed in 1h, task B in 1.5h. I read something about java scheduler but I'm not yet sure if this is the right way.
What are ur ideas?
Cheers
cron is installed on most unix web servers - you can use it to assign tasks to execute later.
If you are looking for programmatic access then take a look at Quartz - a java scheduler. They also have a good tutorial resource.
If this is a long running process, then you can use a ScheduledExecutorService in java to achieve this. Clearly, though if your process exits, then the task will be lost.
cron or quartz would also work as more persistent schedulers. each has their own wrinkles though.
If, for some reason, you decide to implement such a system yourself, here's the idea.
- Have a list where you keep your tasks, ordered by due time.
- Create a daemon process that sleeps most of the time, and wakes e.g. every minute, or even irregularly using
sleep()
. When the daemon wake up, it takes tasks from the queue; if task's due time has come, it runs this task in a separate process (or thread), and removes it from the queue. Having started all due tasks, it goes back to sleep. - Devise an interface to add new tasks to the queue.
精彩评论