Re: starting threads from servlets



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/ + info@xxxxxxxxxxxxxxxxxx


.



Relevant Pages

  • Re: Servlet design question
    ... don't put huge objects in the session, ... Concerning fields in the servlet class itself, no, you shouldn't do it, ... > for that request only (e.g. request-related stuff scooped out of ... > wrapper also contains a reference to my SessionState object, ...
    (comp.lang.java.programmer)
  • Re: pls explain these methods...
    ... >> parameter instead of coding it into the servlet. ... and pass the information as a request attribute. ... with the request to some other piece of the request processing chain. ... a JSP page to generate the actual request: ...
    (comp.lang.java.help)
  • Servlet design question
    ... for that request only (e.g. request-related stuff scooped out of ... I ended up passing request and response parameters ... containing all the session state info and stored a SessionState object ... Even so, I end up with some really huge servlet classes, with maybe ...
    (comp.lang.java.programmer)
  • Re: Request for suggestions re: architecture of IntraNet/Socket app[s]
    ... Your Ajax routine could be used to call an ISAPI dll from the browser, ... Here is the Simple Java Servlet ... @param request servlet request ... @param response servlet response ...
    (borland.public.delphi.non-technical)
  • Re: SendMailServlet example & server setup ?
    ... Thanks for theory but this is supposed to be my first servlet so I need ... Session session = Session.getInstance; ... analyzes the HTTP headers included with the request, ... replication requests, optimal would be the same amount of threads as nodes ...
    (comp.lang.java.help)