KYLIN-2607 Add http timeout for RestClient
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/320e56a5 Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/320e56a5 Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/320e56a5 Branch: refs/heads/KYLIN-2624 Commit: 320e56a532cf6d4968430c8111d239961a07c7f8 Parents: b3ba338 Author: kangkaisen <kangkai...@163.com> Authored: Thu May 11 14:57:29 2017 +0800 Committer: kangkaisen <kangkai...@live.com> Committed: Fri May 12 16:47:39 2017 +0800 ---------------------------------------------------------------------- .../apache/kylin/common/restclient/RestClient.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/320e56a5/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java ---------------------------------------------------------------------- diff --git a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java index 825c67f..33a4e7a 100644 --- a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java +++ b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java @@ -40,6 +40,9 @@ import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpConnectionParams; +import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import org.apache.kylin.common.util.JsonUtil; @@ -59,6 +62,9 @@ public class RestClient { protected static Pattern fullRestPattern = Pattern.compile("(?:([^:]+)[:]([^@]+)[@])?([^:]+)(?:[:](\\d+))?"); + private static final int HTTP_CONNECTION_TIMEOUT_MS = 30000; + private static final int HTTP_SOCKET_TIMEOUT_MS = 120000; + public static boolean matchFullRestPattern(String uri) { Matcher m = fullRestPattern.matcher(uri); return m.matches(); @@ -93,7 +99,11 @@ public class RestClient { this.password = password; this.baseUrl = "http://" + host + ":" + port + "/kylin/api"; - client = new DefaultHttpClient(); + final HttpParams httpParams = new BasicHttpParams(); + HttpConnectionParams.setSoTimeout(httpParams, HTTP_SOCKET_TIMEOUT_MS); + HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_CONNECTION_TIMEOUT_MS); + + client = new DefaultHttpClient(httpParams); if (userName != null && password != null) { CredentialsProvider provider = new BasicCredentialsProvider(); @@ -263,14 +273,14 @@ public class RestClient { InputStreamReader reader = null; BufferedReader rd = null; StringBuffer result = new StringBuffer(); - try{ + try { reader = new InputStreamReader(response.getEntity().getContent()); rd = new BufferedReader(reader); String line = null; while ((line = rd.readLine()) != null) { result.append(line); } - }finally { + } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(rd); }