User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 422,551 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,654 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting

Automatic mail using jsp and javamail

Join Date: Jun 2006
Posts: 5
Reputation: lifayan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lifayan lifayan is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #6  
Jun 7th, 2006
that is simple ,if you use a j2ee container such as tomcat ,
here is a example
this is a ServletContextListener you should config it in web.xml so it will run auto when tomcat starts.

this is the web.xml config
<listener>
<listener-class>com.hecheng.util.ServletListenerTimmer</listener-class>
</listener>

you may need to change some field to fill your condition

public class ServletListenerTimmer implements ServletContextListener {
private java.util.Timer timer = null;
private Log mylogger = LogFactory.getLog(this.getClass());
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
mylogger.info("timmer is started");
timer.schedule(new SendReport(event.getServletContext()), 0, 60 * 60 * 1000);
mylogger.info("add sendmail task");
}
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
mylogger.info("timmer destoryed");
}
}

public class SendReport extends TimerTask {
private static final int C_SCHEDULE_HOUR = 22;
private static boolean isRunning = false;
private ServletContext context = null;
public SendReport(ServletContext context) {
this.context = context;
}
public void run() {
Calendar cal = Calendar.getInstance();
if (!isRunning) {
System.out.println("cal.get(Calendar.HOUR_OF_DAY) = " + cal.get(Calendar.HOUR_OF_DAY));
if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
isRunning = true;
doReportTask();// you can write your sendmail task there
isRunning = false;
}
} else {
context.log("the prevenient task is still running!");
}
}
]
Reply With Quote  
All times are GMT -4. The time now is 12:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC