File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes ServletContextListener & Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextListener & Threads" Watch "ServletContextListener & Threads" New topic
Author

ServletContextListener & Threads

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
** To mods.. wasnt sure which forum to base this post.. Servlets/Threads etc please move it at your discretion.. **

Hi All,

Having a brain strain. Heres what i'm trying to achieve, I wish to have a Class which listens to a socket and launches new socket threads for each connection... (this is ok) But I want the listening class (SocketListener) to also implement the ServletContextListener so it launches and starts when my web app starts.. I'm having trouble getting this to launch in its own thread so it doesnt halt the progress of my J2EE server... as is whats happening at the moment..

Can anyone help me get my head around this servlet/threading problem.

Thankyou (Below is code for SocketListener class)


[ July 10, 2006: Message edited by: Bear Bibeault ]

Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
It looks like the issue is the contextInitialized is invoking the initListener(),
// TODO Need initlistener to run in its own thread...
initListener();
}

which then loops indefinately, as required, but does not return. the contextInitialized() is expecting to return as to let the rest of the application initialize.

I guess what you could do here is make your class also implement the runnable interface. and then have a member Thread myThread instance variable. the listener would then do something like a
to have the listening thread start and you would move the initListener call to the run() method so it gets called from the threaded invocationl.

Then have the contextDestroyed() clean up the running thread, if it is running. do something like:

This would allow the listener to later stop the thread when the app is reloaded; In a past project I found this to be an issue over time, in that zombie threads would sometimes stay around from previous app reloads.

Though this approach is sort of confusing in that we make a thread out of the context listener, that contains a nested thread instance of itself.


As a second idea, I have evolved make my own threaded service concept that manages the start and stop of the thread. Having the threaded part as a separate facility from initial context listeners enables me to restart the thread at any time within the lifespan of the application, or even be used outside of servlet containers, if that was ever a need for you as
well.

Here is my threaded service interface:


And a stub implementation class


So you could make a child class of the ThreadedServiceImpl, and only need to specify the service() method and then put the contents of the while(true) in your thread loop into here. Then your listener could cleanly create the instance of your threaded service child class, (optionally stuffing it into a servlet context attribute), and then the contextInitialized() and contextDestroyed() would invoke the startThread() and stopThread() respectively. Your child class can override these as well if there is special initializition or cleanup, such as the network socket in your case.


Error: Keyboard not attached. Press F1 to continue.
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Thanks Travis, that helps me 100%.




Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContextListener & Threads