Search in sources :

Example 1 with DynamicPeriodicTrigger

use of org.springframework.integration.util.DynamicPeriodicTrigger in project spring-integration by spring-projects.

the class PollerAdviceTests method testActiveIdleAdvice.

@Test
public void testActiveIdleAdvice() throws Exception {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    final CountDownLatch latch = new CountDownLatch(5);
    final LinkedList<Long> triggerPeriods = new LinkedList<Long>();
    final DynamicPeriodicTrigger trigger = new DynamicPeriodicTrigger(10);
    adapter.setSource(() -> {
        triggerPeriods.add(trigger.getPeriod());
        Message<Object> m = null;
        if (latch.getCount() % 2 == 0) {
            m = new GenericMessage<>("foo");
        }
        latch.countDown();
        return m;
    });
    SimpleActiveIdleMessageSourceAdvice toggling = new SimpleActiveIdleMessageSourceAdvice(trigger);
    toggling.setActivePollPeriod(11);
    toggling.setIdlePollPeriod(12);
    adapter.setAdviceChain(Collections.singletonList(toggling));
    adapter.setTrigger(trigger);
    configure(adapter);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    adapter.stop();
    while (triggerPeriods.size() > 5) {
        triggerPeriods.removeLast();
    }
    assertThat(triggerPeriods, contains(10L, 12L, 11L, 12L, 11L));
}
Also used : DynamicPeriodicTrigger(org.springframework.integration.util.DynamicPeriodicTrigger) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)1 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)1