コードの書き方によって異なります。私の見解では、http/ftp呼び出しの処理を担当するAPIClientを作成し、呼び出し元関数に応答を返します。 つまり、すべてのhttp/ftpコールがこのAPIClientファイルを通過する必要があります。タイムアウトをミリ秒単位で格納するクラスパラメータ/定数を持つことは良いことであり、http/ftp呼び出しを行っている間にタイムアウトとしてこの定数を使用します。
public abstract class WebServiceAPIClient {
protected String authUser;
protected String authPassword;
// Set the connection timeout whatever you want...
final Integer connectionTimeOut = 60000;
public WebServiceAPIClient(String user, String pwd, int errorCode) {
this.authUser = user;
this.authPassword = pwd;
this.errorIdentiferCode = errorCode;
}
public String requestRESTGet(String url, List<Integer> successCodes, boolean addBasicAuth) throws CloudWebServiceInvocationException {
Long start = System.currentTimeMillis();
HttpGet get = new HttpGet(url);
DefaultHttpClient client = getHttpClient();
try {
if (addBasicAuth)
addHeaders(get);
HttpResponse response = client.execute(get);
return responseAsString(response);
} catch (ClientProtocolException e) {
internalServerError(e);
} catch (IOException e) {
internalServerError(e);
} finally {
client.getConnectionManager().shutdown();
}
return "";
}
protected DefaultHttpClient getHttpClient() {
DefaultHttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
if (this.connectionTimeOut != null) {
HttpConnectionParams.setConnectionTimeout(params, this.connectionTimeOut);
HttpConnectionParams.setSoTimeout(params, 3 * this.connectionTimeOut);
}
return client;
}
}
うーん、今あなたがグローバルにアクセスの場所に '静的final'変数を定義し、完全に呼び出しを避けるために、それを使用することができうーん、私は知らないが、私はそのようなものが存在するとは思いません – niceman