有个聊天表业主让每5s刷新一次,所以参考别人写了一个能够实现的小案例,记录一下
必须是servlet,还需要配置web.xml
package listener;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class QuartzListener implements ServletContextListener {
private java.util.Timer timer = null;
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("this is last destroyeed");
}
public void contextInitialized(ServletContextEvent event) {
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
System.out.println("======listener test is beginning=========");
}
},0,5000);
}
}
<listener>
<listener-class>
listener.QuartzListener
</listener-class>
</listener>
一般是在拦截器之后,servlet之前
项目启动之后每5秒输出一段话