Displaying differences for changeset
 
display as  

src/csip/sdm/SDMClient.java

@@ -65,33 +65,31 @@
  */
 public class SDMClient {
 
+    private static int MAX_RETRIES = 10;
+    private static int THREAD_SLEEP_ON_RETRY = 500;  // 1/2 second, wait between retries.
     private String url;
 
     private static final PoolingHttpClientConnectionManager CM;
 
-    static ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
-        @Override
-        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
-            // Honor 'keep-alive' header
-            HeaderElementIterator it = new BasicHeaderElementIterator(
-                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
-            while (it.hasNext()) {
-                HeaderElement he = it.nextElement();
-                String param = he.getName();
-                String value = he.getValue();
-                if (value != null && param.equalsIgnoreCase("timeout")) {
-                    try {
-                        return Long.parseLong(value) * 1000;
-                    } catch (NumberFormatException ignore) {
-                    }
+    static ConnectionKeepAliveStrategy myStrategy = (HttpResponse response, HttpContext context) -> {
+        // Honor 'keep-alive' header
+        HeaderElementIterator it = new BasicHeaderElementIterator(
+                response.headerIterator(HTTP.CONN_KEEP_ALIVE));
+        while (it.hasNext()) {
+            HeaderElement he = it.nextElement();
+            String param = he.getName();
+            String value = he.getValue();
+            if (value != null && param.equalsIgnoreCase("timeout")) {
+                try {
+                    return Long.parseLong(value) * 1000;
+                } catch (NumberFormatException ignore) {
                 }
             }
-            HttpHost target = (HttpHost) context.getAttribute(
-                    HttpClientContext.HTTP_TARGET_HOST);
+        }
+        HttpHost target = (HttpHost) context.getAttribute(
+                HttpClientContext.HTTP_TARGET_HOST);
 
-            return 30 * 1000;
-        }
-
+        return 30 * 1000;
     };
 
     static {
@@ -103,9 +101,9 @@
                         .build()
         );
 
-        CM.setMaxTotal(30);
+        CM.setMaxTotal(3000);
         // Increase default max connection per route to 75
-        CM.setDefaultMaxPerRoute(25);        
+        CM.setDefaultMaxPerRoute(2500);
         // Increase max connections for localhost:80 to 125
         //HttpHost localhost = new HttpHost("locahost", 80);
         //CM.setMaxPerRoute(new HttpRoute(localhost), 150);   
@@ -187,6 +185,8 @@
      * @throws java.sql.SQLException
      */
     JSONObject doPOST(JSONObject req) throws SQLException {
+        int retryCount = 0;
+
         EntityBuilder builder = EntityBuilder.create();
         builder.setText(req.toString());
         builder.setContentType(ContentType.APPLICATION_JSON);
@@ -199,23 +199,38 @@
             log.info("REQUEST JSON: " + req);
         }
 
-        try (CloseableHttpResponse httpResponse = httpClient.execute(post)) {
-            InputStream responseStream = httpResponse.getEntity().getContent();
-            String response = IOUtils.toString(responseStream);
-            if (log != null && log.isLoggable(Level.INFO)) {
-                log.info("RESPONSE JSON: " + response);
-                PoolStats ps = CM.getTotalStats();
-                log.info("http-client-connections pool [avail=" + ps.getAvailable() + " leased=" + ps.getLeased() + " pending=" + ps.getPending() + "] ");
+        while (retryCount < MAX_RETRIES) {
+            try (CloseableHttpResponse httpResponse = httpClient.execute(post)) {
+                InputStream responseStream = httpResponse.getEntity().getContent();
+                String response = IOUtils.toString(responseStream);
+                if (log != null && log.isLoggable(Level.INFO)) {
+                    log.info("RESPONSE JSON: " + response);
+                    PoolStats ps = CM.getTotalStats();
+                    log.info("http-client-connections pool [avail=" + ps.getAvailable() + " leased=" + ps.getLeased() + " pending=" + ps.getPending() + "] ");
+                }
+                return new JSONObject(response);
+            } catch (JSONException ex) {
+                throw new SQLException("Invalid SDM return data: " + ex.getMessage(), ex);
+            } catch (IOException ex) {
+                String exMessage = ex.getMessage();
+                if (exMessage.toLowerCase().contains("socket is closed") || exMessage.toLowerCase().contains("connection refused")) {
+                    retryCount++;
+                    log.warning("Retrying: " + retryCount + ", for" + url + ":  " + exMessage);
+                    try{
+                        Thread.sleep(THREAD_SLEEP_ON_RETRY);
+                    } catch (InterruptedException ex1) {
+                        log.info("Thread sleep in retry of doPost interrupted.");
+                    }
+                } else {
+                    throw new SQLException("Communication error with SDM host: " + exMessage, ex);
+                }
+            } finally {
+                //post.releaseConnection();    
+                post.completed();
             }
-            return new JSONObject(response);
-        } catch (JSONException ex) {
-            throw new SQLException("Invalid SDM return data: " + ex.getMessage(), ex);
-        } catch (IOException ex) {
-            throw new SQLException("Communication error with SDM host: " + ex.getMessage(), ex);
-        } finally {
-            //post.releaseConnection();    
-            post.completed();
         }
+
+        return null;
     }
 
     private class PreemptiveAuthInterceptor implements HttpRequestInterceptor {