Search in sources :

Example 1 with SimplePollSkipStrategy

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

the class PollerAdviceTests method testSkipSimple.

@Test
public void testSkipSimple() throws Exception {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    class LocalSource implements MessageSource<Object> {

        private final CountDownLatch latch;

        private LocalSource(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public Message<Object> receive() {
            latch.countDown();
            return null;
        }
    }
    CountDownLatch latch = new CountDownLatch(1);
    adapter.setSource(new LocalSource(latch));
    class OneAndDone10msTrigger implements Trigger {

        private boolean done;

        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
            Date date = done ? null : new Date(System.currentTimeMillis() + 10);
            done = true;
            return date;
        }
    }
    adapter.setTrigger(new OneAndDone10msTrigger());
    configure(adapter);
    List<Advice> adviceChain = new ArrayList<>();
    SimplePollSkipStrategy skipper = new SimplePollSkipStrategy();
    skipper.skipPolls();
    PollSkipAdvice advice = new PollSkipAdvice(skipper);
    adviceChain.add(advice);
    adapter.setAdviceChain(adviceChain);
    adapter.afterPropertiesSet();
    adapter.start();
    assertFalse(latch.await(1, TimeUnit.SECONDS));
    adapter.stop();
    skipper.reset();
    latch = new CountDownLatch(1);
    adapter.setSource(new LocalSource(latch));
    adapter.setTrigger(new OneAndDone10msTrigger());
    adapter.start();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    adapter.stop();
}
Also used : ArrayList(java.util.ArrayList) MessageSource(org.springframework.integration.core.MessageSource) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) DynamicPeriodicTrigger(org.springframework.integration.util.DynamicPeriodicTrigger) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) Trigger(org.springframework.scheduling.Trigger) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) CompoundTrigger(org.springframework.integration.util.CompoundTrigger) TriggerContext(org.springframework.scheduling.TriggerContext) SimplePollSkipStrategy(org.springframework.integration.scheduling.SimplePollSkipStrategy) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) CompoundTriggerAdvice(org.springframework.integration.aop.CompoundTriggerAdvice) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Advice(org.aopalliance.aop.Advice) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Advice (org.aopalliance.aop.Advice)1 Test (org.junit.Test)1 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)1 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)1 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)1 MessageSource (org.springframework.integration.core.MessageSource)1 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)1 SimplePollSkipStrategy (org.springframework.integration.scheduling.SimplePollSkipStrategy)1 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)1 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)1 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)1 Trigger (org.springframework.scheduling.Trigger)1 TriggerContext (org.springframework.scheduling.TriggerContext)1 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)1