Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > starting threads from servlets

Reply
Thread Tools

starting threads from servlets

 
 
Fernando
Guest
Posts: n/a
 
      04-29-2005
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.
 
Reply With Quote
 
 
 
 
Lee Ryman
Guest
Posts: n/a
 
      04-29-2005
-----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

iEYEARECAAYFAkJyLwAACgkQhbcFpQga0LA9jwCeInhUvRid3u dm2EVjGnhYwDeq
IVgAoJA14wwBYaElTy36Sl6dX0u6Emwx
=cs4u
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      04-29-2005
Fernando wrote:
> public synchronized void start() {
> while(true) {
> try {
> verificar();//checks something
> sleep(60000);
> } catch (InterruptedException e) {
> //?
> }
> }
> }


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?
 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      04-29-2005
On Fri, 2005-04-29 at 05:37 -0700, Fernando wrote:
> 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

--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + http://www.velocityreviews.com/forums/(E-Mail Removed)in


 
Reply With Quote
 
Lee Ryman
Guest
Posts: n/a
 
      04-29-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lee Ryman wrote:
> 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

iEYEARECAAYFAkJyP6MACgkQhbcFpQga0LDIAgCfaYyJvoqbGR EeVED5Ri5RXOs9
KPQAnj3JfWfsgccW3Zf3hBVoH4eHQqCf
=V2LZ
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Java servlets: Hi All! I want to display xml file in browser using servlets datta.saru Software 0 05-15-2006 03:30 PM
Threads not running in starting order (?) Timo Nentwig Java 12 12-14-2005 12:37 PM



Advertisments