wood burning stoves 2.0*
The moose likes Servlets and the fly likes call by system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "call by system" Watch "call by system" New topic
Author

call by system

Dennis Liu
Greenhorn

Joined: Dec 03, 2003
Posts: 9
Hi,
How can I write a simple servlet which will be started by system based on the time I set inside the servlet?
Thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I can't help thinking that there may be some confusion behind your question about what a servlet is, and the respoinsibilities of a servlet container.
Can you help us out by explaining a little more about what you want your servlet to do when it is "started"? (or maybe what it should not do, before it is started) Please bear in mind that the whole point of a servlet is that it just sits waiting for incoming requests, processes them and sends back results.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Dennis Liu
Greenhorn

Joined: Dec 03, 2003
Posts: 9
Thanks for yours response.
I installed Tomcat at my computer.I want to run a overnight process which has nothing to do with
users.For example,everyday at 10pm I want the Tomcat(or system) to start this process(or servlet).
and do something on database side.I think Tomcat always runs,so there should be a way to do this.
Thanks again
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You can use Tomcat as a container for this if you really want, but what you are running within it is not really a servlet.
Create a servlet with no "getXXX" methods, just an "init"; mark it as "load-on-startup" in the deployment descriptor, and don't assign it to a URL path. That will get you a class which is loaded, instantiated and initialised whenever Tomcat starts but will never be called by a web request. In the "init" method of this class, start a new Thread and immediately return from init leaving your new thread running.
In your new thread you are free to write any code you like - go to sleep for a bit, wake up, check the time, if it's time to do something, do it and then go back to sleep.
But in all honesty, there's not a lot of reason to do this inside Tomcat. You might as well write a simpler class with a "main" that also starts a thread (but waits for the the thread to complete before returning) doing the same stuff. Then just run it from the command line and leave it running.
Or you could write an even simpler program that just does your DB access then finishes. You can run run this program when needed under the constrol of "cron" or whatever task scheduler is provided by your Operating System.
Has any of that helped?
Dennis Liu
Greenhorn

Joined: Dec 03, 2003
Posts: 9
Hi,
Thanks for your message.I still prefer using Tomcat,but I have some questions,
If the newly created thread's life period depends on the dummy servlet,
how can I keep the dummy servlet always running in order to keep the new thread running?
If the new thread is independent,how can I stop the thread created by dummy
servlet?If I stop Tomcat and restart it, how many threads are running the
overnight process?I hope just one.
Is it good to put my business logic inside the init()?
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
1. the new Thread does not "depend" on the servlet that created it. The JVM regards it as just another Thread with its own priority, daemon status, stack, etc. Servlets don't "run" - they sit and wait for the next request.
2. if you need to be able to stop the Thread there are several approaches depending on how you want to exert control - from a browser, from the server's console, what?
3. if you stop Tomcat, the JVM and all running Threads will die automatically. You may need to provide for a graceful exit.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call by system
 
Similar Threads
calling servlet from another server.
To combine or not..That is the question
getting system info using servlet
problem: code in init() not executing, while code in init(ServletConfig conf) works:
Where does servlet reside?