日期:2014-05-19 浏览次数:20888 次
一.java.util.timer类(一般不用它)
首先在web.xml中配置listener监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 可以用来启动schelduledExcutorsService 或 timer -->
<listener>
<listener-class>com.timer.TimerListener</listener-class>
</listener>
</web-app>
?
然后在com.timer包下创建TimerListener.java
package com.timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.scheldule.SchelduCall;
import java.util.*;
public class TimerListener implements ServletContextListener {
private Timer timer;
public TimerListener(){
System.out.println("执行TimerListener构造函数");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("destory listener");
timer.cancel();
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("init listener");
timer = new Timer(); //可以使用timer
timer.schedule(new TopicTask(), 0, 20*1000); //服务器启动时就执行一次
new SchelduCall().scheldul(); //可以调用scheldul()方法
}
}
?任务类:topicTask.java
package com.timer;
import java.util.*;
public class TopicTask extends TimerTask{
public void run() {
System.out.println("task:"+new Date());
}
}
?然后启动服务器就能循环执行任务了。
?
?
?
二.使用类java.util.concurrent.ScheduledExecutorService;
首先建立SchedulCall.java
package com.scheldule;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class SchelduCall {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public static void main(String[] args) throws Exception{
new SchelduCall().scheldul();
}
public void scheldul(){
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 5, 7, TimeUnit.SECONDS); //过五秒后开始执行,然后循环7秒执行一次
scheduler.schedule(new Runnable() { //第27秒后执行
public void run() {
beeperHandle.cancel(true);
System.out.println("stop");
}
}, 27, TimeUnit.SECONDS);
//scheduler.shutdown();
}
}
?
然后再TimerListener中调用即可
?
?
?
三.自然要属任务调度框架quartz
1.首先在web.xml文件中加入 如下内容(根据自己情况设定)
在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用,提供了一个QuartzInitializerServlet和一个QuartzInitializerListener,用于在加载web应用时,对quartz进行初始化。
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 使用监听器初始化quartz -->
<!-- <context-param>
<param-name>config-file</param-name>
<param-value>/quartz.properties</param-value>
</context-param>
<context-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
<