• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

a regular activity and Thread

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to perform a certain activity on regular intervals like 3 months or 6 months. This activity should get fired automaticaly and should create a activity log with any manual intervention.
I am not quite sure how to implement it using thread.
I am planning to a low priority thread which sleeps most of the time and then check last_action time from database if it > 3 months then start a activity.
Is theer any other efficient way of implementing it
?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are scheduling packages to do this for you like Quartz and Flux. I can't say I'd recommend Flux (have not used the latest versions) and I have no hands on with Quartz.

If I had to do it all on my own, I'd probably do something at JVM startup (don't know if this is a server, desktop app or what) to get the next scheduled task from the database and create a Timer to go off at the scheduled time. When the task is completed, update the database to delete it or show it done, get the next scheduled event and so on.

What happens if you schedule a new tasks before the "next" in memory? Say the next task I got from the database was for 4:00 and at noon I schedule one for 3:00. The update process might cancel the existing timer and start a new one.

This gets trickier yet in a cluster. You need to assure only one server in a cluster does each job, recover from one shutting down before doing its next task, and much more. In clusters Flux uses polling intead of timers. Every so often a server gets the next item from database and marks it in progress. This is within a transaction with read for update so only one server can get an item. When the task is done it deletes the task from database.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use java timer

http://java.sun.com/j2se/1.3/docs/api/java/util/Timer.html
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i would have a Unix daemon for this
 
kirti marode
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Yes, we can use Timer/Timertask and Thread or we can use Unix daemon/cron utility to do it.
My application is a web app, this regular activity is a small activity which is a part of this webapp and has to be fired automaticaly every 3 months a kind of report generation and data integrity check for the webapp.
As its part of a bigger application I can't go for daemon/cron job, This activity will be failrly time consuming so I am bit confused to use Timertask.
I am however trying to use Quartz along with the my struts framework by going through an article on onjava, but till now no luck.
 
I don't always make ads but when I do they're tiny
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic