Search in sources :

Example 1 with ExecutableMethod

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());
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ExecutableMethod(io.micronaut.inject.ExecutableMethod)

Example 2 with ExecutableMethod

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);
    }
}
Also used : ExecutableMethod(io.micronaut.inject.ExecutableMethod) InterceptedMethod(io.micronaut.aop.InterceptedMethod) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) AnnotationValue(io.micronaut.core.annotation.AnnotationValue)

Example 3 with ExecutableMethod

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());
}
Also used : ExecutableMethod(io.micronaut.inject.ExecutableMethod)

Example 4 with ExecutableMethod

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());
}
Also used : ExecutableMethod(io.micronaut.inject.ExecutableMethod) Retry(io.github.resilience4j.retry.Retry)

Example 5 with ExecutableMethod

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);
    }
}
Also used : ExecutableMethod(io.micronaut.inject.ExecutableMethod) InterceptedMethod(io.micronaut.aop.InterceptedMethod) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Retry(io.github.resilience4j.retry.Retry)

Aggregations

ExecutableMethod (io.micronaut.inject.ExecutableMethod)17 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)11 BeanContext (io.micronaut.context.BeanContext)8 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)8 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)7 BoundExecutable (io.micronaut.core.bind.BoundExecutable)7 DefaultExecutableBinder (io.micronaut.core.bind.DefaultExecutableBinder)7 BeanDefinition (io.micronaut.inject.BeanDefinition)7 MessageBody (io.micronaut.messaging.annotation.MessageBody)6 Singleton (jakarta.inject.Singleton)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Argument (io.micronaut.core.type.Argument)5 Map (java.util.Map)5 ExecutorService (java.util.concurrent.ExecutorService)5 InterceptedMethod (io.micronaut.aop.InterceptedMethod)4 StringUtils (io.micronaut.core.util.StringUtils)4 ConsumerAware (io.micronaut.configuration.kafka.ConsumerAware)3 ConsumerRegistry (io.micronaut.configuration.kafka.ConsumerRegistry)3 KafkaAcknowledgement (io.micronaut.configuration.kafka.KafkaAcknowledgement)3