Search in sources :

Example 1 with Output

use of org.springframework.cloud.stream.annotation.Output in project spring-cloud-function by spring-cloud.

the class BeanFactoryEnabledBindings method init.

private void init() {
    if (initialized.compareAndSet(false, true)) {
        String[] names = beanFactory.getBeanNamesForAnnotation(EnableBinding.class);
        for (String bean : names) {
            Class<?> type = beanFactory.getType(bean);
            MultiValueMap<String, Object> attrs = AnnotatedElementUtils.getAllAnnotationAttributes(type, EnableBinding.class.getName());
            List<Object> list = attrs.get("value");
            if (list != null) {
                for (Object object : list) {
                    Class<?>[] bindings = (Class<?>[]) object;
                    for (Class<?> binding : bindings) {
                        List<String> inputs = new ArrayList<>();
                        List<String> outputs = new ArrayList<>();
                        ReflectionUtils.doWithMethods(binding, method -> {
                            Input input = AnnotationUtils.findAnnotation(method, Input.class);
                            Output output = AnnotationUtils.findAnnotation(method, Output.class);
                            if (input != null) {
                                String name = BindingBeanDefinitionRegistryUtils.getBindingTargetName(input, method);
                                inputs.add(BeanFactoryEnabledBindings.this.binding.getBindingDestination(name));
                            }
                            if (output != null) {
                                String name = BindingBeanDefinitionRegistryUtils.getBindingTargetName(output, method);
                                outputs.add(BeanFactoryEnabledBindings.this.binding.getBindingDestination(name));
                            }
                        });
                        BeanFactoryEnabledBindings.this.outputs.addAll(outputs);
                        BeanFactoryEnabledBindings.this.inputs.addAll(inputs);
                        if (inputs.size() == 1 && outputs.size() == 1) {
                            BeanFactoryEnabledBindings.this.outputsToInputs.put(outputs.get(0), inputs.get(0));
                        }
                    }
                }
            }
        }
    }
}
Also used : EnableBinding(org.springframework.cloud.stream.annotation.EnableBinding) ArrayList(java.util.ArrayList) Input(org.springframework.cloud.stream.annotation.Input) Output(org.springframework.cloud.stream.annotation.Output)

Example 2 with Output

use of org.springframework.cloud.stream.annotation.Output in project spring-cloud-stream by spring-cloud.

the class StreamListenerMethodUtils method getOutboundBindingTargetName.

protected static String getOutboundBindingTargetName(Method method) {
    SendTo sendTo = AnnotationUtils.findAnnotation(method, SendTo.class);
    if (sendTo != null) {
        Assert.isTrue(!ObjectUtils.isEmpty(sendTo.value()), StreamListenerErrorMessages.ATLEAST_ONE_OUTPUT);
        Assert.isTrue(sendTo.value().length == 1, StreamListenerErrorMessages.SEND_TO_MULTIPLE_DESTINATIONS);
        Assert.hasText(sendTo.value()[0], StreamListenerErrorMessages.SEND_TO_EMPTY_DESTINATION);
        return sendTo.value()[0];
    }
    Output output = AnnotationUtils.findAnnotation(method, Output.class);
    if (output != null) {
        Assert.isTrue(StringUtils.hasText(output.value()), StreamListenerErrorMessages.ATLEAST_ONE_OUTPUT);
        return output.value();
    }
    return null;
}
Also used : SendTo(org.springframework.messaging.handler.annotation.SendTo) Output(org.springframework.cloud.stream.annotation.Output)

Example 3 with Output

use of org.springframework.cloud.stream.annotation.Output in project spring-cloud-stream by spring-cloud.

the class BindingBeanDefinitionRegistryUtils method registerBindingTargetBeanDefinitions.

public static void registerBindingTargetBeanDefinitions(Class<?> type, final String bindingTargetInterfaceBeanName, final BeanDefinitionRegistry registry) {
    ReflectionUtils.doWithMethods(type, method -> {
        Input input = AnnotationUtils.findAnnotation(method, Input.class);
        if (input != null) {
            String name = getBindingTargetName(input, method);
            registerInputBindingTargetBeanDefinition(input.value(), name, bindingTargetInterfaceBeanName, method.getName(), registry);
        }
        Output output = AnnotationUtils.findAnnotation(method, Output.class);
        if (output != null) {
            String name = getBindingTargetName(output, method);
            registerOutputBindingTargetBeanDefinition(output.value(), name, bindingTargetInterfaceBeanName, method.getName(), registry);
        }
    });
}
Also used : Input(org.springframework.cloud.stream.annotation.Input) Output(org.springframework.cloud.stream.annotation.Output)

Example 4 with Output

use of org.springframework.cloud.stream.annotation.Output in project spring-cloud-stream by spring-cloud.

the class StreamAnnotationCommonMethodUtils method getOutboundBindingTargetName.

public static String getOutboundBindingTargetName(Method method) {
    SendTo sendTo = AnnotationUtils.findAnnotation(method, SendTo.class);
    if (sendTo != null) {
        Assert.isTrue(!ObjectUtils.isEmpty(sendTo.value()), StreamAnnotationErrorMessages.ATLEAST_ONE_OUTPUT);
        Assert.isTrue(sendTo.value().length == 1, StreamAnnotationErrorMessages.SEND_TO_MULTIPLE_DESTINATIONS);
        Assert.hasText(sendTo.value()[0], StreamAnnotationErrorMessages.SEND_TO_EMPTY_DESTINATION);
        return sendTo.value()[0];
    }
    Output output = AnnotationUtils.findAnnotation(method, Output.class);
    if (output != null) {
        Assert.isTrue(StringUtils.hasText(output.value()), StreamAnnotationErrorMessages.ATLEAST_ONE_OUTPUT);
        return output.value();
    }
    return null;
}
Also used : SendTo(org.springframework.messaging.handler.annotation.SendTo) Output(org.springframework.cloud.stream.annotation.Output)

Example 5 with Output

use of org.springframework.cloud.stream.annotation.Output in project spring-cloud-stream by spring-cloud.

the class BindableProxyFactory method invoke.

@Override
public synchronized Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    // try to use cached target
    Object boundTarget = targetCache.get(method);
    if (boundTarget != null) {
        return boundTarget;
    }
    Input input = AnnotationUtils.findAnnotation(method, Input.class);
    if (input != null) {
        String name = BindingBeanDefinitionRegistryUtils.getBindingTargetName(input, method);
        boundTarget = this.inputHolders.get(name).getBoundTarget();
        targetCache.put(method, boundTarget);
        return boundTarget;
    } else {
        Output output = AnnotationUtils.findAnnotation(method, Output.class);
        if (output != null) {
            String name = BindingBeanDefinitionRegistryUtils.getBindingTargetName(output, method);
            boundTarget = this.outputHolders.get(name).getBoundTarget();
            targetCache.put(method, boundTarget);
            return boundTarget;
        }
    }
    return null;
}
Also used : Input(org.springframework.cloud.stream.annotation.Input) Output(org.springframework.cloud.stream.annotation.Output) Method(java.lang.reflect.Method)

Aggregations

Output (org.springframework.cloud.stream.annotation.Output)5 Input (org.springframework.cloud.stream.annotation.Input)3 SendTo (org.springframework.messaging.handler.annotation.SendTo)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 EnableBinding (org.springframework.cloud.stream.annotation.EnableBinding)1