Search in sources :

Example 6 with MessageProducer

use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.

the class ManualFlowTests method testMessageProducerForOutputChannel.

@Test
public void testMessageProducerForOutputChannel() {
    class MessageProducingHandler implements MessageHandler, MessageProducer {

        private MessageChannel outputChannel;

        @Override
        public void setOutputChannel(MessageChannel outputChannel) {
            this.outputChannel = outputChannel;
        }

        @Override
        public MessageChannel getOutputChannel() {
            return this.outputChannel;
        }

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            this.outputChannel.send(message);
        }
    }
    PollableChannel resultChannel = new QueueChannel();
    IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.handle(new MessageProducingHandler()).channel(resultChannel)).register();
    this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId()).send(new GenericMessage<>("test"));
    Message<?> receive = resultChannel.receive(1000);
    assertNotNull(receive);
    assertEquals("test", receive.getPayload());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) MessageProducer(org.springframework.integration.core.MessageProducer) Test(org.junit.Test)

Example 7 with MessageProducer

use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method registerOutputChannelIfCan.

private B registerOutputChannelIfCan(MessageChannel outputChannel) {
    if (!(outputChannel instanceof FixedSubscriberChannelPrototype)) {
        this.integrationComponents.put(outputChannel, null);
        if (this.currentComponent != null) {
            String channelName = null;
            if (outputChannel instanceof MessageChannelReference) {
                channelName = ((MessageChannelReference) outputChannel).getName();
            }
            Object currentComponent = this.currentComponent;
            if (AopUtils.isAopProxy(currentComponent)) {
                currentComponent = extractProxyTarget(currentComponent);
            }
            if (currentComponent instanceof MessageProducer) {
                MessageProducer messageProducer = (MessageProducer) currentComponent;
                checkReuse(messageProducer);
                if (channelName != null) {
                    if (messageProducer instanceof AbstractMessageProducingHandler) {
                        ((AbstractMessageProducingHandler) messageProducer).setOutputChannelName(channelName);
                    } else {
                        throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") must extend 'AbstractMessageProducingHandler' " + "for message channel resolution by name.\n" + "Your handler should extend 'AbstractMessageProducingHandler', " + "its subclass 'AbstractReplyProducingMessageHandler', or you should " + "reference a 'MessageChannel' bean instead of its name.");
                    }
                } else {
                    messageProducer.setOutputChannel(outputChannel);
                }
            } else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
                SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = ((SourcePollingChannelAdapterSpec) currentComponent).get().getT1();
                if (channelName != null) {
                    pollingChannelAdapterFactoryBean.setOutputChannelName(channelName);
                } else {
                    pollingChannelAdapterFactoryBean.setOutputChannel(outputChannel);
                }
            } else {
                throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. " + "This is the end of the integration flow.");
            }
            this.currentComponent = null;
        }
    }
    return _this();
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) FixedSubscriberChannelPrototype(org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Example 8 with MessageProducer

use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.

the class MockIntegrationContext method substituteMessageHandlerFor.

public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandler mockMessageHandler, boolean autoStartup) {
    Object endpoint = this.beanFactory.getBean(consumerEndpointId, IntegrationConsumer.class);
    if (autoStartup && endpoint instanceof Lifecycle) {
        ((Lifecycle) endpoint).stop();
    }
    DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
    Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
    this.beans.put(consumerEndpointId, targetMessageHandler);
    if (mockMessageHandler instanceof MessageProducer) {
        if (targetMessageHandler instanceof MessageProducer) {
            MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
            ((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
        } else {
            if (mockMessageHandler instanceof MockMessageHandler) {
                if (TestUtils.getPropertyValue(mockMessageHandler, "hasReplies", Boolean.class)) {
                    throw new IllegalStateException("The [" + mockMessageHandler + "] " + "with replies can't replace simple MessageHandler [" + targetMessageHandler + "]");
                }
            } else {
                throw new IllegalStateException("The MessageProducer handler [" + mockMessageHandler + "] " + "can't replace simple MessageHandler [" + targetMessageHandler + "]");
            }
        }
    }
    directFieldAccessor.setPropertyValue("handler", mockMessageHandler);
    if (autoStartup && endpoint instanceof Lifecycle) {
        ((Lifecycle) endpoint).start();
    }
}
Also used : MockMessageHandler(org.springframework.integration.test.mock.MockMessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageProducer(org.springframework.integration.core.MessageProducer)

Example 9 with MessageProducer

use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.

the class IntegrationMBeanExporter method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    Map<String, MessageHandlerMetrics> messageHandlers = this.applicationContext.getBeansOfType(MessageHandlerMetrics.class);
    for (Entry<String, MessageHandlerMetrics> entry : messageHandlers.entrySet()) {
        String beanName = entry.getKey();
        MessageHandlerMetrics bean = entry.getValue();
        if (this.handlerInAnonymousWrapper(bean) != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping " + beanName + " because it wraps another handler");
            }
            continue;
        }
        // If the handler is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageHandlerMetrics monitor = (MessageHandlerMetrics) extractTarget(bean);
        this.handlers.add(monitor);
    }
    Map<String, MessageSourceMetrics> messageSources = this.applicationContext.getBeansOfType(MessageSourceMetrics.class);
    for (Entry<String, MessageSourceMetrics> entry : messageSources.entrySet()) {
        // If the source is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageSourceMetrics monitor = (MessageSourceMetrics) extractTarget(entry.getValue());
        this.sources.add(monitor);
    }
    Map<String, MessageChannelMetrics> messageChannels = this.applicationContext.getBeansOfType(MessageChannelMetrics.class);
    for (Entry<String, MessageChannelMetrics> entry : messageChannels.entrySet()) {
        // If the channel is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(entry.getValue());
        this.channels.add(monitor);
    }
    Map<String, MessageProducer> messageProducers = this.applicationContext.getBeansOfType(MessageProducer.class);
    for (Entry<String, MessageProducer> entry : messageProducers.entrySet()) {
        MessageProducer messageProducer = entry.getValue();
        if (messageProducer instanceof Lifecycle) {
            Lifecycle target = (Lifecycle) extractTarget(messageProducer);
            if (!(target instanceof AbstractMessageProducingHandler)) {
                this.inboundLifecycleMessageProducers.add(target);
            }
        }
    }
    super.afterSingletonsInstantiated();
    try {
        registerChannels();
        registerHandlers();
        registerSources();
        registerEndpoints();
        if (this.applicationContext.containsBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
            Object messageHistoryConfigurer = this.applicationContext.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            if (messageHistoryConfigurer instanceof MessageHistoryConfigurer) {
                registerBeanInstance(messageHistoryConfigurer, IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            }
        }
        if (!this.applicationContext.containsBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)) {
            this.managementConfigurer = new IntegrationManagementConfigurer();
            this.managementConfigurer.setDefaultCountsEnabled(true);
            this.managementConfigurer.setDefaultStatsEnabled(true);
            this.managementConfigurer.setApplicationContext(this.applicationContext);
            this.managementConfigurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
            this.managementConfigurer.afterSingletonsInstantiated();
        } else {
            this.managementConfigurer = this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class);
        }
    } catch (RuntimeException e) {
        unregisterBeans();
        throw e;
    }
}
Also used : IntegrationManagementConfigurer(org.springframework.integration.config.IntegrationManagementConfigurer) MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) MessageSourceMetrics(org.springframework.integration.support.management.MessageSourceMetrics) LifecycleTrackableMessageSourceMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics) LifecycleMessageSourceMetrics(org.springframework.integration.support.management.LifecycleMessageSourceMetrics) MessageHistoryConfigurer(org.springframework.integration.history.MessageHistoryConfigurer) Lifecycle(org.springframework.context.Lifecycle) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

MessageProducer (org.springframework.integration.core.MessageProducer)9 MessageChannel (org.springframework.messaging.MessageChannel)6 Lifecycle (org.springframework.context.Lifecycle)5 MessageHandler (org.springframework.messaging.MessageHandler)4 AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)3 Message (org.springframework.messaging.Message)3 Test (org.junit.Test)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 DisposableBean (org.springframework.beans.factory.DisposableBean)2 InitializingBean (org.springframework.beans.factory.InitializingBean)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1