Search in sources :

Example 1 with Merge

use of io.smallrye.reactive.messaging.annotations.Merge in project quarkus by quarkusio.

the class QuarkusMediatorConfigurationUtil method create.

public static QuarkusMediatorConfiguration create(MethodInfo methodInfo, boolean isSuspendMethod, BeanInfo bean, RecorderContext recorderContext, ClassLoader cl, boolean strict) {
    Class[] parameterTypeClasses;
    Class<?> returnTypeClass;
    MediatorConfigurationSupport.GenericTypeAssignable genericReturnTypeAssignable;
    if (isSuspendMethod) {
        parameterTypeClasses = new Class[methodInfo.parameters().size() - 1];
        for (int i = 0; i < methodInfo.parameters().size() - 1; i++) {
            parameterTypeClasses[i] = load(methodInfo.parameters().get(i).name().toString(), cl);
        }
        // the generated invoker will always return a CompletionStage
        // TODO: avoid hard coding this and use an SPI to communicate the info with the invoker generation code
        returnTypeClass = CompletionStage.class;
        genericReturnTypeAssignable = new JandexGenericTypeAssignable(determineReturnTypeOfSuspendMethod(methodInfo), cl);
    } else {
        parameterTypeClasses = new Class[methodInfo.parameters().size()];
        for (int i = 0; i < methodInfo.parameters().size(); i++) {
            parameterTypeClasses[i] = load(methodInfo.parameters().get(i).name().toString(), cl);
        }
        returnTypeClass = load(methodInfo.returnType().name().toString(), cl);
        genericReturnTypeAssignable = new ReturnTypeGenericTypeAssignable(methodInfo, cl);
    }
    QuarkusMediatorConfiguration configuration = new QuarkusMediatorConfiguration();
    MediatorConfigurationSupport mediatorConfigurationSupport = new MediatorConfigurationSupport(fullMethodName(methodInfo), returnTypeClass, parameterTypeClasses, genericReturnTypeAssignable, methodInfo.parameters().isEmpty() ? new AlwaysInvalidIndexGenericTypeAssignable() : new MethodParamGenericTypeAssignable(methodInfo, 0, cl));
    if (strict) {
        mediatorConfigurationSupport.strict();
    }
    configuration.setBeanId(bean.getIdentifier());
    configuration.setMethodName(methodInfo.name());
    String returnTypeName = returnTypeClass.getName();
    configuration.setReturnType(recorderContext.classProxy(returnTypeName));
    Class<?>[] parameterTypes = new Class[methodInfo.parameters().size()];
    for (int i = 0; i < methodInfo.parameters().size(); i++) {
        parameterTypes[i] = recorderContext.classProxy(methodInfo.parameters().get(i).name().toString());
    }
    configuration.setParameterTypes(parameterTypes);
    // We need to extract the value of @Incoming and @Incomings (which contains an array of @Incoming)
    List<String> incomingValues = new ArrayList<>(getValues(methodInfo, INCOMING));
    incomingValues.addAll(getIncomingValues(methodInfo));
    configuration.setIncomings(incomingValues);
    String outgoingValue = getValue(methodInfo, OUTGOING);
    configuration.setOutgoing(outgoingValue);
    Shape shape = mediatorConfigurationSupport.determineShape(incomingValues, outgoingValue);
    configuration.setShape(shape);
    Acknowledgment.Strategy acknowledgment = mediatorConfigurationSupport.processSuppliedAcknowledgement(incomingValues, () -> {
        AnnotationInstance instance = methodInfo.annotation(ACKNOWLEDGMENT);
        if (instance != null) {
            return Acknowledgment.Strategy.valueOf(instance.value().asEnum());
        }
        return null;
    });
    configuration.setAcknowledgment(acknowledgment);
    MediatorConfigurationSupport.ValidationOutput validationOutput = mediatorConfigurationSupport.validate(shape, acknowledgment);
    configuration.setProduction(validationOutput.getProduction());
    configuration.setConsumption(validationOutput.getConsumption());
    configuration.setIngestedPayloadType(validationOutput.getIngestedPayloadType());
    if (validationOutput.getUseBuilderTypes()) {
        configuration.setUseBuilderTypes(validationOutput.getUseBuilderTypes());
    } else {
        configuration.setUseBuilderTypes(false);
    }
    if (acknowledgment == null) {
        acknowledgment = mediatorConfigurationSupport.processDefaultAcknowledgement(shape, validationOutput.getConsumption(), validationOutput.getProduction());
        configuration.setAcknowledgment(acknowledgment);
    }
    configuration.setMerge(mediatorConfigurationSupport.processMerge(incomingValues, new Supplier<Merge.Mode>() {

        @Override
        public Merge.Mode get() {
            AnnotationInstance instance = methodInfo.annotation(MERGE);
            if (instance != null) {
                AnnotationValue value = instance.value();
                if (value == null) {
                    // the default value of @Merge
                    return Merge.Mode.MERGE;
                }
                return Merge.Mode.valueOf(value.asEnum());
            }
            return null;
        }
    }));
    configuration.setBroadcastValue(mediatorConfigurationSupport.processBroadcast(outgoingValue, new Supplier<Integer>() {

        @Override
        public Integer get() {
            AnnotationInstance instance = methodInfo.annotation(BROADCAST);
            if (instance != null) {
                AnnotationValue value = instance.value();
                if (value == null) {
                    // the default value of @Broadcast
                    return 0;
                }
                return value.asInt();
            }
            return null;
        }
    }));
    AnnotationInstance blockingAnnotation = methodInfo.annotation(BLOCKING);
    AnnotationInstance smallryeBlockingAnnotation = methodInfo.annotation(SMALLRYE_BLOCKING);
    AnnotationInstance transactionalAnnotation = methodInfo.annotation(TRANSACTIONAL);
    if (blockingAnnotation != null || smallryeBlockingAnnotation != null || transactionalAnnotation != null) {
        mediatorConfigurationSupport.validateBlocking(validationOutput);
        configuration.setBlocking(true);
        if (blockingAnnotation != null) {
            AnnotationValue ordered = blockingAnnotation.value("ordered");
            configuration.setBlockingExecutionOrdered(ordered == null || ordered.asBoolean());
            String poolName;
            if (blockingAnnotation.value() != null && !(poolName = blockingAnnotation.value().asString()).equals(Blocking.DEFAULT_WORKER_POOL)) {
                configuration.setWorkerPoolName(poolName);
            }
        } else {
            configuration.setBlockingExecutionOrdered(true);
        }
    }
    return configuration;
}
Also used : MediatorConfigurationSupport(io.smallrye.reactive.messaging.providers.MediatorConfigurationSupport) Shape(io.smallrye.reactive.messaging.Shape) ArrayList(java.util.ArrayList) QuarkusMediatorConfiguration(io.quarkus.smallrye.reactivemessaging.runtime.QuarkusMediatorConfiguration) Merge(io.smallrye.reactive.messaging.annotations.Merge) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment) AnnotationValue(org.jboss.jandex.AnnotationValue) Supplier(java.util.function.Supplier) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 2 with Merge

use of io.smallrye.reactive.messaging.annotations.Merge in project smallrye-reactive-messaging by smallrye.

the class DefaultMediatorConfiguration method compute.

public void compute(List<Incoming> incomings, Outgoing outgoing, Blocking blocking) {
    if (incomings != null) {
        for (Incoming incoming : incomings) {
            if (Validation.isBlank(incoming.value())) {
                throw ex.illegalArgumentForAnnotationNullOrBlank("@Incoming", methodAsString());
            }
        }
    } else {
        incomings = Collections.emptyList();
    }
    if (outgoing != null && Validation.isBlank(outgoing.value())) {
        throw ex.illegalArgumentForAnnotationNullOrBlank("@Outgoing", methodAsString());
    }
    this.shape = this.mediatorConfigurationSupport.determineShape(incomings, outgoing);
    this.acknowledgment = this.mediatorConfigurationSupport.processSuppliedAcknowledgement(incomings, () -> {
        Acknowledgment annotation = method.getAnnotation(Acknowledgment.class);
        return annotation != null ? annotation.value() : null;
    });
    if (!incomings.isEmpty()) {
        this.incomingValues = incomings.stream().map(Incoming::value).collect(Collectors.toList());
    }
    if (outgoing != null) {
        this.outgoingValue = outgoing.value();
    }
    if (blocking != null) {
        this.isBlocking = true;
        this.isOrderedExecution = blocking.ordered();
        if (!blocking.value().equals(Blocking.DEFAULT_WORKER_POOL)) {
            this.workerPoolName = blocking.value();
        }
    }
    MediatorConfigurationSupport.ValidationOutput validationOutput = this.mediatorConfigurationSupport.validate(this.shape, this.acknowledgment);
    this.production = validationOutput.getProduction();
    this.consumption = validationOutput.getConsumption();
    if (validationOutput.getUseBuilderTypes()) {
        this.useBuilderTypes = validationOutput.getUseBuilderTypes();
    }
    if (this.acknowledgment == null) {
        this.acknowledgment = this.mediatorConfigurationSupport.processDefaultAcknowledgement(this.shape, this.consumption, this.production);
    }
    this.mergePolicy = this.mediatorConfigurationSupport.processMerge(incomings, () -> {
        Merge annotation = method.getAnnotation(Merge.class);
        return annotation != null ? annotation.value() : null;
    });
    this.broadcastValue = this.mediatorConfigurationSupport.processBroadcast(outgoing, () -> {
        Broadcast annotation = method.getAnnotation(Broadcast.class);
        return annotation != null ? annotation.value() : null;
    });
    if (this.isBlocking) {
        this.mediatorConfigurationSupport.validateBlocking(validationOutput);
    }
    ingestedPayloadType = validationOutput.getIngestedPayloadType();
}
Also used : Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) Merge(io.smallrye.reactive.messaging.annotations.Merge) Broadcast(io.smallrye.reactive.messaging.annotations.Broadcast) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment)

Aggregations

Merge (io.smallrye.reactive.messaging.annotations.Merge)2 Acknowledgment (org.eclipse.microprofile.reactive.messaging.Acknowledgment)2 QuarkusMediatorConfiguration (io.quarkus.smallrye.reactivemessaging.runtime.QuarkusMediatorConfiguration)1 Shape (io.smallrye.reactive.messaging.Shape)1 Broadcast (io.smallrye.reactive.messaging.annotations.Broadcast)1 MediatorConfigurationSupport (io.smallrye.reactive.messaging.providers.MediatorConfigurationSupport)1 ArrayList (java.util.ArrayList)1 Supplier (java.util.function.Supplier)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1