Search in sources :

Example 6 with AnnotationMetadata

use of io.micronaut.core.annotation.AnnotationMetadata in project micronaut-kafka by micronaut-projects.

the class KafkaMessageHeaderBinder method bind.

@Override
public BindingResult<T> bind(ArgumentConversionContext<T> context, ConsumerRecord<?, ?> source) {
    Headers headers = source.headers();
    AnnotationMetadata annotationMetadata = context.getAnnotationMetadata();
    // use deprecated versions as that is what is stored in metadata
    String name = annotationMetadata.stringValue(MessageHeader.class, "name").orElseGet(() -> annotationMetadata.stringValue(MessageHeader.class).orElse(context.getArgument().getName()));
    Iterable<org.apache.kafka.common.header.Header> value = headers.headers(name);
    if (value.iterator().hasNext()) {
        Optional<T> converted = ConversionService.SHARED.convert(value, context);
        return () -> converted;
    } else if (context.getArgument().getType() == Optional.class) {
        // noinspection unchecked
        return () -> (Optional<T>) Optional.of(Optional.empty());
    } else {
        // noinspection unchecked
        return BindingResult.EMPTY;
    }
}
Also used : MessageHeader(io.micronaut.messaging.annotation.MessageHeader) Optional(java.util.Optional) Headers(org.apache.kafka.common.header.Headers) MessageHeader(io.micronaut.messaging.annotation.MessageHeader) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)6 Publishers (io.micronaut.core.async.publisher.Publishers)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)2 InterceptedMethod (io.micronaut.aop.InterceptedMethod)2 InterceptorBean (io.micronaut.aop.InterceptorBean)2 MethodInterceptor (io.micronaut.aop.MethodInterceptor)2 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)2 RequiresMetrics (io.micronaut.configuration.metrics.annotation.RequiresMetrics)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 Nullable (io.micronaut.core.annotation.Nullable)2 Argument (io.micronaut.core.type.Argument)2 CollectionUtils (io.micronaut.core.util.CollectionUtils)2 StringUtils (io.micronaut.core.util.StringUtils)2 ExecutableMethod (io.micronaut.inject.ExecutableMethod)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletionStage (java.util.concurrent.CompletionStage)2