Skip to content

Instantly share code, notes, and snippets.

@skaveesh
Last active January 3, 2021 04:36
Show Gist options
  • Save skaveesh/e6590f5eec985837c231fb741d62032c to your computer and use it in GitHub Desktop.
Save skaveesh/e6590f5eec985837c231fb741d62032c to your computer and use it in GitHub Desktop.
How I Decoupled Circuit Breaker from the Code with AOP in Spring Boot for Better Code Maintenance
//code is omitted for brevity
public final class SupplierUtil {
@FunctionalInterface
public interface SupplierWithException<T, E extends Exception> {
T get() throws Throwable;
}
public static <T, E extends Exception> Supplier<T> rethrowSupplier(SupplierWithException<T, E> supplier) {
return () -> {
try {
return supplier.get();
} catch (Throwable exception) {
throwAsUnchecked(exception);
return null;
}
};
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAsUnchecked(Throwable exception) throws E {
throw (E) exception;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment