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!");
}
}
]