Try to connect to the Web service from the list of multiple configuration servers

Source: Internet
Author: User
Try to connect from the list of multiple configuration servers
In fact, it is relatively simple. There are mainly two important variables, one is the Server Index and the other is the number of attempts to connect. Of course, these variables can be set by yourself.
The following is a column program I wrote. It was involved in my previous project. I simplified it and discussed it with you.

Public   Bool Conn ( Ref Mcservices. Services MS)
{
// Conncount connection attempts
// Server Index configured by serverindex
Try
{
System. Threading. thread. Sleep ( 3000 ); // Blocked for 3 seconds
MS =   New Mcservices. Services ();
Ms. url = Configurationsettings. deleettings [ " Server " + Serverindex];
If (Ms. checkservices ())
{
Conncount =   0 ;
Serverindex =   1 ;
Console. writeline (Ms. url +   " Connection successful! " );

Return   True ;
}

}
Catch (Exception E) // If the connection fails, an exception is thrown.
{
If (Conncount > = 3 )
{
Conncount = 0 ;
If (Serverindex > 3 )
{< br> console. writeline ( " connection attempts on all servers failed! " );
return false ;
}
Else
{Serverindex++;}


Return Conn ( Ref MS );

}
Else
{
Console. writeline (Ms. url +   " Connection attempt failed "   + (Conncount + 1 ) +   " Time, wait 3 seconds to try to connect! " );

Conncount ++ ;
Return Conn ( Ref MS );

}

}

Return   False ;
}

Of course, you can also add backend threads to check whether a thread is suspended, connected, or connected successfully. All of these functions can be used freely.

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.