starting threads from servlets

F

Fernando

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.
 
L

Lee Ryman

-----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-----
 
R

Ross Bamford

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.

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
 
L

Lee Ryman

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

Lee said:
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iEYEARECAAYFAkJyP6MACgkQhbcFpQga0LDIAgCfaYyJvoqbGREeVED5Ri5RXOs9
KPQAnj3JfWfsgccW3Zf3hBVoH4eHQqCf
=V2LZ
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top