starting threads from servlets

Discussion in 'Java' started by Fernando, Apr 29, 2005.

  1. Fernando

    Fernando Guest

    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.
    Fernando, Apr 29, 2005
    #1
    1. Advertising

  2. Fernando

    Lee Ryman Guest

    -----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-----
    Lee Ryman, Apr 29, 2005
    #2
    1. Advertising

  3. 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?
    Thomas Schodt, Apr 29, 2005
    #3
  4. Fernando

    Ross Bamford Guest

    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/ + in
    Ross Bamford, Apr 29, 2005
    #4
  5. Fernando

    Lee Ryman Guest

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

    iEYEARECAAYFAkJyP6MACgkQhbcFpQga0LDIAgCfaYyJvoqbGREeVED5Ri5RXOs9
    KPQAnj3JfWfsgccW3Zf3hBVoH4eHQqCf
    =V2LZ
    -----END PGP SIGNATURE-----
    Lee Ryman, Apr 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig
    Replies:
    12
    Views:
    749
  2. Will McGugan

    overhead of starting threads

    Will McGugan, May 23, 2005, in forum: Python
    Replies:
    2
    Views:
    350
    Will McGugan
    May 23, 2005
  3. raghu
    Replies:
    8
    Views:
    342
    Randy Howard
    Jan 20, 2007
  4. Sam Roberts
    Replies:
    1
    Views:
    214
    Yukihiro Matsumoto
    Feb 11, 2005
  5. K. R.

    Starting Threads

    K. R., Oct 25, 2007, in forum: Ruby
    Replies:
    4
    Views:
    115
    ara.t.howard
    Oct 25, 2007
Loading...

Share This Page