Example usage for org.springframework.integration.endpoint SourcePollingChannelAdapter setTaskScheduler

List of usage examples for org.springframework.integration.endpoint SourcePollingChannelAdapter setTaskScheduler

Introduction

In this page you can find the example usage for org.springframework.integration.endpoint SourcePollingChannelAdapter setTaskScheduler.

Prototype

public void setTaskScheduler(TaskScheduler taskScheduler) 

Source Link

Document

Configure a TaskScheduler for those components which logic relies on the scheduled tasks.

Usage

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testInterrupted() throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);

    MessageSource<Object> ms = () -> {
        startLatch.countDown();/*w w w. ja v  a  2 s . c o m*/
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new MessagingException("Interrupted awaiting stopLatch", e);
        }
        return null;
    };

    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.setAwaitTerminationSeconds(1);
    taskScheduler.afterPropertiesSet();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);

    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
    errorHandlerLogger = spy(errorHandlerLogger);
    DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
    dfa.setPropertyValue("logger", errorHandlerLogger);
    pollingChannelAdapter.setErrorHandler(errorHandler);

    pollingChannelAdapter.setSource(ms);
    pollingChannelAdapter.setOutputChannel(new NullChannel());
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();

    Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
    adapterLogger = spy(adapterLogger);
    when(adapterLogger.isDebugEnabled()).thenReturn(true);

    dfa = new DirectFieldAccessor(pollingChannelAdapter);
    dfa.setPropertyValue("logger", adapterLogger);

    pollingChannelAdapter.start();

    assertTrue(startLatch.await(10, TimeUnit.SECONDS));
    pollingChannelAdapter.stop();

    taskScheduler.shutdown();

    verifyZeroInteractions(errorHandlerLogger);
    verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testStartSourceBeforeRunPollingTask() {
    TaskScheduler taskScheduler = mock(TaskScheduler.class);

    willAnswer(invocation -> {/*w  w w .  jav a2 s . c om*/
        Runnable task = invocation.getArgument(0);
        task.run();
        return null;
    }).given(taskScheduler).schedule(any(Runnable.class), any(Trigger.class));

    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    pollingChannelAdapter.setSource(new LifecycleMessageSource());
    pollingChannelAdapter.setMaxMessagesPerPoll(1);
    QueueChannel outputChannel = new QueueChannel();
    pollingChannelAdapter.setOutputChannel(outputChannel);
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    pollingChannelAdapter.start();

    Message<?> receive = outputChannel.receive(10_000);
    assertNotNull(receive);
    assertEquals(true, receive.getPayload());
    pollingChannelAdapter.stop();
}