The woes of multithreading and writing a scheduler
Hi Gurus
I had no idea where to put this particular question but i fugured this should be the best place( moderators if this is not so please transfer this to the appropriate forum)
My problem is that of writing an age old task of a schedualar and just can't seem to find the best way of doing it. i know of 2 ways, 1 is to write it as a simeple java based program and the other is to run it as a servlet as a thred which wakes up every 30 secons or configured forma properties file or something.I have been advised its not so easy. In both cases i don't have the option of restartng automaticaly if it crashes. Further my schedular has to hit the database in every 30 seconds or so. Plus writing a schedular seems to me a question of reinventing the wheel, so if there is any suggestion you guys can give me on how to go about it, i would really appreciate it.
thanks
Tarun
[907 byte] By [
sodhi007a] at [2007-10-2 2:42:13]

If you are using Windows, I suggest you use a service wrapper. (Use google to find one you like) Then you can set the restart behaviour for that service.
If you are using unix, write a script which calls your service in a loop. You may want to delay between restarts, rotate logs, even log restarts in case a service is restart too often.
Thanks for the reply, well service wrappers are a good idea if i know which service i want to wrap :), i am still not sure if i want to use a servlet or a simple java App as a standalone or something else entirely, can you shed some light on that and i have to query a long table in the database to decide if any action needs to be done( this app needs to trigger a mail sending application based on data fed into the table)
Thanks
Tarun
Hi,I think that is not a good idea use a database table, you make a lot of select to table.You could see by example Jdring in JARS, I think that is a implementation of Cron Tab, by is 100 % pure java.ThanksPS: It is possible that Jdring is not the exactly name.
Take a look at Quartz: http://www.opensymphony.com/quartz
thanks a lot for the response, although the Quartz is a very nice thing to use it does not quite fit into my requirements, or maybe i don't know enough. Here is what i am looking at exactly
My aplication is sort of an brainless email dispatcher, you give it a message it will send it off after some procesing. Tthe application is being used by numerous other applications and all of them want the ability to schedule when the message needs to go out, they just post the message in the database, i pick it up and send it and so the scheduling information is also posted in the database. So i don't need a scheduler "as per say" what i need is a continously running job which will "ping" the database every 30 seconds or so and read all the messages "scheduled" to be sent out, start a new thread to send it and continue on it's merry way.
I am not really sure how to use quarts to do this, cus i don't want this job of mine to die and don't want ot write a cron job or a windows service cus the system has to be OS independent (unless there is an easy way to isolate the implementation like a wrapper or something) So any ideas please.
Thanks
Tarun
Maybe I'm missing something here, but why can't you just use a background Thread that doesn't die until it's instructed to? If exceptions are handled well it shouldn't die prematurely.
that was my thought exactly when i first came to the problem and i think it's the most viable option but there is doubt which is why i wanted to see if there is anything out there to solve my purpose.
My API has 3 parts, the calling part which has the scheduler, a queue in which messages from various "schedulers" arrive and a sending part which does some processing. The background thread is still running in the JVM of the application which called my API , if it crashes so does my "scheduler" and even though the rest of the API is running on another machine isolated by a Queue the mails will not get sent on time.
Hi,You could use Timer , see EJB 2.0 spec.Thanks
back to my original problem, for a moment if i assume the JVM problem does't exist (i trule hope it does't and i am being paranoid :D ) , so that still leaves me with 2 options as you said a timer class or some other hocus pocus like that or a startup servlet, both of which i am assuming ultimately will use some for of thread.sleep() kind of thing, if these 2 are my options then which one is better ( i need some meat for the design doc he he ) anyone ......?