timer用法

web.xml添加内容 建立Servlet
<servlet>
<servlet-name>StatServlet</servlet-name>
<servlet-class>cn.StatServlet</servlet-class>
<init-param>
<param-name>startTask</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>intervalTime</param-name >
<param-value>1</param-value >
</init-param>
<load-on-startup >4</load-on-startup >
</servlet>
<servlet-mapping>
<servlet-name>StatServlet</servlet-name>
<url-pattern>/StatServlet</url-pattern>
</servlet-mapping>


StatServlet类
package cn;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StatServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private Timer timer1 = null;

private Task task1;


public void destroy() {

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

public void init() throws ServletException {

ServletContext context = getServletContext();

String startTask = getInitParameter("startTask");

if(startTask.equals("true")){
timer1 = new Timer(true);
task1 = new Task(context);
//每分钟访问一次
timer1.schedule((TimerTask) task1, 1000, 1000*60);
}
}
}


Task类
package cn;

import java.io.IOException;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

import cn.yicha.pay.log.StatWhiteNameLogger;

public class Task extends TimerTask{
public static final String REQUESTURL = "http://www.baidu.com";
public static boolean isOk = false;

private ServletContext context;

private static boolean isRunning = true;

public Task(ServletContext context){
this.context = context;
}


public void run() {
if(isRunning){
HttpClient client = null;
GetMethod getMethod = null;
int statusCode=888;
try {
client = new HttpClient();
getMethod = new GetMethod(REQUESTURL);
getMethod.setFollowRedirects(true);
//设置连接超时时间(单位毫秒)
client.setConnectionTimeout(10000);
//设置读数据超时时间(单位毫秒)
client.setTimeout(8000);
//执行getMethod
statusCode = client.executeMethod(getMethod);

} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
if(statusCode==200||statusCode==302){
isOk = true;
}else{
isOk = false;
}
//记日志
StatLogger.log(statusCode,isOk);
System.out.println("状态码----> "+statusCode+" isOk-------> "+isOk);
//释放连接
getMethod.releaseConnection();
}
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值