From db820647af93fd571a2a99c941eaf455f9b54891 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 30 Mar 2013 11:00:14 +0000 Subject: [PATCH] * enable 3x fetch-retry for important resources --- .../filebot/web/CachedResource.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/source/net/sourceforge/filebot/web/CachedResource.java b/source/net/sourceforge/filebot/web/CachedResource.java index 0b97a7a7..0361155d 100644 --- a/source/net/sourceforge/filebot/web/CachedResource.java +++ b/source/net/sourceforge/filebot/web/CachedResource.java @@ -22,12 +22,8 @@ public abstract class CachedResource { private Class type; private long expirationTime; - - public CachedResource(String resource, Class type, long expirationTime) { - this.resource = resource; - this.type = type; - this.expirationTime = expirationTime; - } + private int retryCountLimit; + private long retryWaitTime; public CachedResource(String resource, Class type) { @@ -35,6 +31,20 @@ public abstract class CachedResource { } + public CachedResource(String resource, Class type, long expirationTime) { + this(resource, type, expirationTime, 2, 1000); + } + + + public CachedResource(String resource, Class type, long expirationTime, int retryCountLimit, long retryWaitTime) { + this.resource = resource; + this.type = type; + this.expirationTime = expirationTime; + this.retryCountLimit = retryCountLimit; + this.retryWaitTime = retryWaitTime; + } + + protected Cache getCache() { return CacheManager.getInstance().getCache("web-persistent-datasource"); } @@ -82,9 +92,13 @@ public abstract class CachedResource { IOException networkException = null; try { - data = fetchData(new URL(resource), element != null ? lastUpdateTime : 0); + long lastModified = element != null ? lastUpdateTime : 0; + URL url = new URL(resource); + data = fetch(url, lastModified); } catch (IOException e) { networkException = e; + } catch (InterruptedException e) { + throw new RuntimeException(e); } if (data != null) { @@ -114,8 +128,9 @@ public abstract class CachedResource { // throw network error only if we can't use previously cached data if (networkException != null) { - if (product == null) + if (product == null) { throw networkException; + } // just log error and continue with cached data Logger.getLogger(getClass().getName()).log(Level.WARNING, networkException.toString()); @@ -124,4 +139,18 @@ public abstract class CachedResource { return product; } + + protected ByteBuffer fetch(URL url, long lastModified) throws IOException, InterruptedException { + for (int i = 0; retryCountLimit < 0 || i <= retryCountLimit; i++) { + try { + return fetchData(url, lastModified); + } catch (IOException e) { + if (i >= 0 && i >= retryCountLimit) { + throw e; + } + Thread.sleep(retryWaitTime); + } + } + return null; // can't happen + } }