aspose file tools
The moose likes Servlets and the fly likes Setting timer for a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Setting timer for a servlet" Watch "Setting timer for a servlet" New topic
Author

Setting timer for a servlet

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Guys,
I hv a servlet which shoots an email with some data taken from database.I want this servlet to shoot the email thrice in a day at specified time interval.In short i need to invoke a servlet at specified time intervals.How do i do it?
I hv a timer class with an interface TimerListener.But how to invoke a servlet as an object?
Can anyone help me.
public class Timer extends Thread
{
TimerListener m_timerListener;
int m_cycle;
Object m_object;
public Timer(TimerListener timerListener, int cycle)
{
m_timerListener = timerListener;
m_cycle = cycle;
m_object = null;

}
public Timer(TimerListener timerListener, int cycle,
Object object)
{
m_timerListener = timerListener;
m_cycle = cycle;
m_object = object;
}

public void run()
{
while (true)
{
try {sleep(m_cycle * 1000); }
catch (InterruptedException ex) {ex.printStackTrace();}
// Fire a TimerEvent

if (m_timerListener != null)
{
m_timerListener.TimerEvent(m_object);
}
} //while ends
}//run ends
}//class ends
public interface TimerListener
{

void TimerEvent(Object object);

}

Thanx in advance !!!1
Bye,
Manish
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
The Java Ranch has a naming policy, described here and "manish_dalal" is not a valid name; Please use a space to separate your two names.
Thanks.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
I'm puzzled why you feel you need to use a servlet to send an email. I would strongly recommend refactoring your code into one or more "email" classes which are used by the servlet, and also used by the timer. This is a classic example of why servlets should contain no application-specific code, but merely be a thin layer which interfaces your "business" classes to HTTP and HTML.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Frank,
Well,I need to finally include this servlet in an XMLSERVER for my porject.I did not get you what do u mean by generating email classes in servlet & timer.May be I am a begineer level programmer to understand your advice.
Manish
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Dear Manish :
What Mr.Frank means is use servlets only for controllin the flow and not for any specific business logic . Put the business logic in a serperate class and use it in the servlet.
Try to use MVC model
Thank
Siva
Even i am beginner , if anything is wrong let me know


Rgds<p>--Siva Jagadeesan<br /><a href="http://java2simple.blog-city.com" target="_blank" rel="nofollow">http://java2simple.blog-city.com</a><br />Sun Certified Java2 Programmer<br />Sun Certified Web Component Developer<br />BEA Certified Weblogic Server 7 Enterprise Developer
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
Some time ago I posted an explanation of this design approach in response to someone else's question, which you might find helpful. Have a look here.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Setting timer for a servlet
 
Similar Threads
Time Out
Applet timer
Java not for NASA?
You know how macs make one button pulse?
Java Screensaver! ... almost ...