String url = getUrl();
try{
Connection con = getConnection(url, username, pwd);
}catch(ConnectionException e){
cleanUpUrl(url);
url = getUrl();
con = getConnection(url, usernam开发者_运维知识库e, pwd);
}
I've to do something like above. if I don't get Connection with one URL then I'll be trying with another URL. Likewise there are 10URLs which I've to try one after the other.
How will I write the method recursively?
getUrl()
has the logic to read the properties file and gives you random URL out of 10.
cleanUpUrl(url)
has something to do with the setting the expiry time of the URL, if the url is invalid, some property will be set etc etc.
EDIT: Sorry I think I missed something. Recursive because I've do make the method calls until (I get the connection) or (all the URLs are invalid and a different exception is thrown). Looping 10times might not help because the getUrl()'s random logic might pick the same URL more than once.
Does the following makes sense?
Connection con = null;
do{
String url = getUrl();
try{
Connection con = getConnection(url, username, pwd);
}catch(ConnectionException e){
cleanUpUrl(url);
continue;
}catch(Exception e){
return null;
}
}while(con !=null);
getUrl() will throw exception when all urls are invalid.
Recursively? Why?
If you want to try 10 things in a row, use a for loop.
What's the stop condition, a stack overflow?
Will it matter if you created a "while" loop?
I would put the "search for a proper url" in a method, loop though the code 10 times, and return the url you found suitable to break out of the loop. (Below the loop you could return null to indicate that none of the 10 urls was suitable.)
Something like
public String findGoodUrl() {
for (int i = 0; i < 10; i++) {
String url = getUrl();
try{
Connection con = getConnection(url, username, pwd);
return url;
} catch(ConnectionException e) {
cleanUpUrl(url);
}
}
return null;
}
精彩评论