/** * A supplier capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Supplier with a retry strategy */ public FluentSupplier<R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of(() -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.get(); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())) .run(); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A Function capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Function with a retry strategy */ public FluentFunction<T, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of(t -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A BiFunction capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return BiFunction with a retry strategy */ public FluentBiFunction<T1, T2, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of((t1, t2) -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t1, t2); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
@Test public void testMutate(){ MutableInt num = MutableInt.of(20); Stream.of(1,2,3,4).map(i->i*10).peek(i-> num.mutate(n->n+i)).forEach(System.out::println); assertThat(num.getAsInt(),is(120)); } @Test
/** * A supplier capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Supplier with a retry strategy */ public FluentRunnable retry(final int times, final int backoffStartTime) { return FluentFunctions.ofRunnable(() -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { run(); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())) .run(); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A TriFunction capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return TriFunction with a retry strategy */ public FluentTriFunction<T1, T2, T3, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of((t1, t2, t3) -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t1, t2, t3); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A supplier capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Supplier with a retry strategy */ public FluentSupplier<R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of(() -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.get(); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())) .run(); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A BiFunction capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return BiFunction with a retry strategy */ public FluentBiFunction<T1, T2, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of((t1, t2) -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t1, t2); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A Function capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Function with a retry strategy */ public FluentFunction<T, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of(t -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A supplier capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return Supplier with a retry strategy */ public FluentRunnable retry(final int times, final int backoffStartTime) { return FluentFunctions.ofRunnable(() -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { run(); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())) .run(); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }
/** * A TriFunction capable of retrying on failure using an exponential backoff strategy * * @param times Number of times to retry * @param backoffStartTime Wait time before first retry * @return TriFunction with a retry strategy */ public FluentTriFunction<T1, T2, T3, R> retry(final int times, final int backoffStartTime) { return FluentFunctions.of((t1, t2, t3) -> { int count = times; final MutableInt sleep = MutableInt.of(backoffStartTime); Throwable exception = null; while (count-- > 0) { try { return fn.apply(t1, t2, t3); } catch (final Throwable e) { exception = e; } ExceptionSoftener.softenRunnable(() -> Thread.sleep(sleep.get())); sleep.mutate(s -> s * 2); } throw ExceptionSoftener.throwSoftenedException(exception); }); }