You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jignesh Patel <jp...@bangracing.com> on 2004/04/20 17:01:29 UTC

Timer implementation with servlet

Hi All,
I am trying to implement timer with servlet but it works only at the time of 
loading only while I schedule it to work after every 1 minute. 
What is the reason for not working later on?

My servlet code is as follows:
public class TimerServlet extends GenericServlet implements SingleThreadModel 
{
	public void init(ServletConfig config) throws ServletException {
		Timer timer = new Timer();
		System.out.println("timer started");
		timer.schedule(new SessionTrack(),1000);		 
	}
	public void service(ServletRequest arg0, ServletResponse arg1) throws 
ServletException, IOException {
	
	}
	public String getServletInfo() {
		 return null;
	}

}
And code for TimerTask is as follows:

public class SessionTrack extends TimerTask {
     public void run(){
     	System.out.println("sending email");
     	Mailer mailer = new Mailer();
		mailer.sendMail("profile","jpatel@bangracing.com");
     }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Timer implementation with servlet

Posted by Aaron Smuts <aa...@wisc.edu>.
For one, it looks like you are using the wrong schedule method if you
want it to repeat.  Also, the delay is in millis.  So, you have it set
for 1 second.

If you want it to repeat, you have to use the 3 arg method.  

timer.schedule(new SessionTrack(), 0, 60000);

http://java.sun.com/j2se/1.3/docs/api/java/util/Timer.html#schedule(java
.util.TimerTask,%20long)


Aaron Smuts

> -----Original Message-----
> From: Jignesh Patel [mailto:jpatel@bangracing.com]
> Sent: Tuesday, April 20, 2004 10:01 AM
> To: user@struts.apache.org
> Subject: Timer implementation with servlet
> 
> Hi All,
> I am trying to implement timer with servlet but it works only at the
time
> of
> loading only while I schedule it to work after every 1 minute.
> What is the reason for not working later on?
> 
> My servlet code is as follows:
> public class TimerServlet extends GenericServlet implements
> SingleThreadModel
> {
> 	public void init(ServletConfig config) throws ServletException {
> 		Timer timer = new Timer();
> 		System.out.println("timer started");
> 		timer.schedule(new SessionTrack(),1000);
> 	}
> 	public void service(ServletRequest arg0, ServletResponse arg1)
> throws
> ServletException, IOException {
> 
> 	}
> 	public String getServletInfo() {
> 		 return null;
> 	}
> 
> }
> And code for TimerTask is as follows:
> 
> public class SessionTrack extends TimerTask {
>      public void run(){
>      	System.out.println("sending email");
>      	Mailer mailer = new Mailer();
> 		mailer.sendMail("profile","jpatel@bangracing.com");
>      }
> }
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org