The Java timer and the TimerTask class can help us implement the timer function, using the servlet listener to perform some work after the Web service starts. The combination of the two can be implemented in the Web application timer function. 1. Plan Class Code Scheduler
public void Sendscheduler (String datestr) {
Final timer timer = new timer ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Parseposition pos = new parseposition (0);
Date currenttime = Formatter.parse (Datestr, POS);
Timer.schedule (New TimerTask () {
@Override public
void Run () {
//can be placed in the method to be executed) after the scheduled start of operation 1 seconds per interval
System.out.println ("The plan begins.") "+ i++);
Timer.cancel (); Schedule Cancellation
}
, currenttime, 1000);
2. Writing listening
public class Schedulerlistener implements servletcontextlistener{public
void contextdestroyed ( Servletcontextevent arg0) {
} public
void contextinitialized (Servletcontextevent arg0) {
SYSTEM.OUT.PRINTLN ("Service Opening time:" +new date ());
Scheduler Scheduler = new Scheduler ();
Scheduler.sendscheduler ("2012-12-26 17:00:00");
}
3. Listening Configuration
<listener>
<listener-class>org.chh.scheduler.listener.SchedulerListener</listener-class>
</listener>