I want to a create a functional interface or default method where I pass in a jpa repository method such as findById
or findByNumber
and have it return a generic type; this way I annotate it with @Retryable
if the application fails to connect to db. Below are examples of methods I want passed through the functional interface:
Employee employee = repository.findById("2ed21");
Record record = repository.findbyNumber(311);
How about a simple method with a Supplier
as a parameter where you can put literally anything?
@Retryable(...)
public <T> T retry(Supplier<T> supplier) {
return supplier.get();
}
Usage:
Employee employee = retry(() -> repository.findById("2ed21"));
Record record = retry(() -> repository.findbyNumber(311));
Few notes:
@Retryable
from a different bean to overcome a limitation of Spring AOP. To be honest I have never worked with @Retryable
but I guess it would work the same way as @Transactional
, related: Spring @Transaction method call by the method within the same class, does not work?