I have and requirement where I need to execute search function once in 24 hours the function will get the search parameters from DB and execute the search.
One option of doing it is to register this as kron task,but due to some reason my client is not comfortable with using kron job and wants this to be an part of application so I thought of following way kindly give me your valuable suggestions on the same.
I will write one servlet with init method, in this Init method I will create one thread which will execute search logic and sleep for 24 hours.
As init has to be called cause there wont be any request going to this servlet I will set the Load-On-StartUp parameter to load the servlet when server is started.
kindly let me know if there are any drawbacks in this method.
Hi Praful. the idea of putting initialization code inside init method is a good one. However instead of spawning a permanent thread that executes every 24 hrs, you can use a open source scheduler API (http://www.quartzscheduler.org/quartz/). That way you can avoid implementing your own thread.
Praful Thakare
Ranch Hand
Joined: Feb 10, 2001
Posts: 613
posted
0
Hi Arnab,
Thanx a bunch for the information,looking forward to try Quartz.
well can you tell me abt the advantage of using quartz over simple thread ?
Hi Praful. the idea of putting initialization code inside init method is a good one. However instead of spawning a permanent thread that executes every 24 hrs, you can use a open source scheduler API (http://www.quartzscheduler.org/quartz/). That way you can avoid implementing your own thread.
I agree with the idea of avoiding writing threads in a servlet app. Manageing your own threads in a J2EE app is a headache that is best avoided when possible.
For containers that support Servlet Spec 2.3 or higher (Tomcat 4.1 does) it's much better to put initialization code like this in a context listener. Remember, servlet containers are free to instanciate as many instances of a servlet as they deem necessary and at whatever time they deem necessary.
emember, servlet containers are free to instanciate as many instances of a servlet as they deem necessary and at whatever time they deem necessary.
Where did you find that zinger!?! Is this some sort of programmer Urban Myth? The servlet API says very very specifically that unless you have a distributed environment (or are dealing with a SingleThreadModel implementation) the servlet container must use only one instance per declaration. See section SRV2.2 of the servlet 2.4API Bill
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
1
posted
0
It occur to me that I should point out that even though a servlet container is normally allowed to have only one instance of a servlet per declaration, they are allowed to destroy an instance (calling destroy() )if it is not servicing a request and re-create a new one (calling init() ) for the next request. If you are using init to start a separate long-running process, you would need to check that it has not already been created by a previous instance. Bill
It occur to me that I should point out that even though a servlet container is normally allowed to have only one instance of a servlet per declaration, they are allowed to destroy an instance (calling destroy() )if it is not servicing a request and re-create a new one (calling init() ) for the next request. If you are using init to start a separate long-running process, you would need to check that it has not already been created by a previous instance. Bill
Bill, Thank you for the correction. What I should have said was "as often" not "as many". Either way, distributed or not, the context listener is a better tool for managing long running processes. You've just listed an excellent reason why.
-Ben [ December 24, 2004: Message edited by: Ben Souther ]
Praful Thakare
Ranch Hand
Joined: Feb 10, 2001
Posts: 613
posted
0
Hi All,
Thanx a bunch for your replies, this thing is much more complicated then I thought, and sorry for replying soooooooo late.
was away from Earth
Mark, sorry for the spell mistake that really changed the meaning
well now about the business, quote: -------------------------------------------------------------------------------- It occur to me that I should point out that even though a servlet container is normally allowed to have only one instance of a servlet per declaration, they are allowed to destroy an instance (calling destroy() )if it is not servicing a request and re-create a new one (calling init() ) for the next request. If you are using init to start a separate long-running process, you would need to check that it has not already been created by a previous instance. Bill ------------------------------------------------------------------------
well then I guess this approach wont help my requirement, cause if once the servlet is unloaded from memory then the next time will be loaded again only after server is restarted.