Search in sources :

Example 1 with SourcePollingChannelAdapter

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

the class SftpInboundReceiveSample method runDemo.

@Test
public void runDemo() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/SftpInboundReceiveSample-context.xml", this.getClass());
    RemoteFileTemplate<LsEntry> template = null;
    String file1 = "a.txt";
    String file2 = "b.txt";
    String file3 = "c.bar";
    new File("local-dir", file1).delete();
    new File("local-dir", file2).delete();
    try {
        PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class);
        @SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class);
        template = new RemoteFileTemplate<LsEntry>(sessionFactory);
        SftpTestUtils.createTestFiles(template, file1, file2, file3);
        SourcePollingChannelAdapter adapter = context.getBean(SourcePollingChannelAdapter.class);
        adapter.start();
        Message<?> received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received first file message: " + received);
        received = localFileChannel.receive();
        assertNotNull("Expected file", received);
        System.out.println("Received second file message: " + received);
        received = localFileChannel.receive(1000);
        assertNull("Expected null", received);
        System.out.println("No third file was received as expected");
    } finally {
        SftpTestUtils.cleanUp(template, file1, file2, file3);
        context.close();
        assertTrue("Could note delete retrieved file", new File("local-dir", file1).delete());
        assertTrue("Could note delete retrieved file", new File("local-dir", file2).delete());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) File(java.io.File) Test(org.junit.Test)

Example 2 with SourcePollingChannelAdapter

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

the class IntegrationGraphServer method pollingAdapters.

private void pollingAdapters(Collection<IntegrationNode> nodes, Collection<LinkNode> links, Map<String, MessageChannelNode> channelNodes) {
    Map<String, SourcePollingChannelAdapter> spcas = this.applicationContext.getBeansOfType(SourcePollingChannelAdapter.class);
    for (Entry<String, SourcePollingChannelAdapter> entry : spcas.entrySet()) {
        SourcePollingChannelAdapter adapter = entry.getValue();
        MessageSourceNode sourceNode = this.nodeFactory.sourceNode(entry.getKey(), adapter);
        nodes.add(sourceNode);
        producerLink(links, channelNodes, sourceNode);
    }
}
Also used : SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter)

Example 3 with SourcePollingChannelAdapter

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

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

the class InboundChannelAdapterExpressionTests method cron.

@Test
public void cron() {
    SourcePollingChannelAdapter adapter = context.getBean("cronProducer", SourcePollingChannelAdapter.class);
    assertFalse(adapter.isAutoStartup());
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    Trigger trigger = TestUtils.getPropertyValue(adapter, "trigger", Trigger.class);
    assertEquals(CronTrigger.class, trigger.getClass());
    assertEquals("7 6 5 4 3 ?", new DirectFieldAccessor(new DirectFieldAccessor(trigger).getPropertyValue("sequenceGenerator")).getPropertyValue("expression"));
    assertEquals(context.getBean("cronChannel"), adapterAccessor.getPropertyValue("outputChannel"));
    Expression expression = TestUtils.getPropertyValue(adapter, "source.expression", Expression.class);
    assertEquals("'cronTest'", expression.getExpressionString());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Test(org.junit.Test)

Example 5 with SourcePollingChannelAdapter

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

the class InboundChannelAdapterExpressionTests method fixedDelay.

@Test
public void fixedDelay() {
    SourcePollingChannelAdapter adapter = context.getBean("fixedDelayProducer", SourcePollingChannelAdapter.class);
    assertFalse(adapter.isAutoStartup());
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    Trigger trigger = TestUtils.getPropertyValue(adapter, "trigger", Trigger.class);
    assertEquals(PeriodicTrigger.class, trigger.getClass());
    DirectFieldAccessor triggerAccessor = new DirectFieldAccessor(trigger);
    assertEquals(1234L, triggerAccessor.getPropertyValue("period"));
    assertEquals(Boolean.FALSE, triggerAccessor.getPropertyValue("fixedRate"));
    assertEquals(context.getBean("fixedDelayChannel"), adapterAccessor.getPropertyValue("outputChannel"));
    Expression expression = TestUtils.getPropertyValue(adapter, "source.expression", Expression.class);
    assertEquals("'fixedDelayTest'", expression.getExpressionString());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Test(org.junit.Test)

Aggregations

SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)66 Test (org.junit.Test)58 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 Message (org.springframework.messaging.Message)15 PollableChannel (org.springframework.messaging.PollableChannel)13 Collection (java.util.Collection)10 ArrayList (java.util.ArrayList)9 QueueChannel (org.springframework.integration.channel.QueueChannel)9 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)8 Trigger (org.springframework.scheduling.Trigger)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 Expression (org.springframework.expression.Expression)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)5 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)5 File (java.io.File)4