tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Wright <e...@spectacle.ca>
Subject Re: HOW CAN I USE THREADS IN TOMCAT
Date Sat, 01 Nov 2003 16:03:12 GMT
A servlet is just a Java class. You can do anything you can do with the 
java language, including start threads. The following starts a thread 
that runs some task every 10 minutes. The thread is started in the 
servlet init method. I choose to set the thread to daemon mode, meaning 
that when the main thread of execution shuts down the mailer thread will 
automatically be killed. Otherwise you need to be sure to keep track of 
it and be sure to signal it to shutdown in your Servlet.destroy method.

public class MyServlet extends HttpServlet
{
    private static class MailerThread extends Thread
    {
       public void run ()
       {
          while (true)
          {
              // do something
             synchronized (this)
             {
                wait (10*60*1000);
             }
          }
       }
    }

    // the servlet init method
    public void init ()
    {
       MailerThread thread = new MailerThread ();
       thread.setDaemon (true);
       thread.start ();
    }

    // ... doGet, etc. ...
}

-Erik

Eric C wrote:

>Use cron under unix and/or a java app that would use Javamail classes.
>Of course you can do it inside a servlet.
>
>----- Original Message -----
>From: "Prince" <thelastprince@hotmail.com>
>To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
>Sent: Saturday, November 01, 2003 8:51 AM
>Subject: HOW CAN I USE THREADS IN TOMCAT
>
>
>  
>
>>Hi,
>> I need to send a mail automatically at 10 minutes interval. how can i do
>>this? how can i activate threads in tomcat?
>>regards
>>Prince
>>
>>----- Original Message -----
>>From: "Bill Barker" <wbarker@wilshire.com>
>>To: <tomcat-user@jakarta.apache.org>
>>Sent: Saturday, November 01, 2003 1:00 PM
>>Subject: Re: Opinions
>>
>>
>>    
>>
>>>The recent port of the 3.3 variable-substitution to Tomcat 5 may very
>>>      
>>>
>well
>  
>
>>>solve your problems here :).  The ports are supposed to move to
>>>commons-digester, so should be available in 4.1.30 as well.
>>>
>>>"Francois JEANMOUGIN" <Francois.JEANMOUGIN@123multimedia.com> wrote in
>>>message
>>>
>>>      
>>>
>news:153E2BD74548D1408FFF45D4D27E2A5818EB58@mail.tls.123multimedia.com...
>  
>
>>>      
>>>
>>>>>Hu! It's in early developments or is it suitable for production ?
>>>>>          
>>>>>
>>>>It's ready for production.  People are already using it in production
>>>>with tomcat 5.  Of course, I'm biased ;)
>>>>        
>>>>
>>>Well, there is that beta flag in front of tomcat5 that tell me that your
>>>opinion is more than biased :). Of course, I saw a mail telling that the
>>>beta flag is related to specification instability (not code), anyway...
>>>
>>>      
>>>
>>>>It's a Developer, rather than Sysadmin, option.  Chances are you won't
>>>>need to worry about it as you'll use commons-daemon with tomcat, which
>>>>already does the required jsvc invocation for you.
>>>>        
>>>>
>>>Well, that does not help that much. I hope that there will be a
>>>configuration scheme that'll let easily define user/group/port and so on
>>>      
>>>
>>for
>>    
>>
>>>the connectors. Just waiting for a sysadmin point of view :).
>>>
>>>Thanks,
>>>
>>>Fran├žois.
>>>
>>>
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>>
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
>
>  
>


-- 
http://www.spectacle.ca/
The New Online Source for Live Music in Montreal
.::514.286.1699::.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message