Search in sources :

Example 6 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class AbstractMethodAnnotationPostProcessor method configurePollingEndpoint.

protected void configurePollingEndpoint(AbstractPollingEndpoint pollingEndpoint, List<Annotation> annotations) {
    PollerMetadata pollerMetadata = null;
    Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
    if (!ObjectUtils.isEmpty(pollers)) {
        Assert.state(pollers.length == 1, "The 'poller' for an Annotation-based endpoint can have only one '@Poller'.");
        Poller poller = pollers[0];
        String ref = poller.value();
        String triggerRef = poller.trigger();
        String executorRef = poller.taskExecutor();
        String fixedDelayValue = this.beanFactory.resolveEmbeddedValue(poller.fixedDelay());
        String fixedRateValue = this.beanFactory.resolveEmbeddedValue(poller.fixedRate());
        String maxMessagesPerPollValue = this.beanFactory.resolveEmbeddedValue(poller.maxMessagesPerPoll());
        String cron = this.beanFactory.resolveEmbeddedValue(poller.cron());
        String errorChannel = this.beanFactory.resolveEmbeddedValue(poller.errorChannel());
        if (StringUtils.hasText(ref)) {
            Assert.state(!StringUtils.hasText(triggerRef) && !StringUtils.hasText(executorRef) && !StringUtils.hasText(cron) && !StringUtils.hasText(fixedDelayValue) && !StringUtils.hasText(fixedRateValue) && !StringUtils.hasText(maxMessagesPerPollValue), "The '@Poller' 'ref' attribute is mutually exclusive with other attributes.");
            pollerMetadata = this.beanFactory.getBean(ref, PollerMetadata.class);
        } else {
            pollerMetadata = new PollerMetadata();
            if (StringUtils.hasText(maxMessagesPerPollValue)) {
                pollerMetadata.setMaxMessagesPerPoll(Long.parseLong(maxMessagesPerPollValue));
            } else if (pollingEndpoint instanceof SourcePollingChannelAdapter) {
                // SPCAs default to 1 message per poll
                pollerMetadata.setMaxMessagesPerPoll(1);
            }
            if (StringUtils.hasText(executorRef)) {
                pollerMetadata.setTaskExecutor(this.beanFactory.getBean(executorRef, TaskExecutor.class));
            }
            Trigger trigger = null;
            if (StringUtils.hasText(triggerRef)) {
                Assert.state(!StringUtils.hasText(cron) && !StringUtils.hasText(fixedDelayValue) && !StringUtils.hasText(fixedRateValue), "The '@Poller' 'trigger' attribute is mutually exclusive with other attributes.");
                trigger = this.beanFactory.getBean(triggerRef, Trigger.class);
            } else if (StringUtils.hasText(cron)) {
                Assert.state(!StringUtils.hasText(fixedDelayValue) && !StringUtils.hasText(fixedRateValue), "The '@Poller' 'cron' attribute is mutually exclusive with other attributes.");
                trigger = new CronTrigger(cron);
            } else if (StringUtils.hasText(fixedDelayValue)) {
                Assert.state(!StringUtils.hasText(fixedRateValue), "The '@Poller' 'fixedDelay' attribute is mutually exclusive with other attributes.");
                trigger = new PeriodicTrigger(Long.parseLong(fixedDelayValue));
            } else if (StringUtils.hasText(fixedRateValue)) {
                trigger = new PeriodicTrigger(Long.parseLong(fixedRateValue));
                ((PeriodicTrigger) trigger).setFixedRate(true);
            }
            // 'Trigger' can be null. 'PollingConsumer' does fallback to the 'new PeriodicTrigger(10)'.
            pollerMetadata.setTrigger(trigger);
            if (StringUtils.hasText(errorChannel)) {
                MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
                errorHandler.setDefaultErrorChannelName(errorChannel);
                errorHandler.setBeanFactory(this.beanFactory);
                pollerMetadata.setErrorHandler(errorHandler);
            }
        }
    } else {
        pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
        Assert.notNull(pollerMetadata, "No poller has been defined for Annotation-based endpoint, " + "and no default poller is available within the context.");
    }
    pollingEndpoint.setTaskExecutor(pollerMetadata.getTaskExecutor());
    pollingEndpoint.setTrigger(pollerMetadata.getTrigger());
    pollingEndpoint.setAdviceChain(pollerMetadata.getAdviceChain());
    pollingEndpoint.setMaxMessagesPerPoll(pollerMetadata.getMaxMessagesPerPoll());
    pollingEndpoint.setErrorHandler(pollerMetadata.getErrorHandler());
    if (pollingEndpoint instanceof PollingConsumer) {
        ((PollingConsumer) pollingEndpoint).setReceiveTimeout(pollerMetadata.getReceiveTimeout());
    }
    pollingEndpoint.setTransactionSynchronizationFactory(pollerMetadata.getTransactionSynchronizationFactory());
}
Also used : TaskExecutor(org.springframework.core.task.TaskExecutor) CronTrigger(org.springframework.scheduling.support.CronTrigger) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) Trigger(org.springframework.scheduling.Trigger) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Poller(org.springframework.integration.annotation.Poller) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 7 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class EnableIntegrationTests method testMetaAnnotations.

@Test
public void testMetaAnnotations() {
    assertEquals(2, this.context.getBeanNamesForType(GatewayProxyFactoryBean.class).length);
    PollingConsumer consumer = this.context.getBean("annotationTestService.annCount.serviceActivator", PollingConsumer.class);
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertSame(context.getBean("annAdvice"), TestUtils.getPropertyValue(consumer, "handler.adviceChain", List.class).get(0));
    assertEquals(1000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    consumer = this.context.getBean("annotationTestService.annCount1.serviceActivator", PollingConsumer.class);
    consumer.stop();
    assertTrue(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput1"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertSame(context.getBean("annAdvice1"), TestUtils.getPropertyValue(consumer, "handler.adviceChain", List.class).get(0));
    assertEquals(2000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    consumer = this.context.getBean("annotationTestService.annCount2.serviceActivator", PollingConsumer.class);
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertSame(context.getBean("annAdvice"), TestUtils.getPropertyValue(consumer, "handler.adviceChain", List.class).get(0));
    assertEquals(1000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    // Tests when the channel is in a "middle" annotation
    consumer = this.context.getBean("annotationTestService.annCount5.serviceActivator", PollingConsumer.class);
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput3"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertSame(context.getBean("annAdvice"), TestUtils.getPropertyValue(consumer, "handler.adviceChain", List.class).get(0));
    assertEquals(1000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    consumer = this.context.getBean("annotationTestService.annAgg1.aggregator", PollingConsumer.class);
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.discardChannelName"));
    assertEquals(1000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    assertEquals(-1L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertFalse(TestUtils.getPropertyValue(consumer, "handler.sendPartialResultOnExpiry", Boolean.class));
    consumer = this.context.getBean("annotationTestService.annAgg2.aggregator", PollingConsumer.class);
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    assertEquals(23, TestUtils.getPropertyValue(consumer, "phase"));
    assertSame(context.getBean("annInput"), TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.outputChannelName"));
    assertEquals("annOutput", TestUtils.getPropertyValue(consumer, "handler.discardChannelName"));
    assertEquals(1000L, TestUtils.getPropertyValue(consumer, "trigger.period"));
    assertEquals(75L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertTrue(TestUtils.getPropertyValue(consumer, "handler.sendPartialResultOnExpiry", Boolean.class));
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) Test(org.junit.Test)

Example 8 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class PollingTransactionTests method transactionWithCommitAndAdvices.

@Test
@SuppressWarnings("unchecked")
public void transactionWithCommitAndAdvices() throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionTests.xml", this.getClass());
    PollingConsumer advicedPoller = context.getBean("advicedSa", PollingConsumer.class);
    List<Advice> adviceChain = TestUtils.getPropertyValue(advicedPoller, "adviceChain", List.class);
    assertEquals(4, adviceChain.size());
    Runnable poller = TestUtils.getPropertyValue(advicedPoller, "poller", Runnable.class);
    Callable<?> pollingTask = TestUtils.getPropertyValue(poller, "pollingTask", Callable.class);
    assertTrue("Poller is not Advised", pollingTask instanceof Advised);
    Advisor[] advisors = ((Advised) pollingTask).getAdvisors();
    assertEquals(4, advisors.length);
    assertThat("First advisor is not TX", advisors[0].getAdvice(), instanceOf(TransactionInterceptor.class));
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    MessageChannel input = (MessageChannel) context.getBean("goodInputWithAdvice");
    PollableChannel output = (PollableChannel) context.getBean("output");
    assertEquals(0, txManager.getCommitCount());
    assertEquals(0, txManager.getRollbackCount());
    input.send(new GenericMessage<>("test"));
    txManager.waitForCompletion(10000);
    Message<?> message = output.receive(0);
    assertNotNull(message);
    assertEquals(0, txManager.getRollbackCount());
    context.close();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) Advisor(org.springframework.aop.Advisor) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) PollableChannel(org.springframework.messaging.PollableChannel) Advice(org.aopalliance.aop.Advice) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 9 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer 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 10 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer 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)

Aggregations

PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)25 Test (org.junit.Test)17 QueueChannel (org.springframework.integration.channel.QueueChannel)12 Message (org.springframework.messaging.Message)9 BeanFactory (org.springframework.beans.factory.BeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)6 MessageChannel (org.springframework.messaging.MessageChannel)6 PollableChannel (org.springframework.messaging.PollableChannel)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 ErrorMessage (org.springframework.messaging.support.ErrorMessage)5 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)4 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 SubscribableChannel (org.springframework.messaging.SubscribableChannel)4 ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)3 MessagingException (org.springframework.messaging.MessagingException)3 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2