Search in sources :

Example 1 with ConsumerEndpointFactoryBean

use of org.springframework.integration.config.ConsumerEndpointFactoryBean in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method register.

@SuppressWarnings("unchecked")
private <S extends ConsumerEndpointSpec<S, ? extends MessageHandler>> B register(S endpointSpec, Consumer<S> endpointConfigurer) {
    if (endpointConfigurer != null) {
        endpointConfigurer.accept(endpointSpec);
    }
    MessageChannel inputChannel = this.currentMessageChannel;
    this.currentMessageChannel = null;
    if (inputChannel == null) {
        inputChannel = new DirectChannel();
        this.registerOutputChannelIfCan(inputChannel);
    }
    Tuple2<ConsumerEndpointFactoryBean, ? extends MessageHandler> factoryBeanTuple2 = endpointSpec.get();
    addComponents(endpointSpec.getComponentsToRegister());
    if (inputChannel instanceof MessageChannelReference) {
        factoryBeanTuple2.getT1().setInputChannelName(((MessageChannelReference) inputChannel).getName());
    } else {
        if (inputChannel instanceof FixedSubscriberChannelPrototype) {
            String beanName = ((FixedSubscriberChannelPrototype) inputChannel).getName();
            inputChannel = new FixedSubscriberChannel(factoryBeanTuple2.getT2());
            if (beanName != null) {
                ((FixedSubscriberChannel) inputChannel).setBeanName(beanName);
            }
            registerOutputChannelIfCan(inputChannel);
        }
        factoryBeanTuple2.getT1().setInputChannel(inputChannel);
    }
    return addComponent(endpointSpec).currentComponent(factoryBeanTuple2.getT2());
}
Also used : MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) DirectChannel(org.springframework.integration.channel.DirectChannel) FixedSubscriberChannelPrototype(org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 2 with ConsumerEndpointFactoryBean

use of org.springframework.integration.config.ConsumerEndpointFactoryBean in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerViaConsumerEndpointFactoryBean.

@Test
public void testReactiveStreamsConsumerViaConsumerEndpointFactoryBean() throws Exception {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(3);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    ConsumerEndpointFactoryBean endpointFactoryBean = new ConsumerEndpointFactoryBean();
    endpointFactoryBean.setBeanFactory(mock(ConfigurableBeanFactory.class));
    endpointFactoryBean.setInputChannel(testChannel);
    endpointFactoryBean.setHandler(messageHandler);
    endpointFactoryBean.setBeanName("reactiveConsumer");
    endpointFactoryBean.afterPropertiesSet();
    endpointFactoryBean.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    endpointFactoryBean.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    endpointFactoryBean.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result.size(), equalTo(3));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 3 with ConsumerEndpointFactoryBean

use of org.springframework.integration.config.ConsumerEndpointFactoryBean 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)

Example 4 with ConsumerEndpointFactoryBean

use of org.springframework.integration.config.ConsumerEndpointFactoryBean in project spring-integration by spring-projects.

the class MessageHistoryIntegrationTests method testNoHistoryAwareMessageHandler.

@Test
public void testNoHistoryAwareMessageHandler() {
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("messageHistoryWithoutHistoryWriter.xml", MessageHistoryIntegrationTests.class);
    Map<String, ConsumerEndpointFactoryBean> cefBeans = ac.getBeansOfType(ConsumerEndpointFactoryBean.class);
    for (ConsumerEndpointFactoryBean cefBean : cefBeans.values()) {
        DirectFieldAccessor bridgeAccessor = new DirectFieldAccessor(cefBean);
        String handlerClassName = bridgeAccessor.getPropertyValue("handler").getClass().getName();
        assertFalse("org.springframework.integration.config.MessageHistoryWritingMessageHandler".equals(handlerClassName));
    }
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Aggregations

ConsumerEndpointFactoryBean (org.springframework.integration.config.ConsumerEndpointFactoryBean)4 DirectChannel (org.springframework.integration.channel.DirectChannel)3 Test (org.junit.Test)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 MessageHandler (org.springframework.messaging.MessageHandler)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matchers (org.hamcrest.Matchers)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Assert.assertSame (org.junit.Assert.assertSame)1 Assert.assertThat (org.junit.Assert.assertThat)1