File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Kron Job using Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Kron Job using Servlets" Watch "Kron Job using Servlets" New topic
Author

Kron Job using Servlets

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Hi,

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.

I will be using tomcat 4.1.X

Cheers
-Praful
[ December 24, 2004: Message edited by: Mark Spritzler ]

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Arnab Roy
Greenhorn

Joined: Dec 20, 2004
Posts: 1
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: 614
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 ?

Cheers
-Praful
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Prafal, sorry for editing your post. I just had to change the topic from showing Korn to be Kron. For some reason it was getting to me.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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: 614
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.

so I guess I should go for context listener.

Every one,Thanx a bunch again

Cheers
-Praful
[ March 14, 2005: Message edited by: Praful Thakare ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Praful,

The following thread should be interesting to you:
http://www.coderanch.com/t/360033/Servlets/java/util-class-create-thread-servlet

-Ben
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Hi Ben,
That was indeed a good help.
thanx a bunch.


Cheers
-Praful
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Kron Job using Servlets