com.github.gossie.circuitbreaker.CircuitBreaker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of circuit-breaker Show documentation
Show all versions of circuit-breaker Show documentation
A lightweight circuit breaker framework
The newest version!
package com.github.gossie.circuitbreaker;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.Optional;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
/**
* The {@link CircuitBreaker} is implemented as an AspectJ {@link Aspect}. For each class annotated with
* the {@link IntegrationPoint} annotation, a {@link CircuitBreaker} instance is created.
*/
@Aspect("perthis(@within(IntegrationPointConfiguration))")
public class CircuitBreaker {
private final ExecutorService threadpool;
private volatile State state;
public CircuitBreaker() {
threadpool = Executors.newFixedThreadPool(1);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
threadpool.shutdown();
}
});
}
/**
* The method wraps around each method annotated with the {@link IntegrationPoint} annotation.
*
* @param jointPoint The {@link ProceedingJoinPoint} representing the original method invocation.
* @throws InterruptedException Is thrown if this {@link Thread} is interrupted while waiting.
*/
@Around("execution(* *(..)) && @annotation(IntegrationPoint)")
public Object call(ProceedingJoinPoint jointPoint) throws InterruptedException {
IntegrationPoint integrationPoint = retrieveAnntotation(jointPoint);
initializeState(jointPoint);
if (state.isOpen()) {
return determineEmptyResult(jointPoint);
}
long timeout = integrationPoint.errorTimeout();
Object result;
Future
© 2015 - 2024 Weber Informatics LLC | Privacy Policy