This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See 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.
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
posted 14 years ago
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?
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
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