INT-3684: Rebuild Tail Command on Restart

JIRA: https://jira.spring.io/browse/INT-3684
This commit is contained in:
Gavin Gray
2015-03-21 10:33:17 -04:00
committed by Gary Russell
parent a60a8733cc
commit 0d4c839331
4 changed files with 66 additions and 10 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* Default options are "-F -n 0" (follow file name, no existing records).
*
* @author Gary Russell
* @author Gavin Gray
* @since 3.0
*
*/
@@ -57,6 +58,10 @@ public class OSDelegatingFileTailingMessageProducer extends FileTailingMessagePr
}
}
public String getCommand() {
return this.command;
}
@Override
public String getComponentType() {
return super.getComponentType() + " (native)";
@@ -71,13 +76,13 @@ public class OSDelegatingFileTailingMessageProducer extends FileTailingMessagePr
protected void onInit() {
Assert.notNull(getFile(), "File cannot be null");
super.onInit();
this.command = "tail " + this.options + " " + this.getFile().getAbsolutePath();
}
@Override
protected void doStart() {
super.doStart();
destroyProcess();
this.command = "tail " + this.options + " " + this.getFile().getAbsolutePath();
this.getTaskExecutor().execute(new Runnable() {
@Override
@@ -263,5 +268,4 @@ public class OSDelegatingFileTailingMessageProducer extends FileTailingMessagePr
}
}
}

View File

@@ -19,7 +19,7 @@
channel="input"
task-executor="exec"
file="/tmp/baz"
auto-startup="false"
auto-startup="true"
phase="123"
error-channel="tailErrorChannel"/>
@@ -30,7 +30,7 @@
task-scheduler="sched"
file-delay="456"
file="/tmp/foo"
auto-startup="false"
auto-startup="true"
delay="${empty}"
end="${empty}"
reopen="${empty}"
@@ -66,7 +66,8 @@
<int:channel id="input" />
<task:executor id="exec" />
<task:scheduler id="sched" />
<bean class="org.springframework.integration.file.config.FileTailInboundChannelAdapterParserTests$Config" />
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ package org.springframework.integration.file.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.io.File;
@@ -27,6 +29,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.file.tail.ApacheCommonsFileTailingMessageProducer;
import org.springframework.integration.file.tail.OSDelegatingFileTailingMessageProducer;
@@ -39,6 +42,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @author Artem Bilan
* @author Gavin Gray
* @since 3.0
*/
@ContextConfiguration
@@ -76,9 +80,13 @@ public class FileTailInboundChannelAdapterParserTests {
assertEquals("/tmp/baz", normalizedName);
assertEquals("tail -F -n 0 " + fileName, TestUtils.getPropertyValue(defaultAdapter, "command"));
assertSame(exec, TestUtils.getPropertyValue(defaultAdapter, "taskExecutor"));
assertFalse(TestUtils.getPropertyValue(defaultAdapter, "autoStartup", Boolean.class));
assertTrue(TestUtils.getPropertyValue(defaultAdapter, "autoStartup", Boolean.class));
assertEquals(123, TestUtils.getPropertyValue(defaultAdapter, "phase"));
assertSame(this.tailErrorChannel, TestUtils.getPropertyValue(defaultAdapter, "errorChannel"));
this.defaultAdapter.stop();
this.defaultAdapter.setOptions("-F -n 6");
this.defaultAdapter.start();
assertEquals("tail -F -n 6 " + fileName, TestUtils.getPropertyValue(defaultAdapter, "command"));
}
@Test
@@ -89,7 +97,7 @@ public class FileTailInboundChannelAdapterParserTests {
assertEquals("tail -F -n 6 " + fileName, TestUtils.getPropertyValue(nativeAdapter, "command"));
assertSame(exec, TestUtils.getPropertyValue(nativeAdapter, "taskExecutor"));
assertSame(sched, TestUtils.getPropertyValue(nativeAdapter, "taskScheduler"));
assertFalse(TestUtils.getPropertyValue(nativeAdapter, "autoStartup", Boolean.class));
assertTrue(TestUtils.getPropertyValue(nativeAdapter, "autoStartup", Boolean.class));
assertEquals(123, TestUtils.getPropertyValue(nativeAdapter, "phase"));
assertEquals(456L, TestUtils.getPropertyValue(nativeAdapter, "tailAttemptsDelay"));
}
@@ -133,4 +141,14 @@ public class FileTailInboundChannelAdapterParserTests {
}
return absolutePath;
}
public static class Config {
@Bean
public TaskExecutor exec() {
return mock(TaskExecutor.class);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.mock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -42,6 +43,7 @@ import org.springframework.messaging.Message;
/**
* @author Gary Russell
* @author Gavin Gray
* @since 3.0
*
*/
@@ -90,6 +92,37 @@ public class FileTailingMessageProducerTests {
testGuts(adapter, "tailer");
}
@Test
@TailAvailable
public void canRecalculateCommandWhenFileOrOptionsChanged() throws IOException {
File firstFile = File.createTempFile("first", ".txt");
String firstOptions = "-f options";
File secondFile = File.createTempFile("second", ".txt");
String secondOptions = "-f newoptions";
OSDelegatingFileTailingMessageProducer adapter = new OSDelegatingFileTailingMessageProducer();
adapter.setFile(firstFile);
adapter.setOptions(firstOptions);
adapter.setOutputChannel(new QueueChannel());
adapter.setTailAttemptsDelay(500);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
adapter.start();
assertEquals("tail " + firstOptions + " " + firstFile.getAbsolutePath(), adapter.getCommand());
adapter.stop();
adapter.setFile(secondFile);
adapter.start();
assertEquals("tail " + firstOptions + " " + secondFile.getAbsolutePath(), adapter.getCommand());
adapter.stop();
adapter.setOptions(secondOptions);
adapter.start();
assertEquals("tail " + secondOptions + " " + secondFile.getAbsolutePath(), adapter.getCommand());
adapter.stop();
}
private void testGuts(FileTailingMessageProducerSupport adapter, String field)
throws Exception {
this.adapter = adapter;