All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.castled.interceptors.RetryInterceptor Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

@Slf4j
public class RetryInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        final Method method = methodInvocation.getMethod();
        final Retry retry = method.getAnnotation(Retry.class);
        int attempt = 0;
        final Class[] types = retry.types();
        while (true) {
            try {
                return methodInvocation.proceed();
            }
            catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                throw ex;
            }
            catch (final Exception ex) {
                if(matches(ex.getClass(), retry.ignore()) || !matches(ex.getClass(), types)) {
                    throw ex;
                }
                if(++attempt >= retry.attempts()) {
                    log.warn(String.format("Failed after several retries. [%d] for [%s]", attempt, method.getName()), ex);
                    throw ex;
                }
                if(retry.waitTime() > 0) {
                    Thread.sleep(retry.waitTime());
                }
                log.debug(String.format("Attempt [%d] for [%s] because of [%s]", attempt, method.getName(), ex.getMessage()));
            }
        }
    }

    private boolean matches(final Class thrown, final Class... types) {
        for (final Class type : types) {
            if (type.isAssignableFrom(thrown)) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy