Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Setting timer for a servlet

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic