Java Web中Timer的使用方法
来源:互联网 发布:股票自动挂单软件 编辑:程序博客网 时间:2024/06/05 20:58
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;
}
}
- Java Web中Timer的使用方法
- java中定时器Timer/TimerTask的使用方法
- MFC中Timer的使用方法
- Java Web项目中定时器Timer的使用
- Java Web的Timer详解
- java 安卓 timer的使用方法
- java计时器Timer的几种使用方法
- Android中Timer使用方法
- Android中Timer使用方法
- Android中Timer使用方法
- Timer 的使用方法
- java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- Java中Timer的使用
- Java中Timer的用法
- java中timer的使用
- winform的app.config
- 深入理解C++中的mutable关键字
- oracle游标
- Liferay:JSR Portlet Security(权限)
- SQL replace
- Java Web中Timer的使用方法
- linux文件结构
- memset
- ASIHTTPRequest 异步下载
- subset(2)
- 下载报表数据保存到服务器
- c++中的私有构造函数
- 手机上网连接方式(1)
- 史上最全的iPhone 5传言大汇总