minor, restClient support set http timeout

Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/63de91df
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/63de91df
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/63de91df

Branch: refs/heads/master
Commit: 63de91df546c28c4aa745f61619eeb8797eb7b44
Parents: c6e45a7
Author: Chao Long <wayn...@qq.com>
Authored: Wed Dec 27 18:12:19 2017 +0800
Committer: Dong Li <lid...@apache.org>
Committed: Wed Dec 27 18:58:35 2017 +0800

----------------------------------------------------------------------
 .../kylin/common/restclient/RestClient.java     | 27 +++++++++++++++++---
 .../kylin/rest/controller/BasicController.java  |  2 +-
 2 files changed, 24 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/63de91df/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 67ed9c1..02045ae 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
@@ -61,8 +61,8 @@ 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;
+    private int httpConnectionTimeoutMs = 30000;
+    private int httpSocketTimeoutMs = 120000;
 
     public static final String SCHEME_HTTP = "http://";;
 
@@ -86,6 +86,10 @@ public class RestClient {
      * @param uri "user:pwd@host:port"
      */
     public RestClient(String uri) {
+        this(uri, null, null);
+    }
+
+    public RestClient(String uri, Integer httpConnectionTimeoutMs, Integer 
httpSocketTimeoutMs) {
         Matcher m = fullRestPattern.matcher(uri);
         if (!m.matches())
             throw new IllegalArgumentException("URI: " + uri + " -- does not 
match pattern " + fullRestPattern);
@@ -96,10 +100,25 @@ public class RestClient {
         String portStr = m.group(4);
         int port = Integer.parseInt(portStr == null ? "7070" : portStr);
 
+        if (httpConnectionTimeoutMs != null)
+            this.httpConnectionTimeoutMs = httpConnectionTimeoutMs;
+        if (httpSocketTimeoutMs != null)
+            this.httpSocketTimeoutMs = httpSocketTimeoutMs;
+
         init(host, port, user, pwd);
     }
 
     public RestClient(String host, int port, String userName, String password) 
{
+        this(host, port, userName, password, null, null);
+    }
+
+    public RestClient(String host, int port, String userName, String password, 
Integer httpConnectionTimeoutMs,
+            Integer httpSocketTimeoutMs) {
+        if (httpConnectionTimeoutMs != null)
+            this.httpConnectionTimeoutMs = httpConnectionTimeoutMs;
+        if (httpSocketTimeoutMs != null)
+            this.httpSocketTimeoutMs = httpSocketTimeoutMs;
+
         init(host, port, userName, password);
     }
 
@@ -111,8 +130,8 @@ public class RestClient {
         this.baseUrl = SCHEME_HTTP + host + ":" + port + KYLIN_API_PATH;
 
         final HttpParams httpParams = new BasicHttpParams();
-        HttpConnectionParams.setSoTimeout(httpParams, HTTP_SOCKET_TIMEOUT_MS);
-        HttpConnectionParams.setConnectionTimeout(httpParams, 
HTTP_CONNECTION_TIMEOUT_MS);
+        HttpConnectionParams.setSoTimeout(httpParams, httpSocketTimeoutMs);
+        HttpConnectionParams.setConnectionTimeout(httpParams, 
httpConnectionTimeoutMs);
 
         final PoolingClientConnectionManager cm = new 
PoolingClientConnectionManager();
         KylinConfig config = KylinConfig.getInstanceFromEnv();

http://git-wip-us.apache.org/repos/asf/kylin/blob/63de91df/server-base/src/main/java/org/apache/kylin/rest/controller/BasicController.java
----------------------------------------------------------------------
diff --git 
a/server-base/src/main/java/org/apache/kylin/rest/controller/BasicController.java
 
b/server-base/src/main/java/org/apache/kylin/rest/controller/BasicController.java
index b77309e..539a64c 100644
--- 
a/server-base/src/main/java/org/apache/kylin/rest/controller/BasicController.java
+++ 
b/server-base/src/main/java/org/apache/kylin/rest/controller/BasicController.java
@@ -109,7 +109,7 @@ public class BasicController {
 
     protected void setDownloadResponse(String downloadFile, final 
HttpServletResponse response) {
         File file = new File(downloadFile);
-        try (InputStream fileInputStream = new FileInputStream(file); 
OutputStream output = response.getOutputStream();) {
+        try (InputStream fileInputStream = new FileInputStream(file); 
OutputStream output = response.getOutputStream()) {
             response.reset();
             response.setContentType("application/octet-stream");
             response.setContentLength((int) (file.length()));

Reply via email to