Java Web中Timer的使用方法

来源:互联网 发布:股票自动挂单软件 编辑:程序博客网 时间:2024/06/05 20:58
package com.task;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TaskManager implements ServletContextListener {


//public static final long INTERVAL = ;
public static final long NO_DELAY = 0;
private long interval = 1000*60*5;
private Timer timer;

public void contextInitialized(ServletContextEvent event) {
timer = new Timer("NavTech Request Task!",true);
timer.schedule(new TrafficInfoQueryTask(),NO_DELAY,interval);
}

public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}

--------------------------------------------------------------------------------------------------------------------

package com.task;


import java.util.TimerTask;


public class TrafficInfoQueryTask extends TimerTask {

//private static Log log = LogFactory.getLog(TrafficInfoQueryTask.class);
private static boolean isRunning = false;

public void run() {
if (!isRunning) {
isRunning = true;
//start task
Communication.start();
//task is ended
isRunning = false;
} else {
System.out.println("Task Running!");
}
}

----------------------------------------------------------------------------------------------------------------------------

通过HttpClient获取服务器的响应数据。

package com.task;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.xml.sax.SAXException;


import com.trafficxmlparser.TrafficXmlParser;


import java.io.*;
import javax.xml.parsers.ParserConfigurationException;




public class Communication {
  
  //private static String url = "http://172.23.176.11:8080/TrafficInfoServer/TrafficInfoServer";


  public static void start() {
// Create an instance of HttpClient.
//HttpClient client = new HttpClient();


    // Create a method instance.
    //PostMethod method = new PostMethod(url);
    DBOperation dbOperation = null;
    try {
    
     // Execute the method.
  /*int statusCode = client.executeMethod(method);

   if (statusCode != HttpStatus.SC_OK) {
     System.err.println("Method failed: " + method.getStatusLine());
   }
   */
    
   // parse the XML file 
   TrafficXmlParser parser = new TrafficXmlParser();
   long startTime = System.currentTimeMillis();
//parser.parse(method.getResponseBodyAsString());
   parser.parse(new FileInputStream("D:\\Project\\Document\\RealtimeFlowB14.xml"));
   long endtTime = System.currentTimeMillis();
   
   System.out.println("时间为:"+(double)(endtTime-startTime)/1000);
    }
    catch (ParserConfigurationException e1) 
    {
// TODO Auto-generated catch block
e1.printStackTrace();

    catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
    catch (IOException e)
    {
      e.printStackTrace();
    } 
    finally 
    {
     // Release the connection.
     // method.releaseConnection();
    }  
  }
  private static boolean authenticate()
  {
 boolean passAuthen = false;
 return passAuthen;
  }
}

原创粉丝点击