java中处理http连接超时
2007-12-24 09:38
381 查看
http是无状态的连接,不像socket可以有timeout的异常,因此在判断超时上一直比较困扰.
这段时间做j2me的一个小游戏.用到了http连接,学习了论坛上说的一种方法.
"声明一个boolean公共变量,表明当前httpconnection是否得到服务器回应。
你的连接线程中在连接之前置这个变量为false;
另起一个监视线程,拿到那个HttpConnection的连接对象,并循环监视这个boolean公共变量。如果指定时间内(20秒后)你的boolean公共变量还是false,那么就主动置httpconnection=null。这样,那边连接线程就会抛出异常退出来。" -----zhengyun
写了Timer类来实现.(学习国外一个网站上的写法)
class Timer extends Thread {
/** 每个多少毫秒检测一次 */
protected int m_rate = 100;
/** 超时时间长度毫秒计算 */
private int m_length;
/** 已经运行的时间 */
private int m_elapsed;
/**
* 构造函数
*
* @param length
* Length of time before timeout occurs
*/
public Timer(int length) {
// Assign to member variable
m_length = length;
// Set time elapsed
m_elapsed = 0;
}
/**
* 重新计时
*
*/
public synchronized void reset() {
m_elapsed = 0;
System.out.println("reset timer");
}
/**
* 故意设置为超时,可以在服务器有返回,但是错误返回的时候直接调用这个,当成超时处理
*
*/
public synchronized void setTimeOut()
{
m_elapsed = m_length+1;
}
/**
*/
public void run() {
// 循环
System.out.println("timer running");
for (;;) {
// Put the timer to sleep
try {
Thread.sleep(m_rate);
} catch (InterruptedException ioe) {
continue;
}
synchronized (this) {
// Increment time remaining
m_elapsed += m_rate;
// Check to see if the time has been exceeded
if (m_elapsed > m_length && !isConnActive) { //isConnActive 为全局变量
// Trigger a timeout
timeout();
break;
}
}
}
}
/**
* 超时时候的处理
*
*/
public void timeout() {
httpConnection = null;
System.out.println("conn time > " + TIME_OUT + " ms");
}
}
在http连接线程调用的同是调用 new Timer(20*1000).start();
需要重新计时时候调用timer.reset();
不知道大家有没有更好的方法
这段时间做j2me的一个小游戏.用到了http连接,学习了论坛上说的一种方法.
"声明一个boolean公共变量,表明当前httpconnection是否得到服务器回应。
你的连接线程中在连接之前置这个变量为false;
另起一个监视线程,拿到那个HttpConnection的连接对象,并循环监视这个boolean公共变量。如果指定时间内(20秒后)你的boolean公共变量还是false,那么就主动置httpconnection=null。这样,那边连接线程就会抛出异常退出来。" -----zhengyun
写了Timer类来实现.(学习国外一个网站上的写法)
class Timer extends Thread {
/** 每个多少毫秒检测一次 */
protected int m_rate = 100;
/** 超时时间长度毫秒计算 */
private int m_length;
/** 已经运行的时间 */
private int m_elapsed;
/**
* 构造函数
*
* @param length
* Length of time before timeout occurs
*/
public Timer(int length) {
// Assign to member variable
m_length = length;
// Set time elapsed
m_elapsed = 0;
}
/**
* 重新计时
*
*/
public synchronized void reset() {
m_elapsed = 0;
System.out.println("reset timer");
}
/**
* 故意设置为超时,可以在服务器有返回,但是错误返回的时候直接调用这个,当成超时处理
*
*/
public synchronized void setTimeOut()
{
m_elapsed = m_length+1;
}
/**
*/
public void run() {
// 循环
System.out.println("timer running");
for (;;) {
// Put the timer to sleep
try {
Thread.sleep(m_rate);
} catch (InterruptedException ioe) {
continue;
}
synchronized (this) {
// Increment time remaining
m_elapsed += m_rate;
// Check to see if the time has been exceeded
if (m_elapsed > m_length && !isConnActive) { //isConnActive 为全局变量
// Trigger a timeout
timeout();
break;
}
}
}
}
/**
* 超时时候的处理
*
*/
public void timeout() {
httpConnection = null;
System.out.println("conn time > " + TIME_OUT + " ms");
}
}
在http连接线程调用的同是调用 new Timer(20*1000).start();
需要重新计时时候调用timer.reset();
不知道大家有没有更好的方法
相关文章推荐
- java中处理http连接超时的方法
- java中处理http连接超时的方法
- java中处理http连接超时
- java中处理http连接超时的方法
- java中处理http连接超时的方法
- 【java中处理http连接超时的方法】
- 常用java web容器http长连接超时设置
- httpclient.get|post处理(连接、响应超时)
- socket 连接超时处理 java
- httpclient.get|post处理(连接、响应超时)
- Java非阻塞NIO网络编程连接超时的处理
- Java HttpURLConnection超时和IO异常处理
- HttpClient(二)HttpClient使用Ip代理与处理连接超时
- Java http处理get请求,参数中带特殊字符处理方式
- MySQL数据库连接超时(wait_timeout)问题的处理
- Java网络连接之HttpURLConnection与HttpClient 区别及联系(转)
- java线程超时处理
- HttpClient连接请求超时设置
- java实现http协议连接和下载
- 转载】MySQL数据库连接超时(wait_timeout)问题的处理