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");
}
}
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());
}
}
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;
}
Aggregations