Search in sources :

Example 51 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class ConsumerEndpointFactoryBean method initializeEndpoint.

@SuppressWarnings("unchecked")
private void initializeEndpoint() throws Exception {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            return;
        }
        MessageChannel channel = null;
        if (StringUtils.hasText(this.inputChannelName)) {
            channel = this.channelResolver.resolveDestination(this.inputChannelName);
        }
        if (this.inputChannel != null) {
            channel = this.inputChannel;
        }
        Assert.state(channel != null, "one of inputChannelName or inputChannel is required");
        if (channel instanceof SubscribableChannel) {
            Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName + "', since '" + channel + "' is a SubscribableChannel (not pollable).");
            this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
            if (logger.isWarnEnabled() && Boolean.FALSE.equals(this.autoStartup) && channel instanceof FixedSubscriberChannel) {
                logger.warn("'autoStartup=\"false\"' has no effect when using a FixedSubscriberChannel");
            }
        } else if (channel instanceof PollableChannel) {
            PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
            if (this.pollerMetadata == null) {
                this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
                Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName + "', and no default poller is available within the context.");
            }
            pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
            pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());
            pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
            pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
            pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler());
            pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout());
            pollingConsumer.setTransactionSynchronizationFactory(this.pollerMetadata.getTransactionSynchronizationFactory());
            pollingConsumer.setBeanClassLoader(this.beanClassLoader);
            pollingConsumer.setBeanFactory(this.beanFactory);
            this.endpoint = pollingConsumer;
        } else {
            this.endpoint = new ReactiveStreamsConsumer(channel, this.handler);
        }
        this.endpoint.setBeanName(this.beanName);
        this.endpoint.setBeanFactory(this.beanFactory);
        if (this.autoStartup != null) {
            this.endpoint.setAutoStartup(this.autoStartup);
        }
        int phase = this.phase;
        if (!this.isPhaseSet && this.endpoint instanceof PollingConsumer) {
            phase = Integer.MAX_VALUE / 2;
        }
        this.endpoint.setPhase(phase);
        this.endpoint.setRole(this.role);
        if (this.taskScheduler != null) {
            this.endpoint.setTaskScheduler(this.taskScheduler);
        }
        this.endpoint.afterPropertiesSet();
        this.initialized = true;
    }
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 52 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class ScatterGatherHandler method doInit.

@Override
protected void doInit() {
    if (this.gatherChannel == null) {
        this.gatherChannel = new FixedSubscriberChannel(this.gatherer);
    } else {
        if (this.gatherChannel instanceof SubscribableChannel) {
            this.gatherEndpoint = new EventDrivenConsumer((SubscribableChannel) this.gatherChannel, this.gatherer);
        } else if (this.gatherChannel instanceof PollableChannel) {
            this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer);
            ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
        } else {
            throw new MessagingException("Unsupported 'replyChannel' type [" + this.gatherChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
        }
        this.gatherEndpoint.setBeanFactory(this.getBeanFactory());
        this.gatherEndpoint.afterPropertiesSet();
    }
    ((MessageProducer) this.gatherer).setOutputChannel(new FixedSubscriberChannel(message -> {
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(GATHER_RESULT_CHANNEL)) {
            Object gatherResultChannel = headers.get(GATHER_RESULT_CHANNEL);
            if (gatherResultChannel instanceof MessageChannel) {
                messagingTemplate.send((MessageChannel) gatherResultChannel, message);
            } else if (gatherResultChannel instanceof String) {
                messagingTemplate.send((String) gatherResultChannel, message);
            }
        } else {
            throw new MessageDeliveryException(message, "The 'gatherResultChannel' header is required to delivery gather result.");
        }
    }));
    this.replyChannelRegistry = getBeanFactory().getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) ClassUtils(org.springframework.util.ClassUtils) AopUtils(org.springframework.aop.support.AopUtils) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) Lifecycle(org.springframework.context.Lifecycle) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageHandler(org.springframework.messaging.MessageHandler) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Assert(org.springframework.util.Assert) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 53 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class ScatterGatherHandler method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    PollableChannel gatherResultChannel = new QueueChannel();
    Object gatherResultChannelName = this.replyChannelRegistry.channelToChannelName(gatherResultChannel);
    Message<?> scatterMessage = getMessageBuilderFactory().fromMessage(requestMessage).setHeader(GATHER_RESULT_CHANNEL, gatherResultChannelName).setReplyChannel(this.gatherChannel).build();
    this.messagingTemplate.send(this.scatterChannel, scatterMessage);
    Message<?> gatherResult = gatherResultChannel.receive(this.gatherTimeout);
    if (gatherResult != null) {
        return getMessageBuilderFactory().fromMessage(gatherResult).removeHeader(GATHER_RESULT_CHANNEL).setHeader(MessageHeaders.REPLY_CHANNEL, requestMessage.getHeaders().getReplyChannel()).build();
    }
    return null;
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel)

Example 54 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method autoDetectionWithApplicationContext.

@Test
public void autoDetectionWithApplicationContext() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("messageBusTests.xml", this.getClass());
    context.start();
    PollableChannel sourceChannel = (PollableChannel) context.getBean("sourceChannel");
    sourceChannel.send(new GenericMessage<String>("test"));
    PollableChannel targetChannel = (PollableChannel) context.getBean("targetChannel");
    Message<?> result = targetChannel.receive(10000);
    assertEquals("test", result.getPayload());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Example 55 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class AggregatorParserTests method testAggregatorWithPojoReleaseStrategy.

@Test
@SuppressWarnings("unchecked")
public void testAggregatorWithPojoReleaseStrategy() {
    MessageChannel input = this.context.getBean("aggregatorWithPojoReleaseStrategyInput", MessageChannel.class);
    EventDrivenConsumer endpoint = this.context.getBean("aggregatorWithPojoReleaseStrategy", EventDrivenConsumer.class);
    ReleaseStrategy releaseStrategy = TestUtils.getPropertyValue(endpoint, "handler.releaseStrategy", ReleaseStrategy.class);
    Assert.assertTrue(releaseStrategy instanceof MethodInvokingReleaseStrategy);
    MessagingMethodInvokerHelper<Long> methodInvokerHelper = TestUtils.getPropertyValue(releaseStrategy, "adapter.delegate", MessagingMethodInvokerHelper.class);
    Object handlerMethods = TestUtils.getPropertyValue(methodInvokerHelper, "handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = TestUtils.getPropertyValue(methodInvokerHelper, "handlerMethod");
    assertTrue(handlerMethod.toString().contains("checkCompleteness"));
    input.send(createMessage(1L, "correlationId", 4, 0, null));
    input.send(createMessage(2L, "correlationId", 4, 1, null));
    input.send(createMessage(3L, "correlationId", 4, 2, null));
    PollableChannel outputChannel = (PollableChannel) context.getBean("outputChannel");
    Message<?> reply = outputChannel.receive(0);
    Assert.assertNull(reply);
    input.send(createMessage(5L, "correlationId", 4, 3, null));
    reply = outputChannel.receive(0);
    Assert.assertNotNull(reply);
    assertEquals(11L, reply.getPayload());
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) ReleaseStrategy(org.springframework.integration.aggregator.ReleaseStrategy) ExpressionEvaluatingReleaseStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy) Test(org.junit.Test)

Aggregations

PollableChannel (org.springframework.messaging.PollableChannel)210 Test (org.junit.Test)190 MessageChannel (org.springframework.messaging.MessageChannel)89 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)82 QueueChannel (org.springframework.integration.channel.QueueChannel)52 GenericMessage (org.springframework.messaging.support.GenericMessage)52 Message (org.springframework.messaging.Message)40 BeanFactory (org.springframework.beans.factory.BeanFactory)25 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)20 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)19 MessagingException (org.springframework.messaging.MessagingException)16 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)13 MessagingTemplate (org.springframework.integration.core.MessagingTemplate)12 ErrorMessage (org.springframework.messaging.support.ErrorMessage)12 Document (org.w3c.dom.Document)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)9 MessageHistory (org.springframework.integration.history.MessageHistory)9