Re: starting threads from servlets



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It is technically possible to create and start another thread from a
servlet, but IMHO it would be a very bad idea. Once the servlet returned
there would be little means to safely track the existence and state of
the created threads.

One cleaner and nicer means of having the app-server manage a thread for
the life of the web-app is to use a implementation of a
javax.servlet.ServletContextListener
which is initialised on deployment of your web-app using a couple of
lines in your web.xml file such as...

<listener>
<listener-class>ThreadManagerContextListener</listener-class>
</listener>

This listener could create some sort of singleton which controls your
database update thread, and servlets could query the singleton for
information. I haven't really spent alot of time thinking about applying
such a scheme to management of a separate thread, but I have used such a
system involving a context listener to manage a custom-made database
connection pool, and it worked quite well.

Its a basic idea. Any further information can be determined from the API
documents, Google, Java tutorials, etc.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iEYEARECAAYFAkJyLwAACgkQhbcFpQga0LA9jwCeInhUvRid3udm2EVjGnhYwDeq
IVgAoJA14wwBYaElTy36Sl6dX0u6Emwx
=cs4u
-----END PGP SIGNATURE-----
.



Relevant Pages

  • double configuration error of spring context when deploying web app
    ... ok so I have a problem with deployment of my war app in tomcat server ... INFO: The listener "pl.pointit.web.listener.ContextCycleListener" is ... already configured for this context. ...
    (comp.lang.java.programmer)
  • Re: how to use servlet to receive message (not soap message)
    ... > main servlet class (the listener) doesn't even trigger the onMessage. ... What I now think you are trying to do - have a servlet be a JMS ... I use the RMI connector. ...
    (comp.lang.java.programmer)
  • Re: Thread and Listener
    ... Bob Rivers wrote: ... > The problem is that this class is actually a listener. ... > it hangs the other procedures, including tomcat initilization. ... threads inside a servlet container to do things that have nothing to do ...
    (comp.lang.java.programmer)
  • Re: Servlets: Kontext-Initialisierung abbrechen
    ... ich habe in web.xml mittels <listener> eine Klasse ContextListener angegeben, ... In deren contextInitialized-Methode wird unter anderem die Existenz einiger für die Arbeit des Servlets nötiger Dateien geprüft. ... System.exitist dafür ja wohl keine gute Idee :-) Wie macht man das Servlet also am besten un-verfügbar? ...
    (de.comp.lang.java)
  • Re: singleton class/member
    ... I am facing a problem with (Static member) hashmap in a singleton ... I have a class (Say B, a servlet) that is loaded into memory ...
    (comp.lang.java.programmer)