use of io.micronaut.inject.ExecutableMethod in project resilience4j by resilience4j.
the class CircuitBreakerInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
Optional<AnnotationValue<io.github.resilience4j.micronaut.annotation.CircuitBreaker>> opt = context.findAnnotation(io.github.resilience4j.micronaut.annotation.CircuitBreaker.class);
if (!opt.isPresent()) {
return context.proceed();
}
ExecutableMethod executableMethod = context.getExecutableMethod();
final String name = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.CircuitBreaker.class, "name").orElse("default");
CircuitBreaker circuitBreaker = this.circuitBreakerRegistry.circuitBreaker(name);
InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
try {
switch(interceptedMethod.resultType()) {
case PUBLISHER:
return interceptedMethod.handleResult(fallbackReactiveTypes(Flowable.fromPublisher(interceptedMethod.interceptResultAsPublisher()).compose(CircuitBreakerOperator.of(circuitBreaker)), context));
case COMPLETION_STAGE:
return interceptedMethod.handleResult(fallbackForFuture(circuitBreaker.executeCompletionStage(() -> {
try {
return interceptedMethod.interceptResultAsCompletionStage();
} catch (Exception e) {
throw new CompletionException(e);
}
}), context));
case SYNCHRONOUS:
try {
return circuitBreaker.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 RateLimiterInterceptor method findFallbackMethod.
/**
* Finds a fallback method for the given context.
*
* @param context The context
* @return The fallback method if it is present
*/
public Optional<? extends MethodExecutionHandle<?, Object>> findFallbackMethod(MethodInvocationContext<Object, Object> context) {
ExecutableMethod executableMethod = context.getExecutableMethod();
final String fallbackMethod = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.RateLimiter.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 TimeLimiterInterceptor method intercept.
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
Optional<AnnotationValue<io.github.resilience4j.micronaut.annotation.TimeLimiter>> opt = context.findAnnotation(io.github.resilience4j.micronaut.annotation.TimeLimiter.class);
if (!opt.isPresent()) {
return context.proceed();
}
ExecutableMethod executableMethod = context.getExecutableMethod();
final String name = executableMethod.stringValue(io.github.resilience4j.micronaut.annotation.TimeLimiter.class).orElse("default");
TimeLimiter timeLimiter = this.timeLimiterRegistry.timeLimiter(name);
InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
try {
switch(interceptedMethod.resultType()) {
case PUBLISHER:
return interceptedMethod.handleResult(fallbackReactiveTypes(Flowable.fromPublisher(interceptedMethod.interceptResultAsPublisher()).compose(TimeLimiterTransformer.of(timeLimiter)), context));
case COMPLETION_STAGE:
return interceptedMethod.handleResult(fallbackForFuture(timeLimiter.executeCompletionStage(executorService, () -> {
try {
return interceptedMethod.interceptResultAsCompletionStage();
} catch (Exception e) {
throw new CompletionException(e);
}
}), context));
case SYNCHRONOUS:
try {
return timeLimiter.executeFutureSupplier(() -> CompletableFuture.supplyAsync(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 TimeLimiterInterceptor 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.TimeLimiter.class, "fallbackMethod").orElse("");
Class<?> declaringType = context.getDeclaringType();
return executionHandleLocator.findExecutionHandle(declaringType, fallbackMethod, context.getArgumentTypes());
}
use of io.micronaut.inject.ExecutableMethod in project micronaut-kafka by micronaut-projects.
the class KafkaConsumerProcessor method configureDeserializers.
private void configureDeserializers(final ExecutableMethod<?, ?> method, final DefaultKafkaConsumerConfiguration consumerConfiguration) {
final Properties properties = consumerConfiguration.getConfig();
// figure out the Key deserializer
final Argument<?> bodyArgument = findBodyArgument(method);
if (!properties.containsKey(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG) && !consumerConfiguration.getKeyDeserializer().isPresent()) {
final Optional<Argument<?>> keyArgument = Arrays.stream(method.getArguments()).filter(arg -> arg.isAnnotationPresent(KafkaKey.class)).findFirst();
if (keyArgument.isPresent()) {
consumerConfiguration.setKeyDeserializer(serdeRegistry.pickDeserializer(keyArgument.get()));
} else {
// noinspection SingleStatementInBlock
if (bodyArgument != null && ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
final Optional<Argument<?>> keyType = bodyArgument.getTypeVariable("K");
if (keyType.isPresent()) {
consumerConfiguration.setKeyDeserializer(serdeRegistry.pickDeserializer(keyType.get()));
} else {
consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
}
} else {
consumerConfiguration.setKeyDeserializer(new ByteArrayDeserializer());
}
}
}
// figure out the Value deserializer
if (!properties.containsKey(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG) && !consumerConfiguration.getValueDeserializer().isPresent()) {
if (bodyArgument == null) {
// noinspection SingleStatementInBlock
consumerConfiguration.setValueDeserializer(new StringDeserializer());
} else {
if (ConsumerRecord.class.isAssignableFrom(bodyArgument.getType())) {
final Optional<Argument<?>> valueType = bodyArgument.getTypeVariable("V");
if (valueType.isPresent()) {
consumerConfiguration.setValueDeserializer(serdeRegistry.pickDeserializer(valueType.get()));
} else {
consumerConfiguration.setValueDeserializer(new StringDeserializer());
}
} else {
final boolean batch = method.isTrue(KafkaListener.class, "batch");
consumerConfiguration.setValueDeserializer(serdeRegistry.pickDeserializer(batch ? getComponentType(bodyArgument) : bodyArgument));
}
}
}
debugDeserializationConfiguration(method, consumerConfiguration, properties);
}
Aggregations