use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class CircuitBreakerInterceptor method findFallbackMethod.
/**
* Finds a fallback method for the given context.
*
* @param context The context
* @return The fallback method if it is present
*/
@Override
public Optional<? extends MethodExecutionHandle<?, Object>> findFallbackMethod(MethodInvocationContext<Object, Object> context) {
ExecutableMethod executableMethod = context.getExecutableMethod();
final String fallbackMethod = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.CircuitBreaker.class, "fallbackMethod").orElse("");
Class<?> declaringType = context.getDeclaringType();
return executionHandleLocator.findExecutionHandle(declaringType, fallbackMethod, context.getArgumentTypes());
}
use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class RateLimiterInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
Optional<AnnotationValue<io.github.resilience4j.micronaut.annotation.RateLimiter>> opt = context.findAnnotation(io.github.resilience4j.micronaut.annotation.RateLimiter.class);
if (!opt.isPresent()) {
return context.proceed();
}
ExecutableMethod executableMethod = context.getExecutableMethod();
final String name = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.RateLimiter.class, "name").orElse("default");
RateLimiter rateLimiter = this.rateLimiterRegistry.rateLimiter(name);
InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
try {
switch(interceptedMethod.resultType()) {
case PUBLISHER:
return interceptedMethod.handleResult(fallbackReactiveTypes(Flowable.fromPublisher(interceptedMethod.interceptResultAsPublisher()).compose(RateLimiterOperator.of(rateLimiter)), context));
case COMPLETION_STAGE:
return interceptedMethod.handleResult(fallbackForFuture(rateLimiter.executeCompletionStage(() -> {
try {
return interceptedMethod.interceptResultAsCompletionStage();
} catch (Exception e) {
throw new CompletionException(e);
}
}), context));
case SYNCHRONOUS:
try {
return rateLimiter.executeCheckedSupplier(context::proceed);
} catch (Throwable exception) {
return fallback(context, exception);
}
default:
return interceptedMethod.unsupported();
}
} catch (Exception e) {
return interceptedMethod.handleException(e);
}
}
use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class BulkheadInterceptor method findFallbackMethod.
/**
* Finds a fallback method for the given context.
*
* @param context The context
* @return The fallback method if it is present
*/
@Override
public Optional<? extends MethodExecutionHandle<?, Object>> findFallbackMethod(MethodInvocationContext<Object, Object> context) {
ExecutableMethod executableMethod = context.getExecutableMethod();
final String fallbackMethod = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.Bulkhead.class, "fallbackMethod").orElse("");
Class<?> declaringType = context.getDeclaringType();
return executionHandleLocator.findExecutionHandle(declaringType, fallbackMethod, context.getArgumentTypes());
}
use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class RetryInterceptor method findFallbackMethod.
/**
* Finds a fallback method for the given context.
*
* @param context The context
* @return The fallback method if it is present
*/
@Override
public Optional<? extends MethodExecutionHandle<?, Object>> findFallbackMethod(MethodInvocationContext<Object, Object> context) {
ExecutableMethod executableMethod = context.getExecutableMethod();
final String fallbackMethod = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.Retry.class, "fallbackMethod").orElse("");
Class<?> declaringType = context.getDeclaringType();
return executionHandleLocator.findExecutionHandle(declaringType, fallbackMethod, context.getArgumentTypes());
}
use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class RetryInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
Optional<AnnotationValue<io.github.resilience4j.micronaut.annotation.Retry>> opt = context.findAnnotation(io.github.resilience4j.micronaut.annotation.Retry.class);
if (!opt.isPresent()) {
return context.proceed();
}
ExecutableMethod executableMethod = context.getExecutableMethod();
final String name = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.Retry.class, "name").orElse("default");
Retry retry = retryRegistry.retry(name);
InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
try {
switch(interceptedMethod.resultType()) {
case PUBLISHER:
return interceptedMethod.handleResult(fallbackReactiveTypes(Flowable.fromPublisher(interceptedMethod.interceptResultAsPublisher()).compose(RetryTransformer.of(retry)), context));
case COMPLETION_STAGE:
return interceptedMethod.handleResult(fallbackForFuture(retry.executeCompletionStage(executorService, () -> {
try {
return interceptedMethod.interceptResultAsCompletionStage();
} catch (Exception e) {
throw new CompletionException(e);
}
}), context));
case SYNCHRONOUS:
try {
return retry.executeCheckedSupplier(context::proceed);
} catch (Throwable exception) {
return fallback(context, exception);
}
default:
return interceptedMethod.unsupported();
}
} catch (Exception e) {
return interceptedMethod.handleException(e);
}
}
Aggregations