@Test void executeWithRetryImmediatelyStopsOnOther() { AtomicLong retryCounter = new AtomicLong(); try { ProcessUtils.executeWithRetry(() -> { if (retryCounter.getAndIncrement() < 11) { throw new IllegalArgumentException("Waiting for 11"); } }, e -> false, 100, TimeUnit.MILLISECONDS, 10); fail("Should not get here"); } catch (Exception ex) { assertTrue(ex instanceof IllegalArgumentException); assertEquals(1, retryCounter.get()); } }
public static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate, long timeout, TimeUnit timeUnit, long retryInterval) { long completeBefore = System.currentTimeMillis() + timeUnit.toMillis(timeout); RuntimeException lastException = new RuntimeException(); while (completeBefore > System.currentTimeMillis()) { try { runnable.run(); return; } catch (RuntimeException re) { if (!retryPredicate.test(re)) { throw re; } try { Thread.sleep(retryInterval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw re; } lastException = re; } } throw lastException; }
ProcessUtils { public static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate, long timeout, TimeUnit timeUnit, long retryInterval) { long completeBefore = System.currentTimeMillis() + timeUnit.toMillis(timeout); RuntimeException lastException = new RuntimeException(); while (completeBefore > System.currentTimeMillis()) { try { runnable.run(); return; } catch (RuntimeException re) { if (!retryPredicate.test(re)) { throw re; } try { Thread.sleep(retryInterval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw re; } lastException = re; } } throw lastException; } }
ProcessUtils { public static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate, long timeout, TimeUnit timeUnit, long retryInterval) { long completeBefore = System.currentTimeMillis() + timeUnit.toMillis(timeout); RuntimeException lastException = new RuntimeException(); while (completeBefore > System.currentTimeMillis()) { try { runnable.run(); return; } catch (RuntimeException re) { if (!retryPredicate.test(re)) { throw re; } try { Thread.sleep(retryInterval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw re; } lastException = re; } } throw lastException; } private  ProcessUtils(); }
ProcessUtils { public static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate, long timeout, TimeUnit timeUnit, long retryInterval) { long completeBefore = System.currentTimeMillis() + timeUnit.toMillis(timeout); RuntimeException lastException = new RuntimeException(); while (completeBefore > System.currentTimeMillis()) { try { runnable.run(); return; } catch (RuntimeException re) { if (!retryPredicate.test(re)) { throw re; } try { Thread.sleep(retryInterval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw re; } lastException = re; } } throw lastException; } private  ProcessUtils(); static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate,
                                        long timeout, TimeUnit timeUnit,
                                        long retryInterval); }
ProcessUtils { public static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate, long timeout, TimeUnit timeUnit, long retryInterval) { long completeBefore = System.currentTimeMillis() + timeUnit.toMillis(timeout); RuntimeException lastException = new RuntimeException(); while (completeBefore > System.currentTimeMillis()) { try { runnable.run(); return; } catch (RuntimeException re) { if (!retryPredicate.test(re)) { throw re; } try { Thread.sleep(retryInterval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw re; } lastException = re; } } throw lastException; } private  ProcessUtils(); static void executeWithRetry(Runnable runnable, Predicate<RuntimeException> retryPredicate,
                                        long timeout, TimeUnit timeUnit,
                                        long retryInterval);  }