From 66ad69cbe06dbd91b8fb3135f2c643dee6e93e3f Mon Sep 17 00:00:00 2001 From: j4bbi Date: Thu, 10 Dec 2020 16:55:01 +0000 Subject: [PATCH] Add HTTP connection timeouts --- .../export/ExportUsageEventListener.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/dspace/modules/atmire-statistics-exporter/atmire-statistics-exporter-api/src/main/java/com/atmire/statistics/export/ExportUsageEventListener.java b/dspace/modules/atmire-statistics-exporter/atmire-statistics-exporter-api/src/main/java/com/atmire/statistics/export/ExportUsageEventListener.java index c240afd..43089ac 100644 --- a/dspace/modules/atmire-statistics-exporter/atmire-statistics-exporter-api/src/main/java/com/atmire/statistics/export/ExportUsageEventListener.java +++ b/dspace/modules/atmire-statistics-exporter/atmire-statistics-exporter-api/src/main/java/com/atmire/statistics/export/ExportUsageEventListener.java @@ -339,18 +339,27 @@ private static void processUrl(Context c, String urlStr) throws IOException, SQL URL url = new URL(urlStr); conn = url.openConnection(); + /* + Set live connection timeout to a low value. + If the responses are slow we are fine to wait until a better time. + */ + conn.setConnectTimeout(1000); + conn.setReadTimeout(1000); + // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while (rd.readLine() != null) ; rd.close(); if (((HttpURLConnection) conn).getResponseCode() != 200) { + log.error("IRUS server responded with code " + ((HttpURLConnection) conn).getResponseCode() + " : " + urlStr); ExportUsageEventListener.logfailed(c, urlStr); } else if (log.isDebugEnabled()) { log.debug("Successfully posted " + urlStr + " on " + new Date()); } - } catch (Exception e) { - log.error("Failed to send url to tracker URL: " + urlStr); + } + catch (Exception e) { + log.error("Failed to send url to tracker URL: " + urlStr + " - " + e.getMessage()); ExportUsageEventListener.logfailed(c, urlStr); } } @@ -361,6 +370,13 @@ private static void tryReprocessFailed(Context context, OpenURLTracker tracker) try { URL url = new URL(tracker.getUrl()); conn = url.openConnection(); + + /* + Set retry connection timeout to a higher value. + */ + conn.setConnectTimeout(5000); + conn.setReadTimeout(5000); + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while (rd.readLine() != null) ; rd.close();