Search in sources :

Example 1 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-cloud-stream by spring-cloud.

the class DefaultPollerProperties method getPollerMetadata.

public PollerMetadata getPollerMetadata() {
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(this.fixedDelay));
    pollerMetadata.setMaxMessagesPerPoll(this.maxMessagesPerPoll);
    return pollerMetadata;
}
Also used : PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 2 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata 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 3 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-integration by spring-projects.

the class IntervalTriggerParserTests method testFixedRateTrigger.

@Test
public void testFixedRateTrigger() {
    Object poller = context.getBean("pollerWithFixedRateAttribute");
    assertEquals(PollerMetadata.class, poller.getClass());
    PollerMetadata metadata = (PollerMetadata) poller;
    Trigger trigger = metadata.getTrigger();
    assertEquals(PeriodicTrigger.class, trigger.getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    Boolean fixedRate = (Boolean) accessor.getPropertyValue("fixedRate");
    Long period = (Long) accessor.getPropertyValue("period");
    assertEquals(fixedRate, true);
    assertEquals(36L, period.longValue());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Example 4 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-integration by spring-projects.

the class IntervalTriggerParserTests method testFixedDelayTrigger.

@Test
public void testFixedDelayTrigger() {
    Object poller = context.getBean("pollerWithFixedDelayAttribute");
    assertEquals(PollerMetadata.class, poller.getClass());
    PollerMetadata metadata = (PollerMetadata) poller;
    Trigger trigger = metadata.getTrigger();
    assertEquals(PeriodicTrigger.class, trigger.getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    Boolean fixedRate = (Boolean) accessor.getPropertyValue("fixedRate");
    Long period = (Long) accessor.getPropertyValue("period");
    assertEquals(fixedRate, false);
    assertEquals(37L, period.longValue());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Example 5 with PollerMetadata

use of org.springframework.integration.scheduling.PollerMetadata in project spring-integration by spring-projects.

the class PollerParserTests method pollerWithAdviceChain.

@Test
public void pollerWithAdviceChain() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("pollerWithAdviceChain.xml", PollerParserTests.class);
    Object poller = context.getBean("poller");
    assertNotNull(poller);
    PollerMetadata metadata = (PollerMetadata) poller;
    assertNotNull(metadata.getAdviceChain());
    assertEquals(4, metadata.getAdviceChain().size());
    assertSame(context.getBean("adviceBean1"), metadata.getAdviceChain().get(0));
    assertEquals(TestAdviceBean.class, metadata.getAdviceChain().get(1).getClass());
    assertEquals(2, ((TestAdviceBean) metadata.getAdviceChain().get(1)).getId());
    assertSame(context.getBean("adviceBean3"), metadata.getAdviceChain().get(2));
    Advice txAdvice = metadata.getAdviceChain().get(3);
    assertEquals(TransactionInterceptor.class, txAdvice.getClass());
    TransactionAttributeSource transactionAttributeSource = ((TransactionInterceptor) txAdvice).getTransactionAttributeSource();
    assertEquals(NameMatchTransactionAttributeSource.class, transactionAttributeSource.getClass());
    @SuppressWarnings("rawtypes") HashMap nameMap = TestUtils.getPropertyValue(transactionAttributeSource, "nameMap", HashMap.class);
    assertEquals(1, nameMap.size());
    assertEquals("{*=PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,readOnly}", nameMap.toString());
    context.close();
}
Also used : NameMatchTransactionAttributeSource(org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource) TransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HashMap(java.util.HashMap) Advice(org.aopalliance.aop.Advice) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Aggregations

PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)15 Test (org.junit.Test)12 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)12 Trigger (org.springframework.scheduling.Trigger)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Advice (org.aopalliance.aop.Advice)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3 Lifecycle (org.springframework.context.Lifecycle)3 MessagePublishingErrorHandler (org.springframework.integration.channel.MessagePublishingErrorHandler)3 NullChannel (org.springframework.integration.channel.NullChannel)3 SourcePollingChannelAdapterFactoryBean (org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean)3 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)3 Message (org.springframework.messaging.Message)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2