use of org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent in project spring-integration by spring-projects.
the class FileTailingMessageProducerTests method testGuts.
private void testGuts(FileTailingMessageProducerSupport adapter, String field) throws Exception {
this.adapter = adapter;
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
adapter.setTaskScheduler(taskScheduler);
final List<FileTailingEvent> events = new ArrayList<FileTailingEvent>();
adapter.setApplicationEventPublisher(event -> {
FileTailingEvent tailEvent = (FileTailingEvent) event;
logger.debug(event);
events.add(tailEvent);
});
adapter.setFile(new File(testDir, "foo"));
QueueChannel outputChannel = new QueueChannel();
adapter.setOutputChannel(outputChannel);
adapter.setTailAttemptsDelay(500);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
File file = new File(testDir, "foo");
File renamed = new File(testDir, "bar");
file.delete();
renamed.delete();
adapter.start();
waitForField(adapter, field);
FileOutputStream foo = new FileOutputStream(file);
for (int i = 0; i < 50; i++) {
foo.write(("hello" + i + "\n").getBytes());
}
foo.flush();
foo.close();
for (int i = 0; i < 50; i++) {
Message<?> message = outputChannel.receive(10000);
assertNotNull("expected a non-null message", message);
assertEquals("hello" + i, message.getPayload());
}
file.renameTo(renamed);
file = new File(testDir, "foo");
foo = new FileOutputStream(file);
if (adapter instanceof ApacheCommonsFileTailingMessageProducer) {
Thread.sleep(1000);
}
for (int i = 50; i < 100; i++) {
foo.write(("hello" + i + "\n").getBytes());
}
foo.flush();
foo.close();
for (int i = 50; i < 100; i++) {
Message<?> message = outputChannel.receive(10000);
assertNotNull("expected a non-null message", message);
assertEquals("hello" + i, message.getPayload());
assertEquals(file, message.getHeaders().get(FileHeaders.ORIGINAL_FILE));
assertEquals(file.getName(), message.getHeaders().get(FileHeaders.FILENAME));
}
assertThat(events.size(), greaterThanOrEqualTo(1));
}
use of org.springframework.integration.file.tail.FileTailingMessageProducerSupport.FileTailingEvent 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();
}
Aggregations