• 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

ServletContextListener & Threads

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
** 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 ]
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Travis, that helps me 100%.




Dave
 
reply
    Bookmark Topic Watch Topic
  • New Topic