INT-3684: Rebuild Tail Command on Restart
JIRA: https://jira.spring.io/browse/INT-3684 Conflicts: spring-integration-file/src/test/java/org/springframework/integration/file/tail/FileTailingMessageProducerTests.java Conflicts: spring-integration-file/src/test/java/org/springframework/integration/file/config/FileTailInboundChannelAdapterParserTests-context.xml spring-integration-file/src/test/java/org/springframework/integration/file/config/FileTailInboundChannelAdapterParserTests.java
This commit is contained in:
@@ -31,6 +31,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
|
||||
*
|
||||
*/
|
||||
@@ -56,6 +57,10 @@ public class OSDelegatingFileTailingMessageProducer extends FileTailingMessagePr
|
||||
}
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return this.command;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getComponentType() {
|
||||
return super.getComponentType() + " (native)";
|
||||
@@ -65,13 +70,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
|
||||
@@ -257,5 +262,4 @@ public class OSDelegatingFileTailingMessageProducer extends FileTailingMessagePr
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,11 +13,13 @@
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<int:channel id="tailErrorChannel"/>
|
||||
|
||||
<int-file:tail-inbound-channel-adapter id="default"
|
||||
channel="input"
|
||||
task-executor="exec"
|
||||
file="/tmp/baz"
|
||||
auto-startup="false"
|
||||
auto-startup="true"
|
||||
phase="123" />
|
||||
|
||||
<int-file:tail-inbound-channel-adapter id="native"
|
||||
@@ -27,7 +29,7 @@
|
||||
task-scheduler="sched"
|
||||
file-delay="456"
|
||||
file="/tmp/foo"
|
||||
auto-startup="false"
|
||||
auto-startup="true"
|
||||
delay="${empty}"
|
||||
end="${empty}"
|
||||
reopen="${empty}"
|
||||
@@ -63,7 +65,8 @@
|
||||
|
||||
<int:channel id="input" />
|
||||
|
||||
<task:executor id="exec" />
|
||||
|
||||
<task:scheduler id="sched" />
|
||||
|
||||
<bean class="org.springframework.integration.file.config.FileTailInboundChannelAdapterParserTests$Config" />
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -18,6 +18,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;
|
||||
|
||||
@@ -26,6 +28,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;
|
||||
@@ -36,6 +39,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @author Gavin Gray
|
||||
* @since 3.0
|
||||
*
|
||||
*/
|
||||
@@ -71,8 +75,12 @@ 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"));
|
||||
this.defaultAdapter.stop();
|
||||
this.defaultAdapter.setOptions("-F -n 6");
|
||||
this.defaultAdapter.start();
|
||||
assertEquals("tail -F -n 6 " + fileName, TestUtils.getPropertyValue(defaultAdapter, "command"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,7 +91,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"));
|
||||
}
|
||||
@@ -127,4 +135,14 @@ public class FileTailInboundChannelAdapterParserTests {
|
||||
}
|
||||
return absolutePath;
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
@Bean
|
||||
public TaskExecutor exec() {
|
||||
return mock(TaskExecutor.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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;
|
||||
|
||||
Reference in New Issue
Block a user