@Test public void testRetryCallOnFailNoRetrials() throws Exception { Retriable<String> retriableString = new Retriable<String>() { @Override public String retriableCall() throws NoSuchAlgorithmException { return "TestString"; } }; Assert.assertEquals("TestString", retriableString.retryCallOnFail(1, 0)); Retriable<Object> retriableThrows = new Retriable<Object>() { @Override public Object retriableCall() throws NoSuchAlgorithmException { throw new NoSuchAlgorithmException(); } }; try { retriableThrows.retryCallOnFail(1, 0); Assert.fail("retriableThrows does not throw!"); } catch (RetriableException e) { Assert.assertTrue("Expected exception cause is not NoSuchAlgorithmException", e.getCause() instanceof NoSuchAlgorithmException); Assert.assertEquals("Feature not available.", e.getMessage()); } }
public T retryCallOnFail(int times, long timeout) throws RetriableException { int trial = 0; while (true) { try { trial++; return retriableCall(); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof java.net.UnknownHostException) { if (trial == times) { throw new RetriableException("Failed after " + times + " tries.", e); } LOGGER.debug("Retrying (" + (times - trial) + " tries left)."); waitTimeout(timeout); } else { throw new RetriableException(e instanceof NoSuchAlgorithmException ? "Feature not available." : "Can not retry.", e); } } } }
Retriable { public T retryCallOnFail(int times, long timeout) throws RetriableException { int trial = 0; while (true) { try { trial++; return retriableCall(); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof java.net.UnknownHostException) { if (trial == times) { throw new RetriableException("Failed after " + times + " tries.", e); } LOGGER.debug("Retrying (" + (times - trial) + " tries left)."); waitTimeout(timeout); } else { throw new RetriableException(e instanceof NoSuchAlgorithmException ? "Feature not available." : "Can not retry.", e); } } } } }
Retriable { public T retryCallOnFail(int times, long timeout) throws RetriableException { int trial = 0; while (true) { try { trial++; return retriableCall(); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof java.net.UnknownHostException) { if (trial == times) { throw new RetriableException("Failed after " + times + " tries.", e); } LOGGER.debug("Retrying (" + (times - trial) + " tries left)."); waitTimeout(timeout); } else { throw new RetriableException(e instanceof NoSuchAlgorithmException ? "Feature not available." : "Can not retry.", e); } } } }  }
Retriable { public T retryCallOnFail(int times, long timeout) throws RetriableException { int trial = 0; while (true) { try { trial++; return retriableCall(); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof java.net.UnknownHostException) { if (trial == times) { throw new RetriableException("Failed after " + times + " tries.", e); } LOGGER.debug("Retrying (" + (times - trial) + " tries left)."); waitTimeout(timeout); } else { throw new RetriableException(e instanceof NoSuchAlgorithmException ? "Feature not available." : "Can not retry.", e); } } } }  abstract T retriableCall(); T retryCallOnFail(int times, long timeout); }
Retriable { public T retryCallOnFail(int times, long timeout) throws RetriableException { int trial = 0; while (true) { try { trial++; return retriableCall(); } catch (Exception e) { if (e.getCause() != null && e.getCause() instanceof java.net.UnknownHostException) { if (trial == times) { throw new RetriableException("Failed after " + times + " tries.", e); } LOGGER.debug("Retrying (" + (times - trial) + " tries left)."); waitTimeout(timeout); } else { throw new RetriableException(e instanceof NoSuchAlgorithmException ? "Feature not available." : "Can not retry.", e); } } } }  abstract T retriableCall(); T retryCallOnFail(int times, long timeout);  }