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

Architecture question: Running apps on Tomcat Startup

Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
I have an application that I would like to run when Tomcat starts the application. On first blush, I could do it by creating a servlet and in the web.xml file configure it with the <load-on-startup> parameter.

The problem is that it is not a servlet. The code is a sleeper-loop that wakes up every hour, sends a message to another web server, then goes to sleep again. No servlets used. So why do it in Tomcat? It is dependent on code that is being managed by Tomcat.

It seems a bit kludgey to create a servlet that isn't a servlet just to take advantage of the <load-on-startup> feature. Is there a better way to do this?

Thanks much!


There's no place like 127.0.0.1
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Load-on-startup servlets are soooo 2002! Check out context listeners.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
Thanks Bear!

That seems so much better. My spider sense told me what i was doing was kludgey. I have three books, one of them specifically for Tomcat [Pro Apache Tomcat 5/5.5 From Mathew Moodie (Apress)] and there is very little about ContextListeners in print. So I Googled it - and the most helpful thing I found was http://www.onjava.com/onjava/2003/01/08/examples/ContextListener.html

Are these methods (especially contextInitialized) called asynchronously? In other words, the code will be in a near-never ending loop, waking up every few minutes, checking a directory, then going back to sleep again. In other words, do I need to worry about starting a new thread in the Listener I'm about to create so I don't hang everything up?

By the way, when i was digging through the books to look this up, I serendipitously found something else that I needed. w00t!
[ October 15, 2007: Message edited by: Charles McGuire ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Not something I've ever doen, but yeah, you can't just put it in an infinite loop.

Search through this and the Servlets forum and I'm sure you'll find something on running such a process in a separate thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Architecture question: Running apps on Tomcat Startup