How to handle HTTP connection timeout in Java

Source: Internet
Author: User

"Declare a Boolean public variable to indicate whether the current httpconnection receives a server response.

In your connection line, the variable is set to false before the connection;

Start another monitoring thread, get the connection object of the httpconnection, and cyclically monitor this Boolean public variable. If your
If the Boolean public variable is false, the system automatically sets httpconnection = NULL. In this way, the connection line will throw an exception and exit.
. "----- Zhengyun

I wrote the Timer class for implementation. (I learned how to write on a foreign website)

Class timer extends thread {
/** How many milliseconds each time */
Protected int m_rate = 100;

/** Timeout duration in milliseconds */
Private int m_length;

/** Running time */
Private int m_elapsed;

/**
* Constructor
*
* @ 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;
}
/**
* Timing again
*
*/

Public synchronized void reset (){
M_elapsed = 0;
System. Out. println ("reset timer ");
}
/**
* If this parameter is set to time-out, you can call this method directly when the server returns an error, which is treated as time-out.
*
*/
Public synchronized void setTimeout ()
{
M_elapsed = m_length + 1;
}

/**
*/
Public void run (){
// Loop

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 is a global variable.
// Trigger a timeout
Timeout ();
Break;
}
}

}
}

/**
* Processing upon timeout
*
*/
Public void timeout (){
Httpconnection = NULL;
System. Out. println ("conn time>" + time_out + "Ms ");
}
}

In the same way that the HTTP connection thread calls New timer (20*1000). Start ();

Timer. Reset () needs to be called for timing again ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.