Search in sources :

Example 1 with PollSkipAdvice

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

the class PollerAdviceTests method testDefaultDontSkip.

@Test
public void testDefaultDontSkip() throws Exception {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    final CountDownLatch latch = new CountDownLatch(1);
    adapter.setSource(() -> {
        latch.countDown();
        return null;
    });
    adapter.setTrigger(new Trigger() {

        private boolean done;

        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
            Date date = done ? null : new Date(System.currentTimeMillis() + 10);
            done = true;
            return date;
        }
    });
    configure(adapter);
    List<Advice> adviceChain = new ArrayList<Advice>();
    PollSkipAdvice advice = new PollSkipAdvice();
    adviceChain.add(advice);
    adapter.setAdviceChain(adviceChain);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    adapter.stop();
}
Also used : 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) ArrayList(java.util.ArrayList) 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) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) Test(org.junit.Test)

Example 2 with PollSkipAdvice

use of org.springframework.integration.scheduling.PollSkipAdvice 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)2 Date (java.util.Date)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Advice (org.aopalliance.aop.Advice)2 Test (org.junit.Test)2 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)2 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)2 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)2 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)2 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)2 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)2 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)2 Trigger (org.springframework.scheduling.Trigger)2 TriggerContext (org.springframework.scheduling.TriggerContext)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 MessageSource (org.springframework.integration.core.MessageSource)1 SimplePollSkipStrategy (org.springframework.integration.scheduling.SimplePollSkipStrategy)1