将重命名的文件作为输入传递到出站适配器/网关



在 spring-boot-integration 应用程序中,编写了一个自定义锁柜来重命名锁定前的原始文件(fileToLock.getAbsolutePath(( + ".lock"( 和预期的锁定文件,以便任何其他实例将无法处理相同的文件。

当文件重命名时,它复制原始文件和附加文件中的内容是用文件名.lock创建的,其中包含内容和原始文件,大小为0 kb,没有内容。

出站网关将原始文件作为没有内容的输入,并将其路由到目标路径。

想知道如何重命名原始文件,或如何将重命名的文件 filename.lock 作为输入传递给出站网关/适配器。

<integration:chain id="filesOutChain" input-channel="filesOutChain">   
<file:outbound-gateway id="fileMover" 
auto-create-directory="true"
directory-expression="headers.TARGET_PATH"
mode="REPLACE">
<file:request-handler-advice-chain>
<ref bean="retryAdvice" />
</file:request-handler-advice-chain>
</file:outbound-gateway>    
<integration:gateway request-channel="filesOutchainChannel" error-channel="errorChannel"/>
</integration:chain>

自定义文件锁:

public class CustomFileLocker extends AbstractFileLockerFilter{
private final ConcurrentMap<File, FileLock> lockCache = new ConcurrentHashMap<File, FileLock>();
private final ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<File, FileChannel>();
@Override
public boolean lock(File fileToLock) {
FileChannel channel;
FileLock lock;
try {       
boolean fileRename =fileToLock.renameTo(new File(fileToLock.getAbsolutePath() + ".lock"));
if(fileRename)
{
channel = new RandomAccessFile(fileToLock, "rw").getChannel();
lock = channel.tryLock();
if (lock == null || !lock.isValid()) {  
System.out.println(" Problem in acquiring lock!!" + fileToLock.getName());
return false;
}
lockCache.put(fileToLock, lock);
channelCache.put(fileToLock, channel);     
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
@Override
public boolean isLockable(File file) {
return file.canWrite();
}
@Override
public void unlock(File fileToUnlock) {
FileLock lock = lockCache.get(fileToUnlock);
try {
if(lock!=null){
lock.release();
channelCache.get(fileToUnlock).close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

在链中的<file:outbound-gateway id="fileMover">之前怎么样:

<integration:transformer expression="new java.io.File(payload.absolutePath + '.lock')"/>

最新更新