Search in sources :

Example 1 with FileTailingIdleEvent

use of org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingIdleEvent in project spring-integration by spring-projects.

the class FileTailingMessageProducerTests method testIdleEvent.

@Test
public void testIdleEvent() throws Exception {
    ApacheCommonsFileTailingMessageProducer adapter = new ApacheCommonsFileTailingMessageProducer();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.afterPropertiesSet();
    adapter.setTaskScheduler(taskScheduler);
    CountDownLatch idleCountDownLatch = new CountDownLatch(1);
    CountDownLatch fileExistCountDownLatch = new CountDownLatch(1);
    adapter.setApplicationEventPublisher(event -> {
        if (event instanceof FileTailingIdleEvent) {
            idleCountDownLatch.countDown();
        }
        if (event instanceof FileTailingEvent) {
            FileTailingEvent fileTailingEvent = (FileTailingEvent) event;
            if (fileTailingEvent.getMessage().contains("File not found")) {
                fileExistCountDownLatch.countDown();
            }
        }
    });
    File file = spy(new File(this.testDir, "foo"));
    file.delete();
    adapter.setFile(file);
    adapter.setOutputChannel(new NullChannel());
    adapter.setIdleEventInterval(10);
    adapter.afterPropertiesSet();
    adapter.start();
    boolean noFile = fileExistCountDownLatch.await(10, TimeUnit.SECONDS);
    assertTrue("file does not exist event did not emit ", noFile);
    boolean noEvent = idleCountDownLatch.await(100, TimeUnit.MILLISECONDS);
    assertFalse("event should not emit when no file exit", noEvent);
    verify(file, atLeastOnce()).exists();
    file.createNewFile();
    boolean eventRaised = idleCountDownLatch.await(10, TimeUnit.SECONDS);
    assertTrue("idle event did not emit", eventRaised);
    adapter.stop();
    file.delete();
}
Also used : FileTailingIdleEvent(org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingIdleEvent) FileTailingEvent(org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Aggregations

File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 NullChannel (org.springframework.integration.channel.NullChannel)1 FileTailingEvent (org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent)1 FileTailingIdleEvent (org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingIdleEvent)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1