my dog learned polymorphism*
The moose likes Servlets and the fly likes Using ServletContextListener 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 » Java » Servlets
Bookmark "Using ServletContextListener" Watch "Using ServletContextListener" New topic
Author

Using ServletContextListener

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using ServletContextListener
 
Similar Threads
Is it possible to Read from a local Database without Server
Regarding Job Scheduler in JSF
Listeners in a cluster
Is it guaranteed that web server will initialize all web application on the server startup time
How to deal with configuration data in a (web) application?