Search in sources :

Example 6 with Trigger

use of org.springframework.scheduling.Trigger 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)

Example 7 with Trigger

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

the class EnableIntegrationTests method testAnnotatedServiceActivator.

@Test
public void testAnnotatedServiceActivator() throws Exception {
    this.serviceActivatorEndpoint.setReceiveTimeout(10000);
    this.serviceActivatorEndpoint.start();
    assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS));
    assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll"));
    Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertTrue(this.annotationTestService.isRunning());
    Log logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        pollerInterruptedLatch.countDown();
        invocation.callRealMethod();
        return null;
    }).when(logger).debug("Received no Message during the poll, returning 'false'");
    new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger);
    this.serviceActivatorEndpoint.stop();
    assertFalse(this.annotationTestService.isRunning());
    // wait until the service activator's poller is interrupted.
    assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS));
    this.serviceActivatorEndpoint.start();
    assertTrue(this.annotationTestService.isRunning());
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(CronTrigger.class));
    assertEquals("0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression"));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(11L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertSame(this.myTrigger, trigger);
    trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(10L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    this.input.send(MessageBuilder.withPayload("Foo").build());
    Message<?> interceptedMessage = this.wireTapChannel.receive(10000);
    assertNotNull(interceptedMessage);
    assertEquals("Foo", interceptedMessage.getPayload());
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());
    receive = this.wireTapFromOutput.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());
    MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    String messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.containsString("input"));
    assertThat(messageHistoryString, Matchers.containsString("annotationTestService.handle.serviceActivator.handler"));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    receive = this.publishedChannel.receive(10000);
    assertNotNull(receive);
    assertEquals("foo", receive.getPayload());
    messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input")));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    assertThat(messageHistoryString, Matchers.containsString("publishedChannel"));
    assertNull(this.wireTapChannel.receive(0));
    assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0));
    assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0));
    assertTrue(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source"));
    Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source");
    assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class));
    assertNull(this.counterChannel.receive(10));
    SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class);
    assertFalse(countSA.isAutoStartup());
    assertEquals(23, countSA.getPhase());
    countSA.start();
    for (int i = 0; i < 10; i++) {
        Message<?> message = this.counterChannel.receive(1000);
        assertNotNull(message);
        assertEquals(i + 1, message.getPayload());
    }
    Message<?> message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    assertNull(this.fooChannel.receive(10));
    message = this.messageChannel.receive(1000);
    assertNotNull(message);
    assertEquals("bar", message.getPayload());
    assertTrue(message.getHeaders().containsKey("foo"));
    assertEquals("FOO", message.getHeaders().get("foo"));
    MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel);
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()"));
    assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()"));
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
}
Also used : CronTrigger(org.springframework.scheduling.support.CronTrigger) Log(org.apache.commons.logging.Log) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) SmartLifecycle(org.springframework.context.SmartLifecycle) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) MessageHistory(org.springframework.integration.history.MessageHistory) EnableMessageHistory(org.springframework.integration.config.EnableMessageHistory) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Trigger(org.springframework.scheduling.Trigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 8 with Trigger

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

the class IntervalTriggerParserTests method testFixedRateTrigger.

@Test
public void testFixedRateTrigger() {
    Object poller = context.getBean("pollerWithFixedRateAttribute");
    assertEquals(PollerMetadata.class, poller.getClass());
    PollerMetadata metadata = (PollerMetadata) poller;
    Trigger trigger = metadata.getTrigger();
    assertEquals(PeriodicTrigger.class, trigger.getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    Boolean fixedRate = (Boolean) accessor.getPropertyValue("fixedRate");
    Long period = (Long) accessor.getPropertyValue("period");
    assertEquals(fixedRate, true);
    assertEquals(36L, period.longValue());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Example 9 with Trigger

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

the class IntervalTriggerParserTests method testFixedDelayTrigger.

@Test
public void testFixedDelayTrigger() {
    Object poller = context.getBean("pollerWithFixedDelayAttribute");
    assertEquals(PollerMetadata.class, poller.getClass());
    PollerMetadata metadata = (PollerMetadata) poller;
    Trigger trigger = metadata.getTrigger();
    assertEquals(PeriodicTrigger.class, trigger.getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(trigger);
    Boolean fixedRate = (Boolean) accessor.getPropertyValue("fixedRate");
    Long period = (Long) accessor.getPropertyValue("period");
    assertEquals(fixedRate, false);
    assertEquals(37L, period.longValue());
}
Also used : PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Trigger(org.springframework.scheduling.Trigger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) Test(org.junit.Test)

Example 10 with Trigger

use of org.springframework.scheduling.Trigger 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)

Aggregations

Trigger (org.springframework.scheduling.Trigger)17 Test (org.junit.Test)14 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)13 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)10 CronTrigger (org.springframework.scheduling.support.CronTrigger)8 TriggerContext (org.springframework.scheduling.TriggerContext)7 Date (java.util.Date)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)5 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)5 Expression (org.springframework.expression.Expression)4 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)4 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)4 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)4 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)4 ArrayList (java.util.ArrayList)3 Advice (org.aopalliance.aop.Advice)3 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)3 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)3 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)3