File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Architecture question: Running apps on Tomcat Startup

 
Charles McGuire
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Load-on-startup servlets are soooo 2002! Check out context listeners.
 
Charles McGuire
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic