在我的情况下,带有nio-locker设置的入站通道适配器通过通道提供给服务激活器。服务激活器获得一个异常,说文件已锁定。
从上面的链接,没有附加Jira票,所以我不知道它是否已经解决。我在文档中看到它说nio-locker被共享,但不确定是否包括修复Windows'功能'
运行Windows 8,java 1.8.0_60,Spring Integration 4.1.5
例外:
Caused by: java.io.IOException: The process cannot access the file because another process has locked a portion of the file
XML Config分布在两个上下文文件中。
第一个上下文文件:
<file:inbound-channel-adapter id="filesIn" prevent-duplicates="true" auto-startup="false" auto-create-directory="false" channel="fileInChannel" filter="headerFilter" directory="${input.directory}">
<int:poller id="poller" fixed-delay="5000" />
<file:nio-locker/>
</file:inbound-channel-adapter>
第二个上下文文件
<int:channel id="fileInChannel">
</int:channel>
<int:service-activator input-channel="fileInChannel"
output-channel="headerEnricherInput" ref="fileHandler" />
服务激活器的片段
public Message<File> handleFile(Message<File> inputMessage) {
File input = inputMessage.getPayload();
String baseName = FilenameUtils.getBaseName(input.getName());
String headerFileName = baseName + ".xml";
File headerFile = new File(FilenameUtils.getFullPath(input
.getAbsolutePath()), headerFileName);
String transaction = new String();
try {
JAXBContext jc = JAXBContext.newInstance(HeaderFile.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
HeaderFile header = (HeaderFile) unmarshaller.unmarshal(headerFile);
System.out.println("Transaction " + header.getTransaction());
transaction = header.getTransaction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
感谢。
答案 0 :(得分:0)
请按照论坛帖子中的链接找到我的代码:
public static void readAndLogFile(File lockedFile) throws Exception {
FileLock fileLock = FileChannelCache.tryLockFor(lockedFile);
FileChannel fileChannel = fileLock.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
fileChannel.read(byteBuffer);
System.out.println("Read File " + lockedFile.getName() + " from process " + JVM_PROCESS_ID + " with content: " + new String(byteBuffer.array()));
}
如您所见,我们应该使用负责锁定的FileChannel
来读取文件内容。
在互斥文件锁定的情况下,Unix有点宽恕。但是对于Windows,我们就在那里: - )。