定时执行任务的三种方法:
1)java.util.Timer.
2)ServletContextListener.
3)org.springframework.scheduling.timer.ScheduledTimerTask
1)java.util.Timer
这个方法应该是最常用的,不过这个方法需要手工启动你的任务:
Timer timer=new Timer();
timer.schedule(new ListByDayTimerTask(),10000,86400000);
这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
2)ServletContextListener
这个方法在web容器环境比较方便,这样,在web server启动后就可以自动运行该任务,不需要手工操作。
将ListByDayListener implements ServletContextListener接口,在contextInitialized方法中加入启动Timer的代码,在contextDestroyed方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:
<-listener>
<-listener-class>com.qq.customer.ListByDayListener</listener-class>
<-/listener>
3)org.springframework.scheduling.timer.ScheduledTimerTask如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer.xml中加入下面的内容就可以了:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="MyTimeTask1"/>
</list>
</property>
</bean>
<bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/>
<bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="MyTimeTask"/>
</property>
<property name="delay">
<value>10000</value>
</property>
<property name="period">
<value>86400000</value>
</property>
</bean>
</beans>
以上内容转载自:http://dev.csdn.net/author/xtpdcsse/ec8e8080a5b04fa79e7d4828bc807d3f.html
下面是我的实现。
1)利用java.util.Timer. 代码如下
<1>StartThread.java
<2>StatPlan.java
<3>StatWeekPlan .java
2)ServletContextListener.实现
<1> SysStatListener .java
<2> StatTask .java
<3>在web.xml中添加下面的内容(注:下面的内容放到<filter-mapping>的后面)
<listener>
<listener-class>com.jview.auto.stat.SysStatListener </listener-class>
</listener>
1)java.util.Timer.
2)ServletContextListener.
3)org.springframework.scheduling.timer.ScheduledTimerTask
1)java.util.Timer
这个方法应该是最常用的,不过这个方法需要手工启动你的任务:
Timer timer=new Timer();
timer.schedule(new ListByDayTimerTask(),10000,86400000);
这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
2)ServletContextListener
这个方法在web容器环境比较方便,这样,在web server启动后就可以自动运行该任务,不需要手工操作。
将ListByDayListener implements ServletContextListener接口,在contextInitialized方法中加入启动Timer的代码,在contextDestroyed方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:
<-listener>
<-listener-class>com.qq.customer.ListByDayListener</listener-class>
<-/listener>
3)org.springframework.scheduling.timer.ScheduledTimerTask如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer.xml中加入下面的内容就可以了:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="MyTimeTask1"/>
</list>
</property>
</bean>
<bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/>
<bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="MyTimeTask"/>
</property>
<property name="delay">
<value>10000</value>
</property>
<property name="period">
<value>86400000</value>
</property>
</bean>
</beans>
以上内容转载自:http://dev.csdn.net/author/xtpdcsse/ec8e8080a5b04fa79e7d4828bc807d3f.html
下面是我的实现。
1)利用java.util.Timer. 代码如下
<1>StartThread.java
package com.jview.main;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
public class StartThread extends Thread {
private static Logger logger = Logger.getLogger("StartThread");
public static final int EXECUTE_CYC = 86400000; //24*60*60*1000毫秒
int startH = 9;
int startM = 52;
private Timer _timer ;
private Date _statDate;
private Date _nowDate;
public StartThread(){
_nowDate = new Date();
_timer = new Timer();
_statDate = new Date(_nowDate.getYear(),_nowDate.getMonth(),_nowDate.getDate(),startH,startM);
}
public void StartUp(){
_timer.schedule(
new TimerTask() {
public void run()
{
logger.info("开始统计...");
try {
Class.forName("com.jview.stat.StatPlan").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},_statDate,EXECUTE_CYC);
}
public static void main(String[] args) {
StartThread _statUp = new StartThread();
_statUp.StartUp();
}
}
<2>StatPlan.java
package com.jview.stat;
import java.util.Calendar;
import org.apache.log4j.Logger;
public class StatPlan {
private static Logger logger = Logger.getLogger("StatPlan");
private int i = 0;
private StatPlanService _sps ;
public StatPlan(){
_sps = new StatWeekPlan();
statPlan();
}
public void statPlan(){
Calendar _c = Calendar.getInstance();
logger.info("stat plan ... 执行"+ i +"次,时间:"+_c.getTime());
_sps.StatPlan();
i++;
for(int i = 0; i<9999;i++){
if(i==0 || i== 9998){
logger.info(""+i);
}
}
}
}
<3>StatWeekPlan .java
import org.apache.log4j.Logger;
public class StatWeekPlan extends StatPlanService {
private static Logger logger = Logger.getLogger("StatWeekPlan");
public void StatPlan(){
logger.info("this is statWeekPlan");
}
}
<4>StatPlanService .java
public class StatPlanService {
public void StatPlan(){
}
}
2)ServletContextListener.实现
<1> SysStatListener .java
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
public class SysStatListener implements ServletContextListener{
private static Logger logger = Logger.getLogger("ListByDayListener");
private StatTask _sTask;
public void contextDestroyed(ServletContextEvent scevent) {
String status = "停止系统统计线程";
scevent.getServletContext().log(status);
logger.info(status);
_sTask.shutDown();
}
public void contextInitialized(ServletContextEvent scevent) {
String status = "启动系统统计线程";
scevent.getServletContext().log(status);
logger.info(status);
_sTask = new StatTask();
_sTask.startUp();
}
}
<2> StatTask .java
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
public class StatTask extends Thread {
private static Logger logger = Logger.getLogger("StartThread");
public static final int EXECUTE_CYC = 86400000;
int startH = 9;
int startM = 52;
private Timer _timer ;
private Date _statDate;
private Date _nowDate;
public StatTask(){
_nowDate = new Date();
_timer = new Timer();
_statDate = new Date(_nowDate.getYear(),_nowDate.getMonth(),_nowDate.getDate(),startH,startM);
}
public void startUp(){
_timer.schedule(
new TimerTask() {
public void run()
{
logger.info("开始统计...");
try {
Class.forName("com.jview.stat.StatPlan").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},_statDate,EXECUTE_CYC);
}
public void shutDown(){
_timer.cancel();
}
<3>在web.xml中添加下面的内容(注:下面的内容放到<filter-mapping>的后面)
<listener>
<listener-class>com.jview.auto.stat.SysStatListener </listener-class>
</listener>