Search in sources :

Example 1 with FunctionRegistration

use of org.springframework.cloud.function.context.FunctionRegistration 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 FunctionRegistration

use of org.springframework.cloud.function.context.FunctionRegistration in project spring-cloud-stream by spring-cloud.

the class ImplicitFunctionBindingTests method dynamicBindingTestWithFunctionRegistrationAndExplicitDestination.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void dynamicBindingTestWithFunctionRegistrationAndExplicitDestination() {
    try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration.getCompleteConfiguration(EmptyConfiguration.class)).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.function-in-0.destination=input")) {
        InputDestination input = context.getBean(InputDestination.class);
        try {
            input.send(new GenericMessage<byte[]>("hello".getBytes()));
            // it should since there are no functions and no bindings
            fail();
        } catch (Exception e) {
        // good, we expected it
        }
        Function<byte[], byte[]> function = v -> v;
        FunctionRegistration functionRegistration = new FunctionRegistration(function, "function");
        functionRegistration = functionRegistration.type(ResolvableType.forClassWithGenerics(Function.class, Object.class, Object.class).getType());
        FunctionBindingTestUtils.bind(context, functionRegistration);
        input.send(new GenericMessage<byte[]>("hello".getBytes()), "input");
        OutputDestination output = context.getBean(OutputDestination.class);
        assertThat(output.receive(1000, "function-out-0").getPayload()).isEqualTo("hello".getBytes());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Arrays(java.util.Arrays) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) FunctionBindingTestUtils(org.springframework.cloud.stream.binder.test.FunctionBindingTestUtils) BindingCreatedEvent(org.springframework.cloud.stream.binder.BindingCreatedEvent) DirectWithAttributesChannel(org.springframework.cloud.stream.messaging.DirectWithAttributesChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Disabled(org.junit.jupiter.api.Disabled) ContextFunctionCatalogAutoConfiguration(org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration) LoggingHandler(org.springframework.integration.handler.LoggingHandler) FunctionRegistration(org.springframework.cloud.function.context.FunctionRegistration) Duration(java.time.Duration) Map(java.util.Map) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Assert.fail(org.junit.Assert.fail) ResolvableType(org.springframework.core.ResolvableType) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Collection(java.util.Collection) ApplicationListener(org.springframework.context.ApplicationListener) MessageChannel(org.springframework.messaging.MessageChannel) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Test(org.junit.jupiter.api.Test) List(java.util.List) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) TestChannelBinderConfiguration(org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration) FunctionAroundWrapper(org.springframework.cloud.function.context.catalog.FunctionAroundWrapper) FunctionInvocationWrapper(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper) BindingsLifecycleController(org.springframework.cloud.stream.binding.BindingsLifecycleController) QueueChannel(org.springframework.integration.channel.QueueChannel) KafkaNull(org.springframework.kafka.support.KafkaNull) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Function(java.util.function.Function) Supplier(java.util.function.Supplier) State(org.springframework.cloud.stream.binding.BindingsLifecycleController.State) ArrayList(java.util.ArrayList) MessageBuilder(org.springframework.integration.support.MessageBuilder) Binding(org.springframework.cloud.stream.binder.Binding) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) BeanCreationException(org.springframework.beans.factory.BeanCreationException) EmitterProcessor(reactor.core.publisher.EmitterProcessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) Mono(reactor.core.publisher.Mono) MessageHeaders(org.springframework.messaging.MessageHeaders) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) WebApplicationType(org.springframework.boot.WebApplicationType) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Collections(java.util.Collections) Function(java.util.function.Function) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) FunctionRegistration(org.springframework.cloud.function.context.FunctionRegistration) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Test(org.junit.jupiter.api.Test)

Example 3 with FunctionRegistration

use of org.springframework.cloud.function.context.FunctionRegistration in project spring-cloud-stream by spring-cloud.

the class StreamBridge method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    if (this.initialized) {
        return;
    }
    FunctionRegistration<Function<Object, Object>> fr = new FunctionRegistration<>(v -> v, STREAM_BRIDGE_FUNC_NAME);
    fr.getProperties().put("singleton", "false");
    Type functionType = ResolvableType.forClassWithGenerics(Function.class, Object.class, Object.class).getType();
    this.functionRegistry.register(fr.type(functionType));
    Map<String, DirectWithAttributesChannel> channels = applicationContext.getBeansOfType(DirectWithAttributesChannel.class);
    for (Entry<String, DirectWithAttributesChannel> channelEntry : channels.entrySet()) {
        if (channelEntry.getValue().getAttribute("type").equals("output")) {
            this.channelCache.put(channelEntry.getKey(), channelEntry.getValue());
        }
    }
    this.initialized = true;
}
Also used : Function(java.util.function.Function) MimeType(org.springframework.util.MimeType) ResolvableType(org.springframework.core.ResolvableType) Type(java.lang.reflect.Type) DirectWithAttributesChannel(org.springframework.cloud.stream.messaging.DirectWithAttributesChannel) FunctionRegistration(org.springframework.cloud.function.context.FunctionRegistration)

Aggregations

Function (java.util.function.Function)3 FunctionRegistration (org.springframework.cloud.function.context.FunctionRegistration)3 Consumer (java.util.function.Consumer)2 Supplier (java.util.function.Supplier)2 FunctionInvocationWrapper (org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper)2 DirectWithAttributesChannel (org.springframework.cloud.stream.messaging.DirectWithAttributesChannel)2 ResolvableType (org.springframework.core.ResolvableType)2 Serializable (java.io.Serializable)1 Type (java.lang.reflect.Type)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assert.fail (org.junit.Assert.fail)1 AfterEach (org.junit.jupiter.api.AfterEach)1