0

I am creating a java service which will continuously run in the background and the job of the service is to create a copy of the table at a particular date. To be exact, i read data from some table and if record_date in table matches the current date, i need to create the table copy. Then the service should sleep until the next date to run. Next date to run is also determined by looking at the record in the table.

Currently, how i do this, is to create a thread which runs in while(true) loop. and when thread is finished performing the task i.e. creating a table copy, I put it to sleep using Thread.sleep() until next time it needs to run. The number of milliseconds for thread to sleep, i calculate by taking the difference between the current date (date on which the task is performed by thread) and the next run date.

Is this the right approach, is using thread.sleep() for this particular scenario the right thing? I say this because next run date for a thread could be after three months or even a year. Also please let me know if i am not very clear here.

Kara
  • 6,115
  • 16
  • 50
  • 57
wazzz
  • 319
  • 2
  • 4
  • 13
  • 1
    Maybe use wait() and notify(). – jn1kk Oct 16 '12 at 14:06
  • I think your `Thread.sleep(...)` mechanism is fine. – Gray Oct 16 '12 at 14:26
  • @jsn i thought of that too, but the problem is what notifies the waiting thread, it needs to be invoked when the current date equals record_date(comes from db). to detect that event happening, there must be something checking continuously, at least once a day. I prefer a solution where thread or a process sleeps until it needs to actually execute and not consume system resources by continuously executing. – wazzz Oct 16 '12 at 14:36
  • @wazzz I see. But just a personal opinion, if you are using sleep in threads, you are almost always doings threads wrong. – jn1kk Oct 16 '12 at 14:49
  • @jsn again i think you are right and that's precisely why i asked a question. let's see if i get a solution from some one which may not require using sleep(...) and at the same time not require to execute continuously :) – wazzz Oct 16 '12 at 14:56
  • Well, I don't understand something here :( You say like the scheduling needs to be dynamic and cannot be decided before hand, then you say that `The number of milliseconds for thread to sleep, i calculate by taking the difference between the current date (date on which the task is performed by thread) and the next run date` So you exactly know when the next run will be from your second statement. Is that right ? – prajeesh kumar Oct 17 '12 at 03:45
  • cron a job to run on everyday, then in that call the java program (no need for this to be a job or scheduler, since cron is the schedule) which will check the db table for the current_date schedule. If the current_date schedule is there then take a backup of the table else exit. I don't know whether you have any constraints for this. This way, your java program will run once daily and then exit instead of sleeping till the next day. – prajeesh kumar Oct 17 '12 at 03:55
  • that's what is dynamic about it, that it needs not to run daily, next run date could be tomorrow or once in a quarter or 6 months or a year. different product types have different dates for taking backup (copy of table) and i determine the earliest one among those dates. Having said that, I'll look into cron job solution. I haven't written any cron job before, so i don't know if i can read from db and then write logic to determine earliest date etc using cron. Anyways i'll look at tutorials. – wazzz Oct 17 '12 at 08:42

3 Answers3

2

What about dissecting both operations?

  1. Write a Java Job which when invokes checks for date in the table and create a copy.
  2. Schedule the java job to run the way you want it to run.

Since we UNIX so cron helps us a lot in doing such tasks.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • Thanks for the response. Scheduling is dynamic in my case, the dates must come from db and then earliest possible date is determined to run the job. If that would not have been the case, i could have used pgagent job scheduler. – wazzz Oct 16 '12 at 14:27
  • like SiB said, split the operations, leave the daily invoking of the task to cronjob. – prajeesh kumar Oct 17 '12 at 03:58
0

Have a look at the Lock interface. This is an abstraction for wait() and notify(), which is what you should use instead of sleep().

There is an answer here which illustrates why.

Community
  • 1
  • 1
Jivings
  • 22,834
  • 6
  • 60
  • 101
0

Check out the Java Timer API or the Quartz library

clinton
  • 612
  • 3
  • 6