This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 60804
    
  65

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: 60804
    
  65

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.
 
jQuery in Action, 2nd edition
 
subject: Architecture question: Running apps on Tomcat Startup
 
Similar Threads
TomCat refuse to execute init(), but WebLogic could!
how to make servlet able to run as soon as computer started
Web applications with various configurations files
How can I verify the ocurrence of <load-on-startup> Servlet? (TOMCAT)
Loading Data at app startup