Search in sources :

Example 36 with PeriodicTrigger

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

the class PollingLifecycleTests method ensurePollerTaskStopsForAdapterWithInterruptible.

@Test
public void ensurePollerTaskStopsForAdapterWithInterruptible() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    QueueChannel channel = new QueueChannel();
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setMaxMessagesPerPoll(-1);
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    final Runnable coughtInterrupted = mock(Runnable.class);
    MessageSource<String> source = () -> {
        try {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                latch.countDown();
            }
        } catch (InterruptedException e) {
            coughtInterrupted.run();
        }
        return new GenericMessage<String>("hello");
    };
    adapterFactory.setSource(source);
    adapterFactory.setOutputChannel(channel);
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(taskScheduler);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(3000, TimeUnit.SECONDS));
    // 
    adapter.stop();
    Thread.sleep(1000);
    Mockito.verify(coughtInterrupted, times(1)).run();
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) CountDownLatch(java.util.concurrent.CountDownLatch) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 37 with PeriodicTrigger

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

the class PollingLifecycleTests method testAdapterLifecycleIsPropagatedToMessageSource.

@Test
public void testAdapterLifecycleIsPropagatedToMessageSource() throws Exception {
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    adapterFactory.setOutputChannel(new NullChannel());
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    final AtomicBoolean startInvoked = new AtomicBoolean();
    final AtomicBoolean stopInvoked = new AtomicBoolean();
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setObject(new Lifecycle() {

        @Override
        public void start() {
            startInvoked.set(true);
        }

        @Override
        public void stop() {
            stopInvoked.set(true);
        }

        @Override
        public boolean isRunning() {
            return false;
        }
    });
    source.setMethodName("isRunning");
    adapterFactory.setSource(source);
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(this.taskScheduler);
    adapter.start();
    adapter.stop();
    assertTrue(startInvoked.get());
    assertTrue(stopInvoked.get());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) Lifecycle(org.springframework.context.Lifecycle) NullChannel(org.springframework.integration.channel.NullChannel) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 38 with PeriodicTrigger

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

the class PollingLifecycleTests method ensurePollerTaskStopsForAdapter.

@Test
public void ensurePollerTaskStopsForAdapter() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    QueueChannel channel = new QueueChannel();
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    MessageSource<String> source = spy(new MessageSource<String>() {

        @Override
        public Message<String> receive() {
            latch.countDown();
            return new GenericMessage<String>("hello");
        }
    });
    adapterFactory.setSource(source);
    adapterFactory.setOutputChannel(channel);
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(taskScheduler);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(20, TimeUnit.SECONDS));
    assertNotNull(channel.receive(100));
    adapter.stop();
    assertNull(channel.receive(1000));
    Mockito.verify(source, times(1)).receive();
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) CountDownLatch(java.util.concurrent.CountDownLatch) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 39 with PeriodicTrigger

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

the class Pollers method periodicTrigger.

private static PollerSpec periodicTrigger(long period, TimeUnit timeUnit, boolean fixedRate, long initialDelay) {
    PeriodicTrigger periodicTrigger = new PeriodicTrigger(period, timeUnit);
    periodicTrigger.setFixedRate(fixedRate);
    periodicTrigger.setInitialDelay(initialDelay);
    return new PollerSpec(periodicTrigger);
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Example 40 with PeriodicTrigger

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

the class CharacterStreamSourceTests method testEOFIntegrationTest.

@Test
public void testEOFIntegrationTest() throws Exception {
    StringReader reader = new StringReader("test");
    CharacterStreamReadingMessageSource source = new CharacterStreamReadingMessageSource(reader, -1, true);
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    CountDownLatch latch = new CountDownLatch(2);
    source.setApplicationEventPublisher(e -> {
        if (e instanceof StreamClosedEvent) {
            if (latch.getCount() == 1) {
                adapter.stop();
            }
            latch.countDown();
        }
    });
    adapter.setSource(source);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.afterPropertiesSet();
    adapter.setTaskScheduler(scheduler);
    adapter.setTrigger(new PeriodicTrigger(100));
    QueueChannel out = new QueueChannel();
    adapter.setOutputChannel(out);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    Message<?> received = out.receive(10000);
    assertNotNull(received);
    assertEquals("test", received.getPayload());
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertFalse(adapter.isRunning());
    scheduler.shutdown();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StringReader(java.io.StringReader) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Aggregations

PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)42 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)20 Before (org.junit.Before)19 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)14 Test (org.junit.Test)14 QueueChannel (org.springframework.integration.channel.QueueChannel)10 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 BeanFactory (org.springframework.beans.factory.BeanFactory)6 Message (org.springframework.messaging.Message)6 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 NullChannel (org.springframework.integration.channel.NullChannel)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 Trigger (org.springframework.scheduling.Trigger)4 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)4 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3