Search in sources :

Example 1 with FunctionInvocationWrapper

use of org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper in project spring-cloud-stream by spring-cloud.

the class FunctionBindingTestUtils method bind.

@SuppressWarnings("rawtypes")
public static void bind(ConfigurableApplicationContext applicationContext, Object function) {
    try {
        Object targetFunction = function;
        if (function instanceof FunctionRegistration) {
            targetFunction = ((FunctionRegistration) function).getTarget();
        }
        String functionName = targetFunction instanceof Function ? "function" : (targetFunction instanceof Consumer ? "consumer" : "supplier");
        System.setProperty("spring.cloud.function.definition", functionName);
        applicationContext.getBeanFactory().registerSingleton(functionName, function);
        Object actualFunction = ((FunctionInvocationWrapper) applicationContext.getBean(FunctionCatalog.class).lookup(functionName)).getTarget();
        InitializingBean functionBindingRegistrar = applicationContext.getBean("functionBindingRegistrar", InitializingBean.class);
        functionBindingRegistrar.afterPropertiesSet();
        BindableProxyFactory bindingProxy = applicationContext.getBean("&" + functionName + "_binding", BindableProxyFactory.class);
        bindingProxy.afterPropertiesSet();
        InitializingBean functionBinder = applicationContext.getBean("functionInitializer", InitializingBean.class);
        functionBinder.afterPropertiesSet();
        BindingServiceProperties bindingProperties = applicationContext.getBean(BindingServiceProperties.class);
        String inputBindingName = functionName + "-in-0";
        String outputBindingName = functionName + "-out-0";
        Map<String, BindingProperties> bindings = bindingProperties.getBindings();
        BindingProperties inputProperties = bindings.get(inputBindingName);
        BindingProperties outputProperties = bindings.get(outputBindingName);
        ConsumerProperties consumerProperties = inputProperties.getConsumer();
        ProducerProperties producerProperties = outputProperties.getProducer();
        TestChannelBinder binder = applicationContext.getBean(TestChannelBinder.class);
        if (actualFunction instanceof Supplier || actualFunction instanceof Function) {
            Binding<MessageChannel> bindProducer = binder.bindProducer(outputProperties.getDestination(), applicationContext.getBean(outputBindingName, MessageChannel.class), producerProperties == null ? new ProducerProperties() : producerProperties);
            bindProducer.start();
        }
        if (actualFunction instanceof Consumer || actualFunction instanceof Function) {
            Binding<MessageChannel> bindConsumer = binder.bindConsumer(inputProperties.getDestination(), null, applicationContext.getBean(inputBindingName, MessageChannel.class), consumerProperties == null ? new ConsumerProperties() : consumerProperties);
            bindConsumer.start();
        }
    } catch (Exception e) {
        throw new IllegalStateException("Failed to bind function", e);
    } finally {
        System.clearProperty("spring.cloud.function.definition");
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) FunctionRegistration(org.springframework.cloud.function.context.FunctionRegistration) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) Function(java.util.function.Function) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) Consumer(java.util.function.Consumer) MessageChannel(org.springframework.messaging.MessageChannel) BindableProxyFactory(org.springframework.cloud.stream.binding.BindableProxyFactory) Supplier(java.util.function.Supplier) InitializingBean(org.springframework.beans.factory.InitializingBean) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties)

Example 2 with FunctionInvocationWrapper

use of org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper in project spring-cloud-stream by spring-cloud.

the class FunctionConfiguration method integrationFlowFromProvidedSupplier.

@SuppressWarnings({ "rawtypes", "unchecked" })
private IntegrationFlowBuilder integrationFlowFromProvidedSupplier(Supplier<?> supplier, Publisher<Object> beginPublishingTrigger, PollableBean pollable, GenericApplicationContext context, TaskScheduler taskScheduler, ProducerProperties producerProperties, String bindingName) {
    IntegrationFlowBuilder integrationFlowBuilder;
    boolean splittable = pollable != null && (boolean) AnnotationUtils.getAnnotationAttributes(pollable).get("splittable");
    FunctionInvocationWrapper function = (supplier instanceof PartitionAwareFunctionWrapper) ? (FunctionInvocationWrapper) ((PartitionAwareFunctionWrapper) supplier).function : (FunctionInvocationWrapper) supplier;
    boolean reactive = FunctionTypeUtils.isPublisher(function.getOutputType());
    if (pollable == null && reactive) {
        Publisher publisher = (Publisher) supplier.get();
        publisher = publisher instanceof Mono ? ((Mono) publisher).delaySubscription(beginPublishingTrigger).map(this::wrapToMessageIfNecessary) : ((Flux) publisher).delaySubscription(beginPublishingTrigger).map(this::wrapToMessageIfNecessary);
        integrationFlowBuilder = IntegrationFlows.from(publisher);
        // see https://github.com/spring-cloud/spring-cloud-stream/issues/1863 for details about the following code
        // will keep AC alive
        taskScheduler.schedule(() -> {
        }, Instant.now());
    } else {
        // implies pollable
        boolean autoStartup = producerProperties != null ? producerProperties.isAutoStartup() : true;
        integrationFlowBuilder = IntegrationFlows.fromSupplier(supplier, spca -> spca.id(bindingName + "_spca").autoStartup(autoStartup));
        // only apply the PollableBean attributes if this is a reactive function.
        if (splittable && reactive) {
            integrationFlowBuilder = integrationFlowBuilder.split();
        }
    }
    return integrationFlowBuilder;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) BindingCreatedEvent(org.springframework.cloud.stream.binder.BindingCreatedEvent) DirectWithAttributesChannel(org.springframework.cloud.stream.messaging.DirectWithAttributesChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) IntegrationFlowBuilder(org.springframework.integration.dsl.IntegrationFlowBuilder) BindingServiceConfiguration(org.springframework.cloud.stream.config.BindingServiceConfiguration) Tuples(reactor.util.function.Tuples) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ContextFunctionCatalogAutoConfiguration(org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) RoutingFunction(org.springframework.cloud.function.context.config.RoutingFunction) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageUtils(org.springframework.cloud.function.context.message.MessageUtils) Method(java.lang.reflect.Method) ClassUtils(org.springframework.util.ClassUtils) MethodMetadata(org.springframework.core.type.MethodMetadata) AbstractSubscribableChannel(org.springframework.integration.channel.AbstractSubscribableChannel) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) TaskScheduler(org.springframework.scheduling.TaskScheduler) Instant(java.time.Instant) MessageChannel(org.springframework.messaging.MessageChannel) SupportedBindableFeatures(org.springframework.cloud.stream.binding.SupportedBindableFeatures) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Type(java.lang.reflect.Type) Environment(org.springframework.core.env.Environment) CollectionUtils(org.springframework.util.CollectionUtils) LogFactory(org.apache.commons.logging.LogFactory) FunctionCatalog(org.springframework.cloud.function.context.FunctionCatalog) IntegrationReactiveUtils(org.springframework.integration.util.IntegrationReactiveUtils) CloudEventMessageUtils(org.springframework.cloud.function.cloudevent.CloudEventMessageUtils) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ApplicationContextAware(org.springframework.context.ApplicationContextAware) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessagingException(org.springframework.messaging.MessagingException) BinderFactory(org.springframework.cloud.stream.binder.BinderFactory) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MonoSink(reactor.core.publisher.MonoSink) PollableBean(org.springframework.cloud.function.context.PollableBean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) MessageBuilder(org.springframework.integration.support.MessageBuilder) NewDestinationBindingCallback(org.springframework.cloud.stream.binding.NewDestinationBindingCallback) BinderFactoryAutoConfiguration(org.springframework.cloud.stream.config.BinderFactoryAutoConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) FunctionTypeUtils(org.springframework.cloud.function.context.catalog.FunctionTypeUtils) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) BindableProxyFactory(org.springframework.cloud.stream.binding.BindableProxyFactory) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ObjectUtils(org.springframework.util.ObjectUtils) Import(org.springframework.context.annotation.Import) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) Field(java.lang.reflect.Field) FunctionProperties(org.springframework.cloud.function.context.FunctionProperties) ApplicationContext(org.springframework.context.ApplicationContext) MessageHeaders(org.springframework.messaging.MessageHeaders) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Flux(reactor.core.publisher.Flux) ParameterizedType(java.lang.reflect.ParameterizedType) FunctionContextUtils(org.springframework.cloud.function.context.config.FunctionContextUtils) EnvironmentAware(org.springframework.context.EnvironmentAware) ReflectionUtils(org.springframework.util.ReflectionUtils) Log(org.apache.commons.logging.Log) FunctionRegistry(org.springframework.cloud.function.context.FunctionRegistry) Bean(org.springframework.context.annotation.Bean) AutoConfigureBefore(org.springframework.boot.autoconfigure.AutoConfigureBefore) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) Mono(reactor.core.publisher.Mono) Publisher(org.reactivestreams.Publisher) IntegrationFlowBuilder(org.springframework.integration.dsl.IntegrationFlowBuilder)

Example 3 with FunctionInvocationWrapper

use of org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper in project spring-cloud-stream by spring-cloud.

the class FunctionConfiguration method supplierInitializer.

/*
	 * Binding initializer responsible only for Suppliers
	 */
@Bean
InitializingBean supplierInitializer(FunctionCatalog functionCatalog, StreamFunctionProperties functionProperties, GenericApplicationContext context, BindingServiceProperties serviceProperties, @Nullable List<BindableFunctionProxyFactory> proxyFactories, StreamBridge streamBridge, TaskScheduler taskScheduler) {
    if (CollectionUtils.isEmpty(proxyFactories)) {
        return null;
    }
    return new InitializingBean() {

        @SuppressWarnings("rawtypes")
        @Override
        public void afterPropertiesSet() throws Exception {
            for (BindableFunctionProxyFactory proxyFactory : proxyFactories) {
                FunctionInvocationWrapper functionWrapper = functionCatalog.lookup(proxyFactory.getFunctionDefinition());
                if (functionWrapper != null && functionWrapper.isSupplier()) {
                    // gather output content types
                    List<String> contentTypes = new ArrayList<String>();
                    if (proxyFactory.getOutputs().size() == 0) {
                        return;
                    }
                    Assert.isTrue(proxyFactory.getOutputs().size() == 1, "Supplier with multiple outputs is not supported at the moment.");
                    String outputName = proxyFactory.getOutputs().iterator().next();
                    BindingProperties bindingProperties = serviceProperties.getBindingProperties(outputName);
                    ProducerProperties producerProperties = bindingProperties.getProducer();
                    if (!(bindingProperties.getProducer() != null && producerProperties.isUseNativeEncoding())) {
                        contentTypes.add(bindingProperties.getContentType());
                    }
                    // see https://github.com/spring-cloud/spring-cloud-stream/issues/2027
                    String functionDefinition = proxyFactory.getFunctionDefinition();
                    String[] functionNames = StringUtils.delimitedListToStringArray(functionDefinition.replaceAll(",", "|").trim(), "|");
                    Function supplier = null;
                    Function function = null;
                    if (!ObjectUtils.isEmpty(functionNames) && functionNames.length > 1) {
                        String supplierName = functionNames[0];
                        String remainingFunctionDefinition = StringUtils.arrayToCommaDelimitedString(Arrays.copyOfRange(functionNames, 1, functionNames.length));
                        supplier = functionCatalog.lookup(supplierName);
                        function = functionCatalog.lookup(remainingFunctionDefinition, contentTypes.toArray(new String[0]));
                        if (!((FunctionInvocationWrapper) supplier).isOutputTypePublisher() && ((FunctionInvocationWrapper) function).isInputTypePublisher()) {
                            functionWrapper = null;
                        } else {
                            functionWrapper = functionCatalog.lookup(proxyFactory.getFunctionDefinition(), contentTypes.toArray(new String[0]));
                        }
                    } else {
                        functionWrapper = functionCatalog.lookup(proxyFactory.getFunctionDefinition(), contentTypes.toArray(new String[0]));
                    }
                    Publisher<Object> beginPublishingTrigger = setupBindingTrigger(context);
                    if (!functionProperties.isComposeFrom() && !functionProperties.isComposeTo()) {
                        String integrationFlowName = proxyFactory.getFunctionDefinition() + "_integrationflow";
                        PollableBean pollable = extractPollableAnnotation(functionProperties, context, proxyFactory);
                        if (functionWrapper != null) {
                            // Type functionType = functionWrapper.getFunctionType();
                            IntegrationFlow integrationFlow = integrationFlowFromProvidedSupplier(new PartitionAwareFunctionWrapper(functionWrapper, context, producerProperties), beginPublishingTrigger, pollable, context, taskScheduler, producerProperties, outputName).route(Message.class, message -> {
                                if (message.getHeaders().get("spring.cloud.stream.sendto.destination") != null) {
                                    String destinationName = (String) message.getHeaders().get("spring.cloud.stream.sendto.destination");
                                    return streamBridge.resolveDestination(destinationName, producerProperties, null);
                                }
                                return outputName;
                            }).get();
                            IntegrationFlow postProcessedFlow = (IntegrationFlow) context.getAutowireCapableBeanFactory().applyBeanPostProcessorsBeforeInitialization(integrationFlow, integrationFlowName);
                            context.registerBean(integrationFlowName, IntegrationFlow.class, () -> postProcessedFlow);
                        } else {
                            // Type functionType = ((FunctionInvocationWrapper) supplier).getFunctionType();
                            IntegrationFlow integrationFlow = integrationFlowFromProvidedSupplier(new PartitionAwareFunctionWrapper(supplier, context, producerProperties), beginPublishingTrigger, pollable, context, taskScheduler, producerProperties, outputName).channel(c -> c.direct()).fluxTransform((Function<? super Flux<Message<Object>>, ? extends Publisher<Object>>) function).route(Message.class, message -> {
                                if (message.getHeaders().get("spring.cloud.stream.sendto.destination") != null) {
                                    String destinationName = (String) message.getHeaders().get("spring.cloud.stream.sendto.destination");
                                    return streamBridge.resolveDestination(destinationName, producerProperties, null);
                                }
                                return outputName;
                            }).get();
                            IntegrationFlow postProcessedFlow = (IntegrationFlow) context.getAutowireCapableBeanFactory().applyBeanPostProcessorsBeforeInitialization(integrationFlow, integrationFlowName);
                            context.registerBean(integrationFlowName, IntegrationFlow.class, () -> postProcessedFlow);
                        }
                    }
                }
            }
        }
    };
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) BindingCreatedEvent(org.springframework.cloud.stream.binder.BindingCreatedEvent) DirectWithAttributesChannel(org.springframework.cloud.stream.messaging.DirectWithAttributesChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) IntegrationFlowBuilder(org.springframework.integration.dsl.IntegrationFlowBuilder) BindingServiceConfiguration(org.springframework.cloud.stream.config.BindingServiceConfiguration) Tuples(reactor.util.function.Tuples) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ContextFunctionCatalogAutoConfiguration(org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) RoutingFunction(org.springframework.cloud.function.context.config.RoutingFunction) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageUtils(org.springframework.cloud.function.context.message.MessageUtils) Method(java.lang.reflect.Method) ClassUtils(org.springframework.util.ClassUtils) MethodMetadata(org.springframework.core.type.MethodMetadata) AbstractSubscribableChannel(org.springframework.integration.channel.AbstractSubscribableChannel) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) TaskScheduler(org.springframework.scheduling.TaskScheduler) Instant(java.time.Instant) MessageChannel(org.springframework.messaging.MessageChannel) SupportedBindableFeatures(org.springframework.cloud.stream.binding.SupportedBindableFeatures) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Type(java.lang.reflect.Type) Environment(org.springframework.core.env.Environment) CollectionUtils(org.springframework.util.CollectionUtils) LogFactory(org.apache.commons.logging.LogFactory) FunctionCatalog(org.springframework.cloud.function.context.FunctionCatalog) IntegrationReactiveUtils(org.springframework.integration.util.IntegrationReactiveUtils) CloudEventMessageUtils(org.springframework.cloud.function.cloudevent.CloudEventMessageUtils) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ApplicationContextAware(org.springframework.context.ApplicationContextAware) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessagingException(org.springframework.messaging.MessagingException) BinderFactory(org.springframework.cloud.stream.binder.BinderFactory) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MonoSink(reactor.core.publisher.MonoSink) PollableBean(org.springframework.cloud.function.context.PollableBean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) MessageBuilder(org.springframework.integration.support.MessageBuilder) NewDestinationBindingCallback(org.springframework.cloud.stream.binding.NewDestinationBindingCallback) BinderFactoryAutoConfiguration(org.springframework.cloud.stream.config.BinderFactoryAutoConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) FunctionTypeUtils(org.springframework.cloud.function.context.catalog.FunctionTypeUtils) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) BindableProxyFactory(org.springframework.cloud.stream.binding.BindableProxyFactory) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ObjectUtils(org.springframework.util.ObjectUtils) Import(org.springframework.context.annotation.Import) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) Field(java.lang.reflect.Field) FunctionProperties(org.springframework.cloud.function.context.FunctionProperties) ApplicationContext(org.springframework.context.ApplicationContext) MessageHeaders(org.springframework.messaging.MessageHeaders) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Flux(reactor.core.publisher.Flux) ParameterizedType(java.lang.reflect.ParameterizedType) FunctionContextUtils(org.springframework.cloud.function.context.config.FunctionContextUtils) EnvironmentAware(org.springframework.context.EnvironmentAware) ReflectionUtils(org.springframework.util.ReflectionUtils) Log(org.apache.commons.logging.Log) FunctionRegistry(org.springframework.cloud.function.context.FunctionRegistry) Bean(org.springframework.context.annotation.Bean) AutoConfigureBefore(org.springframework.boot.autoconfigure.AutoConfigureBefore) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) Message(org.springframework.messaging.Message) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArrayList(java.util.ArrayList) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RoutingFunction(org.springframework.cloud.function.context.config.RoutingFunction) Function(java.util.function.Function) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) InitializingBean(org.springframework.beans.factory.InitializingBean) PollableBean(org.springframework.cloud.function.context.PollableBean) PollableBean(org.springframework.cloud.function.context.PollableBean) InitializingBean(org.springframework.beans.factory.InitializingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with FunctionInvocationWrapper

use of org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper in project spring-cloud-stream by spring-cloud.

the class StreamBridgeTests method testNoCachingOfStreamBridgeFunction.

@SuppressWarnings("unchecked")
@Test
public void testNoCachingOfStreamBridgeFunction() throws Exception {
    try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration.getCompleteConfiguration(ConsumerConfiguration.class, InterceptorConfiguration.class)).web(WebApplicationType.NONE).run("--spring.cloud.function.definition=function", "--spring.jmx.enabled=false")) {
        StreamBridge bridge = context.getBean(StreamBridge.class);
        bridge.send("function-in-0", (Object) "hello foo", MimeTypeUtils.TEXT_PLAIN);
        bridge.send("function-in-0", (Object) "hello foo", MimeTypeUtils.APPLICATION_JSON);
        bridge.send("function-in-0", (Object) "hello foo", MimeTypeUtils.TEXT_HTML);
        Field field = ReflectionUtils.findField(StreamBridge.class, "streamBridgeFunctionCache");
        field.setAccessible(true);
        Map<String, FunctionInvocationWrapper> map = (Map<String, FunctionInvocationWrapper>) field.get(bridge);
        assertThat(map.size()).isEqualTo(3);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Field(java.lang.reflect.Field) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Map(java.util.Map) Test(org.junit.Test)

Example 5 with FunctionInvocationWrapper

use of org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper in project spring-cloud-stream by spring-cloud.

the class StreamBridge method getStreamBridgeFunction.

private synchronized FunctionInvocationWrapper getStreamBridgeFunction(String outputContentType, ProducerProperties producerProperties) {
    if (StringUtils.hasText(outputContentType) && this.streamBridgeFunctionCache.containsKey(outputContentType)) {
        return this.streamBridgeFunctionCache.get(outputContentType);
    } else {
        FunctionInvocationWrapper functionToInvoke = this.functionCatalog.lookup(STREAM_BRIDGE_FUNC_NAME, outputContentType.toString());
        this.streamBridgeFunctionCache.put(outputContentType, functionToInvoke);
        functionToInvoke.setSkipOutputConversion(producerProperties.isUseNativeEncoding());
        return functionToInvoke;
    }
}
Also used : FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper)

Aggregations

FunctionInvocationWrapper (org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper)5 Field (java.lang.reflect.Field)3 Map (java.util.Map)3 Function (java.util.function.Function)3 Supplier (java.util.function.Supplier)3 InitializingBean (org.springframework.beans.factory.InitializingBean)3 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)3 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)3 BindableProxyFactory (org.springframework.cloud.stream.binding.BindableProxyFactory)3 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)3 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)3 MessageChannel (org.springframework.messaging.MessageChannel)3 GenericArrayType (java.lang.reflect.GenericArrayType)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2