Search in sources :

Example 1 with SimpleFunctionRegistry

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

the class DefaultBinderFactory method getBinderInstance.

@SuppressWarnings("unchecked")
private <T> Binder<T, ConsumerProperties, ProducerProperties> getBinderInstance(String configurationName) {
    if (!this.binderInstanceCache.containsKey(configurationName)) {
        logger.info("Creating binder: " + configurationName);
        BinderConfiguration binderConfiguration = this.binderConfigurations.get(configurationName);
        Assert.state(binderConfiguration != null, "Unknown binder configuration: " + configurationName);
        BinderType binderType = this.binderTypeRegistry.get(binderConfiguration.getBinderType());
        Assert.notNull(binderType, "Binder type " + binderConfiguration.getBinderType() + " is not defined");
        Map<String, Object> binderProperties = new HashMap<>();
        this.flatten(null, binderConfiguration.getProperties(), binderProperties);
        ConfigurableApplicationContext binderProducingContext = this.initializeBinderContextSimple(configurationName, binderProperties, binderType, binderConfiguration);
        Map<String, MessageConverter> messageConverters = binderProducingContext.getBeansOfType(MessageConverter.class);
        if (!CollectionUtils.isEmpty(messageConverters) && !ObjectUtils.isEmpty(context.getBeansOfType(FunctionCatalog.class))) {
            FunctionCatalog functionCatalog = this.context.getBean(FunctionCatalog.class);
            if (functionCatalog instanceof SimpleFunctionRegistry) {
                ((SimpleFunctionRegistry) functionCatalog).addMessageConverters(messageConverters.values());
            }
        }
        Binder<T, ?, ?> binder = binderProducingContext.getBean(Binder.class);
        /*
			 * This will ensure that application defined errorChannel and other beans are
			 * accessible within binder's context (see
			 * https://github.com/spring-cloud/spring-cloud-stream/issues/1384)
			 */
        if (this.context != null && binder instanceof ApplicationContextAware) {
            ((ApplicationContextAware) binder).setApplicationContext(this.context);
        }
        if (!CollectionUtils.isEmpty(this.listeners)) {
            for (Listener binderFactoryListener : this.listeners) {
                binderFactoryListener.afterBinderContextInitialized(configurationName, binderProducingContext);
            }
        }
        logger.info("Caching the binder: " + configurationName);
        this.binderInstanceCache.put(configurationName, new SimpleImmutableEntry<>(binder, binderProducingContext));
    }
    logger.info("Retrieving cached binder: " + configurationName);
    return (Binder<T, ConsumerProperties, ProducerProperties>) this.binderInstanceCache.get(configurationName).getKey();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) FunctionCatalog(org.springframework.cloud.function.context.FunctionCatalog) ApplicationContextAware(org.springframework.context.ApplicationContextAware) ApplicationListener(org.springframework.context.ApplicationListener) HashMap(java.util.HashMap) SimpleFunctionRegistry(org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry) MessageConverter(org.springframework.messaging.converter.MessageConverter)

Aggregations

HashMap (java.util.HashMap)1 FunctionCatalog (org.springframework.cloud.function.context.FunctionCatalog)1 SimpleFunctionRegistry (org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationListener (org.springframework.context.ApplicationListener)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 MessageConverter (org.springframework.messaging.converter.MessageConverter)1