java Spring 集成文件 - 在写入文件之前通过入站通道适配器读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14171283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring integration File - reading the file by inbound channel adapter before it gets written
提问by Mahendran
I am testing the SI sampleand I am encountering the following issue.
我正在测试SI 示例,但遇到以下问题。
My SI ver-1.0.4
我的 SI ver-1.0.4
For easier understanding I am pasting the contents below:
为了更容易理解,我粘贴以下内容:
fileCopyDemo-text.xml
fileCopyDemo-text.xml
<file:inbound-channel-adapter id="filesIn"
directory="file:${java.io.tmpdir}/spring-integration-samples/input" filename-pattern=".*\.xml">
<integration:poller id="poller" >
<integration:interval-trigger initial-delay="1000" interval="2000" fixed-rate="true"/>
</integration:poller>
</file:inbound-channel-adapter>
<integration:service-activator input-channel="filesIn"
output-channel="filesOut"
ref="handler"/>
<file:outbound-channel-adapter id="filesOut"
directory="file:${java.io.tmpdir}/spring-integration-samples/output"
delete-source-files="true"/>
<bean id="handler" class="org.springframework.integration.samples.filecopy.Handler"/>
FileBasedCopyTest.java
基于文件的CopyTest.java
public class FileBasedFileCopyTest {
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/fileCopyDemo-file.xml");
}
}
Handler.java
处理程序
public class Handler {
public File handleFile(File input) {
System.out.println("Copying file: " + input.getAbsolutePath());
return input;
}
}
I have another java class which is creating a file into the input folder.
我有另一个 java 类,它正在将文件创建到输入文件夹中。
Sample.java
示例.java
public static void main(String[] args) {
try {
String str = "SomeMoreTextIsHere";
File newTextFile = new File("C:/Windows/Temp/spring-integration-samples/input/thetextfile.xml");
FileWriter fw = new FileWriter(newTextFile);
fw.write(str);
fw.close();
} catch (IOException iox) {
iox.printStackTrace();
}
}
I have run the FileBasedCopyTest and it is now listening to input folder.
我已经运行 FileBasedCopyTest,它现在正在侦听输入文件夹。
QuestionI am running the Sample.java file in debug mode and the momemt
问题我正在调试模式下运行 Sample.java 文件和 momemt
FileWriter fw = new FileWriter(newTextFile);
FileWriter fw = new FileWriter(newTextFile);
is executed I am receiving the control in Handler.java, ideally I wanted to receive the control once the file is closed. How to handle this scenario?
执行我在 Handler.java 中接收控件,理想情况下我想在文件关闭后接收控件。如何处理这种情况?
PS : I have also read thisbut not sure in this case how to handle here.
PS:我也读过这个,但不确定在这种情况下如何处理。
回答by Gary Russell
This is a common problem, regardless of whether you are using Spring Integration or not.
无论您是否使用 Spring Integration,这都是一个常见问题。
A common approach (used by SI in the outbound adapter) is to write the file with a temporary name (e.g. foo.txt.writing
) and then rename it to foo.txt
after it is written.
一种常见的方法(由 SI 在出站适配器中使用)是使用临时名称(例如foo.txt.writing
)写入文件,然后foo.txt
在写入后将其重命名为。