Hi,
i have a problem, i need to start a thread form a servlet, so that it
keeps running in background, checking some values in DB every minute.
The problem is that i start the thread from the servlet and firefox
tab seems to be loading all the time, not allowing me to click on
other links from the jsp page, because when i do so the thread is
interrupted.
I've tried to create another class which starts the thread, and call
it from the servlet, but it's the same story.
The thread should keep executing itself with this loop inside start:
public synchronized void start() {
while(true) {
try {
verificar();//checks something
sleep(60000);
} catch (InterruptedException e) {
//?
}
}
}
I'm not sure if i'm aproaching this from the right view. if i want the
thread to be always running, is this the right thing to do? and how
should i start the thread from the servlet?
Thanks.
Lee Ryman - 29 Apr 2005 13:56 GMT
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.
Lee Ryman - 29 Apr 2005 15:07 GMT
> 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.
I just realised that my connection pool did indeed have threads (to
check the state of pooled connections) and it functioned quite well,
shutting themselves down automatically (used a week reference to itself
as a backup so it could determine when it wasn't referenced by anyone
else). So it would seem the aforementioned strategy is plausible. Its
been a few years since i delved into this stuff, bear with my fragile
mind :)
Kind regards,
Lee
Thomas Schodt - 29 Apr 2005 14:00 GMT
> public synchronized void start() {
> while(true) {
[quoted text clipped - 6 lines]
> }
> }
You do not say, but it is clear that you are extending Thread
and you misunderstood how that works.
<http://java.sun.com/j2se/1.4.2/docs/api/>
BTW. How is the thread going to stop?
Ross Bamford - 29 Apr 2005 14:23 GMT
> Hi,
> i have a problem, i need to start a thread form a servlet, so that it
[quoted text clipped - 25 lines]
>
> Thanks.
The above code shouldn't be in the start method, which is running in the
calling (i.e. servlet) thread, which is why the request never returns.
Instead (of extending thread?) the code should be in the run() method of
a runnable. You should then find that your requests return.
You would probably find that your container didn't shutdown properly,
however, or some other such problem, because the JVM would be waiting on
your thread to finish. You could get around that perhaps using a Daemon
Thread, which isn't waited on but terminated at shutdown.
Better than that would be to follow another poster's suggestion, and
have your thread controlled from a ServletContextListener, starting at
stopping it at context start/stop. Keep a reference to the thread, or
better still to a static accessor, in a context attribute.
There is a proviso to all of this - be bloody careful. The servlets
environment is already pretty complex with respect to it's threading,
and you'll need to make sure you are passing things around safely and so
on.
Cheers,
Ross

Signature
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + info@the.website.domain