• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using ServletContextListener

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic