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

Using ServletContextListener

 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a co-worker that would like to run a process when his web application is brought online (assuming periodic app server reboots). Every time his web app is brought up, he wants to spawn some other process.

Would it be possible to use a ServletContextListener (and implement the contextInitialized method) to accomplish this? If I understand this correctly, this method is invoked when the application context is initialized and that happens only once, when the server is brought up and your application is loaded. Is it possible that this happens more than once?

Also, are there any dangers to doing this? The alternative would be to schedule a task through some other scheduling mechanism but, if either the scheduling mechanism isn't working or there are delays to the app server being brought up, there could be issues. It would be nice if the web app, itself, could kick off the process.

So, anyone see any problems with that approach?

Thanks,
Corey

P.S.

I have no idea what he's actually doing with this "extra process." He only came to me and asked me how I might do such a thing and, as I'm in class all this week, I haven't had a further chance to discuss it with him. It seems an odd thing to do, but, if that's what he wants to do, I'll assume he has his reasons.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a contextListener is the place to do this. The contextInitialized method get's called whenever the app is reloaded, whether via a server reboot or just a restart of the application (from the manager etc..).

If he's managing his own threads he will either want to use daemon threads or be sure to clean them up in the contextDestroyed method of the listener. Otherwise, you may have trouble restarting the app and/or container.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic