Forums Register Login

call by system

+Pie Number of slices to send: Send
Hi,
How can I write a simple servlet which will be started by system based on the time I set inside the servlet?
Thanks
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 686 times.
Similar Threads
calling servlet from another server.
getting system info using servlet
Where does servlet reside?
problem: code in init() not executing, while code in init(ServletConfig conf) works:
To combine or not..That is the question
set time
call by system
User Authentication and login System in JAVA
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:37:41.