Duke Martin wrote:
> hi,
>
> I recently posted a message about timed servlet actions. I agree that
> starting a thread every five minutes is the way to go, but I am not clear on
> a few things. I only want to launch the thread when my servlet is called
> and I want it to stop when the servlet is stopped. I want the thread to
> call my "Receive" method(every 5 min), which retrieves messages from the
> mail server, and display on the current screen a new mail notification. The
> notification can be as simple as printing "new mail". The servlet where the
> thread will exist uses the doPost method because info about the user is
> submitted to the servlet. How do I implement the procedure described above?
>
> Thanks
> Duke
>
This depends on what you mean by "I want it to stop when the servlet is
stopped".
(1) If you want the thread to stop when the servlet engine is shut down, that's
pretty simple -- stop it in the destroy() method.
(2) If you want to stop it before you return from the current request, that is
also pretty simple -- just stop it before your return from the doPost() method.
(3) If you want the thread to run in between requests from the user, though,
you've got a more complicated design issue. While the user is looking at the
response from the doPost(), the servlet isn't running -- so there's no direct
place for any output from your thread to go. One approach might be to use
sessions, and pass a reference to the session to your thread startup code. The
thread could then set user variables in the session to define the state (such
as new mail waiting). With suitable use of HttpSessionBindingListener, you
could arrange to stop the thread when the session was invalidated.
But you've got a more fundamental issue here -- how do you plan to send the
"new mail" notification to the user? Normally, HTTP requests are always
initiated by the client, not the server. You need to think about having a page
that periodically refreshes itself (and therefore picks up the current status),
or an applet that periodically requests the status and updates its display.
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html