Timer timer=new Timer(); TimerTask task=new TimerTask(){ public void run(){ //here is what you want to do, println helloworld for example System.out.println("HelloWorld"); } }; //period between two execute long period=5*1000; timer.schedule(task,0,period); 5秒打印一次helloworld
import java.net.URL; import java.io.*; import java.net.*;public class TimerListener implements javax.servlet.ServletContextListener { private java.util.Timer timer; long iVoxCount = 0; long iMsgCount = 0; private java.io.InputStream is = null; public TimerListener() { System.out.println("初始化成功"); timer = new java.util.Timer(true); } public void contextDestroyed(javax.servlet.ServletContextEvent event) { System.out.println("系统关闭"); timer.cancel(); } public void contextInitialized(javax.servlet.ServletContextEvent event) { System.out.println("开始检测"); //System.out.println( event.getServletContext().getRealPath( "/" ) ); timer.schedule( new java.util.TimerTask() { private HttpURLConnection conn; public void run() { // System.out.println("111"); try { URL url = new URL("servletURL"); try { conn = (HttpURLConnection) url.openConnection(); is = conn.getInputStream(); is.close(); conn.disconnect(); } catch (IOException ex1) { } } catch (MalformedURLException ex) { } } } , 1000, 时间间隔); } }不知道这样能满足你的要求不,这是启动tomcat以后可以自动定期运行的一段程序 时间间隔是毫秒
TimerTask task=new TimerTask(){
public void run(){
//here is what you want to do, println helloworld for example
System.out.println("HelloWorld");
}
};
//period between two execute
long period=5*1000;
timer.schedule(task,0,period);
5秒打印一次helloworld
import java.io.*;
import java.net.*;public class TimerListener
implements javax.servlet.ServletContextListener {
private java.util.Timer timer;
long iVoxCount = 0;
long iMsgCount = 0;
private java.io.InputStream is = null; public TimerListener() {
System.out.println("初始化成功");
timer = new java.util.Timer(true);
}
public void contextDestroyed(javax.servlet.ServletContextEvent event) {
System.out.println("系统关闭");
timer.cancel();
} public void contextInitialized(javax.servlet.ServletContextEvent event) {
System.out.println("开始检测");
//System.out.println( event.getServletContext().getRealPath( "/" ) );
timer.schedule( new java.util.TimerTask() {
private HttpURLConnection conn;
public void run() {
// System.out.println("111");
try { URL url = new URL("servletURL");
try {
conn = (HttpURLConnection) url.openConnection();
is = conn.getInputStream();
is.close();
conn.disconnect();
}
catch (IOException ex1) {
}
}
catch (MalformedURLException ex) {
}
}
}
, 1000, 时间间隔);
}
}不知道这样能满足你的要求不,这是启动tomcat以后可以自动定期运行的一段程序
时间间隔是毫秒
<listener>
<listener-class>TimerListener</listener-class>
</listener>这样就可以启动了
xx_ch(好客小虫) --能把完整的给我吗,上面的程序好象不全--------, 1000, 时间间隔);