Search in sources :

Example 6 with FixedSubscriberChannel

use of org.springframework.integration.channel.FixedSubscriberChannel in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method checkAndConfigureFixedSubscriberChannel.

@SuppressWarnings("unchecked")
public static void checkAndConfigureFixedSubscriberChannel(Element element, ParserContext parserContext, String channelName, String handlerBeanName) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    if (registry.containsBeanDefinition(channelName)) {
        BeanDefinition inputChannelDefinition = registry.getBeanDefinition(channelName);
        if (FixedSubscriberChannel.class.getName().equals(inputChannelDefinition.getBeanClassName())) {
            ConstructorArgumentValues constructorArgumentValues = inputChannelDefinition.getConstructorArgumentValues();
            if (constructorArgumentValues.isEmpty()) {
                constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(handlerBeanName));
            } else {
                parserContext.getReaderContext().error("Only one subscriber is allowed for a FixedSubscriberChannel.", element);
            }
        }
    } else {
        BeanDefinition bfppd;
        if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME)) {
            bfppd = new RootBeanDefinition(FixedSubscriberChannelBeanFactoryPostProcessor.class);
            registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME, bfppd);
        } else {
            bfppd = registry.getBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME);
        }
        ManagedMap<String, String> candidates;
        ValueHolder argumentValue = bfppd.getConstructorArgumentValues().getArgumentValue(0, Map.class);
        if (argumentValue == null) {
            candidates = new ManagedMap<String, String>();
            bfppd.getConstructorArgumentValues().addIndexedArgumentValue(0, candidates);
        } else {
            candidates = (ManagedMap<String, String>) argumentValue.getValue();
        }
        candidates.put(handlerBeanName, channelName);
    }
}
Also used : FixedSubscriberChannelBeanFactoryPostProcessor(org.springframework.integration.config.FixedSubscriberChannelBeanFactoryPostProcessor) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 7 with FixedSubscriberChannel

use of org.springframework.integration.channel.FixedSubscriberChannel in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method log.

/**
 * Populate a {@link WireTap} for the {@link #currentMessageChannel}
 * with the {@link LoggingHandler} subscriber for the provided
 * {@link LoggingHandler.Level} logging level, logging category
 * and SpEL expression for the log message.
 * @param level the {@link LoggingHandler.Level}.
 * @param category the logging category.
 * @param logExpression the {@link Expression} to evaluate logger message at runtime
 * against the request {@link Message}.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see #wireTap(WireTapSpec)
 */
public B log(LoggingHandler.Level level, String category, Expression logExpression) {
    LoggingHandler loggingHandler = new LoggingHandler(level);
    if (StringUtils.hasText(category)) {
        loggingHandler.setLoggerName(category);
    }
    if (logExpression != null) {
        loggingHandler.setLogExpression(logExpression);
    } else {
        loggingHandler.setShouldLogFullMessage(true);
    }
    addComponent(loggingHandler);
    MessageChannel loggerChannel = new FixedSubscriberChannel(loggingHandler);
    return wireTap(loggerChannel);
}
Also used : LoggingHandler(org.springframework.integration.handler.LoggingHandler) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 8 with FixedSubscriberChannel

use of org.springframework.integration.channel.FixedSubscriberChannel in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method route.

private <R extends AbstractMessageRouter, S extends AbstractRouterSpec<S, R>> B route(S routerSpec, Consumer<S> routerConfigurer) {
    if (routerConfigurer != null) {
        routerConfigurer.accept(routerSpec);
    }
    BridgeHandler bridgeHandler = new BridgeHandler();
    boolean registerSubflowBridge = false;
    Map<Object, String> componentsToRegister = null;
    Map<Object, String> routerComponents = routerSpec.getComponentsToRegister();
    if (routerComponents != null) {
        componentsToRegister = new LinkedHashMap<>(routerComponents);
        routerComponents.clear();
    }
    register(routerSpec, null);
    if (!CollectionUtils.isEmpty(componentsToRegister)) {
        for (Map.Entry<Object, String> entry : componentsToRegister.entrySet()) {
            Object component = entry.getKey();
            if (component instanceof IntegrationFlowDefinition) {
                IntegrationFlowDefinition<?> flowBuilder = (IntegrationFlowDefinition<?>) component;
                if (flowBuilder.isOutputChannelRequired()) {
                    registerSubflowBridge = true;
                    flowBuilder.channel(new FixedSubscriberChannel(bridgeHandler));
                }
                addComponent(flowBuilder.get());
            } else {
                this.integrationComponents.put(component, entry.getValue());
            }
        }
    }
    if (routerSpec.isDefaultToParentFlow()) {
        routerSpec.defaultOutputChannel(new FixedSubscriberChannel(bridgeHandler));
        registerSubflowBridge = true;
    }
    if (registerSubflowBridge) {
        this.currentComponent = null;
        handle(bridgeHandler);
    }
    return _this();
}
Also used : BridgeHandler(org.springframework.integration.handler.BridgeHandler) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 9 with FixedSubscriberChannel

use of org.springframework.integration.channel.FixedSubscriberChannel in project spring-integration by spring-projects.

the class IntegrationFlowBeanPostProcessor method processStandardIntegrationFlow.

private Object processStandardIntegrationFlow(StandardIntegrationFlow flow, String flowBeanName) {
    String flowNamePrefix = flowBeanName + ".";
    int subFlowNameIndex = 0;
    int channelNameIndex = 0;
    Map<Object, String> integrationComponents = flow.getIntegrationComponents();
    Map<Object, String> targetIntegrationComponents = new LinkedHashMap<>(integrationComponents.size());
    for (Map.Entry<Object, String> entry : integrationComponents.entrySet()) {
        Object component = entry.getKey();
        if (component instanceof ConsumerEndpointSpec) {
            ConsumerEndpointSpec<?, ?> endpointSpec = (ConsumerEndpointSpec<?, ?>) component;
            MessageHandler messageHandler = endpointSpec.get().getT2();
            ConsumerEndpointFactoryBean endpoint = endpointSpec.get().getT1();
            String id = endpointSpec.getId();
            if (id == null) {
                id = generateBeanName(endpoint, entry.getValue());
            }
            Collection<?> messageHandlers = this.beanFactory.getBeansOfType(messageHandler.getClass(), false, false).values();
            if (!messageHandlers.contains(messageHandler)) {
                String handlerBeanName = generateBeanName(messageHandler);
                String[] handlerAlias = new String[] { id + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX };
                registerComponent(messageHandler, handlerBeanName, flowBeanName);
                for (String alias : handlerAlias) {
                    this.beanFactory.registerAlias(handlerBeanName, alias);
                }
            }
            registerComponent(endpoint, id, flowBeanName);
            targetIntegrationComponents.put(endpoint, id);
        } else {
            Collection<?> values = this.beanFactory.getBeansOfType(component.getClass(), false, false).values();
            if (!values.contains(component)) {
                if (component instanceof AbstractMessageChannel) {
                    String channelBeanName = ((AbstractMessageChannel) component).getComponentName();
                    if (channelBeanName == null) {
                        channelBeanName = entry.getValue();
                        if (channelBeanName == null) {
                            channelBeanName = flowNamePrefix + "channel" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + channelNameIndex++;
                        }
                    }
                    registerComponent(component, channelBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, channelBeanName);
                } else if (component instanceof MessageChannelReference) {
                    String channelBeanName = ((MessageChannelReference) component).getName();
                    if (!this.beanFactory.containsBean(channelBeanName)) {
                        DirectChannel directChannel = new DirectChannel();
                        registerComponent(directChannel, channelBeanName, flowBeanName);
                        targetIntegrationComponents.put(directChannel, channelBeanName);
                    }
                } else if (component instanceof FixedSubscriberChannel) {
                    FixedSubscriberChannel fixedSubscriberChannel = (FixedSubscriberChannel) component;
                    String channelBeanName = fixedSubscriberChannel.getComponentName();
                    if ("Unnamed fixed subscriber channel".equals(channelBeanName)) {
                        channelBeanName = flowNamePrefix + "channel" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + channelNameIndex++;
                    }
                    registerComponent(component, channelBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, channelBeanName);
                } else if (component instanceof SourcePollingChannelAdapterSpec) {
                    SourcePollingChannelAdapterSpec spec = (SourcePollingChannelAdapterSpec) component;
                    Map<Object, String> componentsToRegister = spec.getComponentsToRegister();
                    if (!CollectionUtils.isEmpty(componentsToRegister)) {
                        componentsToRegister.entrySet().stream().filter(o -> !this.beanFactory.getBeansOfType(o.getKey().getClass(), false, false).values().contains(o.getKey())).forEach(o -> registerComponent(o.getKey(), generateBeanName(o.getKey(), o.getValue())));
                    }
                    SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = spec.get().getT1();
                    String id = spec.getId();
                    if (!StringUtils.hasText(id)) {
                        id = generateBeanName(pollingChannelAdapterFactoryBean, entry.getValue());
                    }
                    registerComponent(pollingChannelAdapterFactoryBean, id, flowBeanName);
                    targetIntegrationComponents.put(pollingChannelAdapterFactoryBean, id);
                    MessageSource<?> messageSource = spec.get().getT2();
                    if (!this.beanFactory.getBeansOfType(messageSource.getClass(), false, false).values().contains(messageSource)) {
                        String messageSourceId = id + ".source";
                        if (messageSource instanceof NamedComponent && ((NamedComponent) messageSource).getComponentName() != null) {
                            messageSourceId = ((NamedComponent) messageSource).getComponentName();
                        }
                        registerComponent(messageSource, messageSourceId, flowBeanName);
                    }
                } else if (component instanceof StandardIntegrationFlow) {
                    String subFlowBeanName = entry.getValue() != null ? entry.getValue() : flowNamePrefix + "subFlow" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + subFlowNameIndex++;
                    registerComponent(component, subFlowBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, subFlowBeanName);
                } else if (component instanceof AnnotationGatewayProxyFactoryBean) {
                    AnnotationGatewayProxyFactoryBean gateway = (AnnotationGatewayProxyFactoryBean) component;
                    String gatewayId = entry.getValue();
                    if (gatewayId == null) {
                        gatewayId = gateway.getComponentName();
                    }
                    if (gatewayId == null) {
                        gatewayId = flowNamePrefix + "gateway";
                    }
                    registerComponent(gateway, gatewayId, flowBeanName, beanDefinition -> {
                        ((AbstractBeanDefinition) beanDefinition).setSource(new DescriptiveResource(gateway.getObjectType().getName()));
                    });
                    targetIntegrationComponents.put(component, gatewayId);
                } else {
                    String generatedBeanName = generateBeanName(component, entry.getValue());
                    registerComponent(component, generatedBeanName, flowBeanName);
                    targetIntegrationComponents.put(component, generatedBeanName);
                }
            } else {
                targetIntegrationComponents.put(entry.getKey(), entry.getValue());
            }
        }
    }
    flow.setIntegrationComponents(targetIntegrationComponents);
    return flow;
}
Also used : DescriptiveResource(org.springframework.core.io.DescriptiveResource) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageSource(org.springframework.integration.core.MessageSource) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LinkedHashMap(java.util.LinkedHashMap) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) Map(java.util.Map) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) Collection(java.util.Collection) BeansException(org.springframework.beans.BeansException) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) SmartInitializingSingleton(org.springframework.beans.factory.SmartInitializingSingleton) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) IntegrationConfigUtils(org.springframework.integration.config.IntegrationConfigUtils) MessageHandler(org.springframework.messaging.MessageHandler) CollectionUtils(org.springframework.util.CollectionUtils) BeanFactory(org.springframework.beans.factory.BeanFactory) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean) DirectChannel(org.springframework.integration.channel.DirectChannel) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) LinkedHashMap(java.util.LinkedHashMap) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) MessageSource(org.springframework.integration.core.MessageSource) DescriptiveResource(org.springframework.core.io.DescriptiveResource) NamedComponent(org.springframework.integration.support.context.NamedComponent) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Aggregations

FixedSubscriberChannel (org.springframework.integration.channel.FixedSubscriberChannel)9 MessageChannel (org.springframework.messaging.MessageChannel)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 FluxMessageChannel (org.springframework.integration.channel.FluxMessageChannel)2 ConsumerEndpointFactoryBean (org.springframework.integration.config.ConsumerEndpointFactoryBean)2 MessageChannelReference (org.springframework.integration.dsl.support.MessageChannelReference)2 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)2 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)2 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)2 MessageHandler (org.springframework.messaging.MessageHandler)2 PollableChannel (org.springframework.messaging.PollableChannel)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 Assert (org.springframework.util.Assert)2 Collection (java.util.Collection)1 AopUtils (org.springframework.aop.support.AopUtils)1