Design Scheduled tasks in a Web application;
Method One:
Listeners that use the servlet context
Steps:
A
Create a listener class implementation interface: Servletcontextlistener
Two ways to implement an interface:
Package com.bjmobile.action;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
/**
* Listener class and timer combine to complete the task of listening timed execution;
* @author New
*
*/
public class Contextlistener implements Servletcontextlistener {
Private Java.util.Timer Timer = null;
public void contextinitialized (Servletcontextevent event) {
Timer = new Java.util.Timer (true);//True indicates that it is defined as a daemon thread;
SYSTEM.OUT.PRINTLN ("Timer has been activated");
Timer Dispatch statement, where MyTask is a custom execution task that needs to be dispatched
Perform one task at initialization time;
This example performs a task every three minutes;
Timer.schedule (New MyTask (), 0, 3 * 60 * 1000);
SYSTEM.OUT.PRINTLN ("added task dispatch table");
}
public void contextdestroyed (Servletcontextevent event) {
Timer.cancel ();
System.out.println ("timer destruction");
}
}
Two
Create a task class MyTask: inheriting the TimerTask class
Write the task in the Run method:
Package com.bjmobile.action;
Import Java.util.Calendar;
Import Java.util.TimerTask;
Task classes performed on a timed basis
public class MyTask extends TimerTask {
private static final int c_schedule_hour = 11;
private static Boolean isrunning = false;
public void Run () {
Calendar cal = Calendar.getinstance ();
if (!isrunning) {
if (C_schedule_hour = = Cal.get (calendar.hour_of_day)) {
When the time is 11, the task will not be executed;
So the time interval of the scheduler Timer.schedule can not be greater than one hour, otherwise it may miss the time period;
The interval of the scheduler Timer.schedule can not be less than one hour, otherwise it may perform several tasks a day;
IsRunning = true;
System.out.println ("Start execution of specified tasks");
TODO to add a custom detailed task, here's just an example
int i = 0;
while (i++ < 5) {
SYSTEM.OUT.PRINTLN ("completed task" + i + "/" + 10);
}
End
IsRunning = false;
SYSTEM.OUT.PRINTLN ("Specify end of Task execution");
} else {
System.out.println ("Time for task execution not yet");
}
} else {
SYSTEM.OUT.PRINTLN ("Last task was not completed");
}
}
}
Three
Configure the Listener class in the Web. xml file:
<listener>
<listener-class>com.bjmobile.action.ContextListener</listener-class>
</listener>
Method Two:
Using a dedicated servlet, initialize the Task Scheduler in the Servlet init method, set the Web server to load this servlet automatically
Steps:
A
Create a Taskservlet class
Package com.bjmobile;
Import Java.util.Calendar;
Import Java.util.Timer;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
/**
* The servlet and the timer combine to complete the task of listening scheduled execution, loading the scheduler at initialization;
* @author New
*
*/
public class Taskservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
Timer timer = new timer ();
public void Init () throws Servletexception {
String sendmailhour = "14";
String sendmailminutes = "04";
String Sendmailsecond = "30";
Set the first execution time;
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.hour_of_day, Integer.parseint (Sendmailhour));
Cal.set (Calendar.minute, Integer.parseint (sendmailminutes));
Cal.set (Calendar.second, Integer.parseint (Sendmailsecond));
System.out.println ("******* 1. Current time: "+cal.gettime ()");
As the timer is initialized, if the time of day of the timer has passed, perform a task immediately,
If the time of day is not yet, then the time has come to carry out the task;
Timer Dispatch statement, where Testtask is a custom execution task that needs to be dispatched
Cal.gettime () is the first execution time;
This example executes a task 24 hours later;
Timer.schedule (New Testtask (), Cal.gettime (), 24 * 60 * 60 * 1000);
SYSTEM.OUT.PRINTLN ("Timer 2 has been started");
}
@Override
public void Destroy () {
TODO auto-generated Method Stub
Super.destroy ();
Timer.cancel (); The timer is not destroyed when the project application is closed.
SYSTEM.OUT.PRINTLN ("Timer 2 destroyed. ");
}
}
Two
Create a task class Testtask: inheriting the TimerTask class
Write the task in the Run method:
Package com.bjmobile;
Import Java.util.TimerTask;
Task classes performed on a timed basis
public class Testtask extends TimerTask {
public void Run () {
System.out.println ("Performing tasks");
}
}
Three
Configure the Servlet class in the Web. xml file:
<servlet>
<servlet-name>taskServlet</servlet-name>
<servlet-class>com.bjmobile.TaskServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
This servlet is performing a background operation and does not require a corresponding external request, so it is not necessary to define servlet-mapping for it.
<load-on-startup>1</load-on-startup> must be set, automatic container initialization, loading tasks;