org.springframework.cloud.function.context.FunctionRegistration Java Examples
The following examples show how to use
org.springframework.cloud.function.context.FunctionRegistration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testReactiveFunctionMessages() { FunctionRegistration<ReactiveFunction> registration = new FunctionRegistration<>(new ReactiveFunction(), "reactive") .type(FunctionType.of(ReactiveFunction.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(registration); Function lookedUpFunction = catalog.lookup("reactive"); assertThat(lookedUpFunction).isNotNull(); Flux<List<String>> result = (Flux<List<String>>) lookedUpFunction .apply(Flux.just(MessageBuilder .withPayload("[{\"name\":\"item1\"},{\"name\":\"item2\"}]") .setHeader(MessageHeaders.CONTENT_TYPE, "application/json") .build() )); Assertions.assertIterableEquals(result.blockFirst(), Arrays.asList("item1", "item2")); }
Example #2
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void testFunctionComposition() { FunctionRegistration<UpperCase> upperCaseRegistration = new FunctionRegistration<>( new UpperCase(), "uppercase").type(FunctionType.of(UpperCase.class)); FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>( new Reverse(), "reverse").type(FunctionType.of(Reverse.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(upperCaseRegistration); catalog.register(reverseRegistration); Function<Flux<String>, Flux<String>> lookedUpFunction = catalog .lookup("uppercase|reverse"); assertThat(lookedUpFunction).isNotNull(); assertThat(lookedUpFunction.apply(Flux.just("star")).blockFirst()) .isEqualTo("RATS"); }
Example #3
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void testFunctionCompositionImplicit() { FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>( new Words(), "words").type(FunctionType.of(Words.class)); FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>( new Reverse(), "reverse").type(FunctionType.of(Reverse.class)); FunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(wordsRegistration); catalog.register(reverseRegistration); // There's only one function, we should be able to leave that blank Supplier<String> lookedUpFunction = catalog.lookup("words|"); assertThat(lookedUpFunction).isNotNull(); assertThat(lookedUpFunction.get()).isEqualTo("olleh"); }
Example #4
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test @Disabled public void testFunctionCompletelyImplicitComposition() { FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>( new Words(), "words").type(FunctionType.of(Words.class)); FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>( new Reverse(), "reverse").type(FunctionType.of(Reverse.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(wordsRegistration); catalog.register(reverseRegistration); // There's only one function, we should be able to leave that blank Supplier<Flux<String>> lookedUpFunction = catalog.lookup("|"); assertThat(lookedUpFunction).isNotNull(); assertThat(lookedUpFunction.get().blockFirst()).isEqualTo("olleh"); }
Example #5
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void testFunctionLookup() { TestFunction function = new TestFunction(); FunctionRegistration<TestFunction> registration = new FunctionRegistration<>( function, "foo").type(FunctionType.of(TestFunction.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(registration); FunctionInvocationWrapper lookedUpFunction = catalog.lookup("hello"); assertThat(lookedUpFunction).isNotNull(); // because we only have one and can look it up with any name FunctionRegistration<TestFunction> registration2 = new FunctionRegistration<>( function, "foo2").type(FunctionType.of(TestFunction.class)); catalog.register(registration2); lookedUpFunction = catalog.lookup("hello"); assertThat(lookedUpFunction).isNull(); }
Example #6
Source File: FunctionTypeUtils.java From spring-cloud-function with Apache License 2.0 | 6 votes |
private static void assertSupportedTypes(Type type) { if (type instanceof ParameterizedType) { type = ((ParameterizedType) type).getRawType(); Assert.isTrue(type instanceof Class<?>, "Must be one of Supplier, Function, Consumer" + " or FunctionRegistration. Was " + type); } Class<?> candidateType = (Class<?>) type; Assert.isTrue(Supplier.class.isAssignableFrom(candidateType) || Function.class.isAssignableFrom(candidateType) || Consumer.class.isAssignableFrom(candidateType) || FunctionRegistration.class.isAssignableFrom(candidateType) || type.getTypeName().startsWith("org.springframework.context.annotation.ConfigurationClassEnhancer"), "Must be one of Supplier, Function, Consumer" + " or FunctionRegistration. Was " + type); }
Example #7
Source File: KotlinLambdaToFunctionAutoConfiguration.java From spring-cloud-function with Apache License 2.0 | 6 votes |
/** * Will transform all discovered Kotlin's Function lambdas to java * Supplier, Function and Consumer, retaining the original Kotlin type * characteristics. * * @return the bean factory post processor */ @Bean public BeanFactoryPostProcessor kotlinToFunctionTransformer() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName); ResolvableType rt = beanDefinition.getResolvableType(); if (rt.getType().getTypeName().startsWith("kotlin.jvm.functions.Function")) { RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class); ConstructorArgumentValues ca = new ConstructorArgumentValues(); ca.addGenericArgumentValue(beanDefinition); cbd.setConstructorArgumentValues(ca); ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanDefinitionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, cbd); } } } }; }
Example #8
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void testFunctionCompositionWithMessages() { FunctionRegistration<UpperCaseMessage> upperCaseRegistration = new FunctionRegistration<>( new UpperCaseMessage(), "uppercase") .type(FunctionType.of(UpperCaseMessage.class)); FunctionRegistration<ReverseMessage> reverseRegistration = new FunctionRegistration<>( new ReverseMessage(), "reverse") .type(FunctionType.of(ReverseMessage.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(upperCaseRegistration); catalog.register(reverseRegistration); Function<Flux<Message<String>>, Flux<Message<String>>> lookedUpFunction = catalog .lookup("uppercase|reverse"); assertThat(lookedUpFunction).isNotNull(); assertThat(lookedUpFunction .apply(Flux.just(MessageBuilder.withPayload("star").build())).blockFirst() .getPayload()).isEqualTo("RATS"); }
Example #9
Source File: BeanFactoryAwareFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Override Object locateFunction(String name) { Object function = super.locateFunction(name); if (function == null) { try { function = BeanFactoryAnnotationUtils.qualifiedBeanOfType(this.applicationContext.getBeanFactory(), Object.class, name); } catch (Exception e) { // ignore } } if (function == null && this.applicationContext.containsBean(name)) { function = this.applicationContext.getBean(name); } if (function != null && this.notFunction(function.getClass()) && this.applicationContext .containsBean(name + FunctionRegistration.REGISTRATION_NAME_SUFFIX)) { // e.g., Kotlin lambdas function = this.applicationContext .getBean(name + FunctionRegistration.REGISTRATION_NAME_SUFFIX, FunctionRegistration.class); } return function; }
Example #10
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void testFunctionCompositionMixedMessages() { FunctionRegistration<UpperCaseMessage> upperCaseRegistration = new FunctionRegistration<>( new UpperCaseMessage(), "uppercase") .type(FunctionType.of(UpperCaseMessage.class)); FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>( new Reverse(), "reverse").type(FunctionType.of(Reverse.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(upperCaseRegistration); catalog.register(reverseRegistration); Function<Message<String>, String> lookedUpFunction = catalog .lookup("uppercase|reverse"); assertThat(lookedUpFunction).isNotNull(); String result = lookedUpFunction.apply(MessageBuilder.withPayload("star").setHeader("foo", "bar").build()); assertThat(result).isEqualTo("RATS"); }
Example #11
Source File: KotlinLambdaToFunctionAutoConfiguration.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Override public FunctionRegistration getObject() throws Exception { String name = this.name.endsWith(FunctionRegistration.REGISTRATION_NAME_SUFFIX) ? this.name.replace(FunctionRegistration.REGISTRATION_NAME_SUFFIX, "") : this.name; Type functionType = FunctionContextUtils.findType(name, this.beanFactory); FunctionRegistration<?> registration = new FunctionRegistration<>(this, name); Type[] types = ((ParameterizedType) functionType).getActualTypeArguments(); if (functionType.getTypeName().contains("Function0")) { functionType = ResolvableType.forClassWithGenerics(Supplier.class, ResolvableType.forType(types[0])) .getType(); } else if (functionType.getTypeName().contains("Function1")) { functionType = ResolvableType.forClassWithGenerics(Function.class, ResolvableType.forType(types[0]), ResolvableType.forType(types[1])).getType(); } else { throw new UnsupportedOperationException("Multi argument Kotlin functions are not currently supported"); } registration = registration.type(functionType); return registration; }
Example #12
Source File: ContextFunctionCatalogInitializer.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof FunctionRegistry) { FunctionRegistry catalog = (FunctionRegistry) bean; for (FunctionRegistration<?> registration : this.functions) { Assert.notEmpty(registration.getNames(), "FunctionRegistration must define at least one name. Was empty"); if (registration.getType() == null) { throw new IllegalStateException( "You need an explicit type for the function: " + registration.getNames()); // TODO: in principle Spring could know how to extract this // from the supplier, but in practice there is no functional // bean registration with parametric types. } catalog.register(registration); } } return bean; }
Example #13
Source File: FunctionExporterInitializer.java From spring-cloud-function with Apache License 2.0 | 6 votes |
private void registerExport(GenericApplicationContext context) { context.registerBean(ExporterProperties.class, () -> new ExporterProperties()); context.registerBean(FunctionExporterAutoConfiguration.class, () -> new FunctionExporterAutoConfiguration(context.getBean(ExporterProperties.class))); if (context.getBeanFactory().getBeanNamesForType(DestinationResolver.class, false, false).length == 0) { context.registerBean(DestinationResolver.class, () -> context.getBean(FunctionExporterAutoConfiguration.class).simpleDestinationResolver()); } if (context.getBeanFactory().getBeanNamesForType(RequestBuilder.class, false, false).length == 0) { context.registerBean(RequestBuilder.class, () -> context.getBean(FunctionExporterAutoConfiguration.class) .simpleRequestBuilder(context.getEnvironment())); } if (context.getEnvironment().getProperty("spring.cloud.function.web.export.source.url") != null) { context.registerBean("origin", FunctionRegistration.class, () -> context .getBean(FunctionExporterAutoConfiguration.class).origin(context.getBean(WebClient.Builder.class))); } if (context.getEnvironment().getProperty("spring.cloud.function.web.export.sink.url") != null) { context.registerBean(SupplierExporter.class, () -> context.getBean(FunctionExporterAutoConfiguration.class).sourceForwarder( context.getBean(RequestBuilder.class), context.getBean(DestinationResolver.class), context.getBean(FunctionCatalog.class), context.getBean(WebClient.Builder.class))); } }
Example #14
Source File: BeanFactoryAwareFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override Type discoverFunctionType(Object function, String... names) { if (function instanceof RoutingFunction) { return FunctionType.of(FunctionContextUtils.findType(applicationContext.getBeanFactory(), names)).getType(); } else if (function instanceof FunctionRegistration) { return ((FunctionRegistration) function).getType().getType(); } boolean beanDefinitionExists = false; for (int i = 0; i < names.length && !beanDefinitionExists; i++) { beanDefinitionExists = this.applicationContext.getBeanFactory().containsBeanDefinition(names[i]); if (this.applicationContext.containsBean("&" + names[i])) { Class<?> objectType = this.applicationContext.getBean("&" + names[i], FactoryBean.class) .getObjectType(); return FunctionTypeUtils.discoverFunctionTypeFromClass(objectType); } } if (!beanDefinitionExists) { logger.info("BeanDefinition for function name(s) '" + Arrays.asList(names) + "' can not be located. FunctionType will be based on " + function.getClass()); } Type type = FunctionTypeUtils.discoverFunctionTypeFromClass(function.getClass()); if (beanDefinitionExists) { Type t = FunctionTypeUtils.getImmediateGenericType(type, 0); if (t == null || t == Object.class) { type = FunctionType.of(FunctionContextUtils.findType(this.applicationContext.getBeanFactory(), names)).getType(); } } return type; }
Example #15
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void simpleConsumer() { create(SimpleConfiguration.class); assertThat(this.context.getBean("consumer")) .isInstanceOf(FunctionRegistration.class); Function<Flux<String>, Mono<Void>> consumer = this.catalog.lookup(Function.class, "consumer"); consumer.apply(Flux.just("foo", "bar")).subscribe(); assertThat(this.context.getBean(SimpleConfiguration.class).list).hasSize(2); }
Example #16
Source File: SimpleFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override public FunctionRegistration<?> getRegistration(Object function) { FunctionRegistration<?> registration = this.registrationsByFunction.get(function); // need to do this due to the deployer not wrapping the actual target into FunctionInvocationWrapper // hence the lookup would need to be made by the actual target if (registration == null && function instanceof FunctionInvocationWrapper) { function = ((FunctionInvocationWrapper) function).target; } return this.registrationsByFunction.get(function); }
Example #17
Source File: SimpleFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T> void register(FunctionRegistration<T> registration) { this.registrationsByFunction.put(registration.getTarget(), (FunctionRegistration<Object>) registration); for (String name : registration.getNames()) { this.registrationsByName.put(name, (FunctionRegistration<Object>) registration); } }
Example #18
Source File: AbstractComposableFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 5 votes |
private FunctionRegistration<?> find(String name, boolean supplierFound) { Object result = this.functions.get(name); if (result == null && !StringUtils.hasText(name)) { if (supplierFound && this.getFunctionNames().size() == 1) { result = this.functions.get(this.getFunctionNames().iterator().next()); } else if (!supplierFound && this.getSupplierNames().size() == 1) { result = this.functions.get(this.getSupplierNames().iterator().next()); } } return getRegistration(result); }
Example #19
Source File: AbstractComposableFunctionRegistry.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override public FunctionRegistration<?> getRegistration(Object function) { String functionName = function == null ? null : this.lookupFunctionName(function); if (StringUtils.hasText(functionName)) { FunctionRegistration<?> registration = new FunctionRegistration<Object>( function, functionName); FunctionType functionType = this.findType(registration, functionName); return registration.type(functionType.getType()); } return null; }
Example #20
Source File: StreamBridge.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@Override public void afterSingletonsInstantiated() { if (this.initialized) { return; } FunctionRegistration<Function<Object, Object>> fr = new FunctionRegistration<>(v -> v, STREAM_BRIDGE_FUNC_NAME); this.functionRegistry.register(fr.type(FunctionType.from(Object.class).to(Object.class).message())); 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; }
Example #21
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override public void initialize(GenericApplicationContext context) { context.registerBean("function", FunctionRegistration.class, () -> new FunctionRegistration<>(function()).type( FunctionType.from(String.class).to(String.class).getType())); context.registerBean(PropertiesConfiguration.class, () -> this); }
Example #22
Source File: SimpleFunctionRegistryTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void testFunctionCompositionExplicit() { FunctionRegistration<Words> wordsRegistration = new FunctionRegistration<>( new Words(), "words").type(FunctionType.of(Words.class)); FunctionRegistration<Reverse> reverseRegistration = new FunctionRegistration<>( new Reverse(), "reverse").type(FunctionType.of(Reverse.class)); SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter); catalog.register(wordsRegistration); catalog.register(reverseRegistration); Supplier<String> lookedUpFunction = catalog.lookup("words|reverse"); assertThat(lookedUpFunction).isNotNull(); assertThat(lookedUpFunction.get()).isEqualTo("olleh"); }
Example #23
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void simpleSupplier() { create(SimpleConfiguration.class); assertThat(this.context.getBean("supplier")) .isInstanceOf(FunctionRegistration.class); Supplier<String> supplier = this.catalog.lookup(Supplier.class, "supplier"); assertThat(supplier.get()).isEqualTo("hello"); }
Example #24
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void lookUps() { create(SimpleConfiguration.class); assertThat(this.context.getBean("function")) .isInstanceOf(FunctionRegistration.class); assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) .isInstanceOf(Function.class); }
Example #25
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void properties() { create(PropertiesConfiguration.class, "app.greeting=hello"); assertThat(this.context.getBean("function")) .isInstanceOf(FunctionRegistration.class); @SuppressWarnings("unchecked") Function<Flux<String>, Flux<String>> function = (Function<Flux<String>, Flux<String>>) this.catalog .lookup(Function.class, "function"); assertThat(function).isInstanceOf(Function.class); assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("hello foo"); }
Example #26
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void value() { create(ValueConfiguration.class, "app.greeting=hello"); assertThat(this.context.getBean("function")) .isInstanceOf(FunctionRegistration.class); @SuppressWarnings("unchecked") Function<Flux<String>, Flux<String>> function = (Function<Flux<String>, Flux<String>>) this.catalog .lookup(Function.class, "function"); assertThat(function).isInstanceOf(Function.class); assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("hello foo"); }
Example #27
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test @Disabled public void compose() { create(SimpleConfiguration.class); assertThat(this.context.getBean("function")) .isInstanceOf(FunctionRegistration.class); @SuppressWarnings("unchecked") Supplier<Flux<String>> supplier = (Supplier<Flux<String>>) this.catalog .lookup(Supplier.class, "supplier|function"); assertThat(supplier).isInstanceOf(Supplier.class); assertThat(supplier.get().blockFirst()).isEqualTo("HELLO"); // TODO: support for function composition }
Example #28
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void missingType() { Assertions.assertThrows(BeanCreationException.class, () -> { create(MissingTypeConfiguration.class); assertThat(this.context.getBean("function")) .isInstanceOf(FunctionRegistration.class); assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "function")) .isInstanceOf(Function.class); // TODO: support for type inference from functional bean registrations }); }
Example #29
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void dependencyInjection() { create(DependencyInjectionConfiguration.class); assertThat(this.context.getBean("foos")).isInstanceOf(FunctionRegistration.class); assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "foos")) .isInstanceOf(Function.class); assertThat( this.inspector.getInputType(this.catalog.lookup(Function.class, "foos"))) .isEqualTo(String.class); }
Example #30
Source File: ContextFunctionCatalogInitializerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void simpleFunction() { create(SimpleConfiguration.class); Object bean = this.context.getBean("function"); assertThat(bean).isInstanceOf(FunctionRegistration.class); Function<Flux<String>, Flux<Person>> function = this.catalog.lookup(Function.class, "function"); assertThat(function.apply(Flux.just("{\"name\":\"foo\"}")).blockFirst().getName()).isEqualTo("FOO"); assertThat(bean).isNotSameAs(function); assertThat(this.inspector.getRegistration(function)).isNotNull(); assertThat(this.inspector.getRegistration(function).getType()) .isEqualTo(FunctionType.from(Person.class).to(Person.class)); }