java - 将重命名的文件作为输入传递给出站适配器/网关
问题描述
在 spring-boot-integration 应用程序中,编写了一个自定义储物柜以在锁定之前重命名原始文件 (fileToLock.getAbsolutePath() + ".lock") 并预期锁定文件,以便任何其他实例都无法处理相同的文件.
重命名文件时,它会使用包含内容的 filename.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')"/>
?
推荐阅读
- cobol - COBOL 中的 COMP VALUE ZERO 是什么意思?
- sql - 连接 XML 嵌套 SQL
- python - 如何使用 Celery(无第三方包)发送 Django 的密码重置邮件?
- javascript - 由于 Bootstrap,javascript GET 和 POST 方法不起作用
- sse - 用另一个字节替换一个字节
- .net-core - 在跨平台的 .NET Core 可执行文件中,如何指定本机依赖项的路径/将它们放在哪里?
- linux - 使用 AWK 将文件的第一列拆分为多列
- couchdb - 带有时间戳过滤的 CouchDB Count Reduce
- postgresql - 如何删除 pgAdmin 4 旧版本?
- php - 发布未捕获复选框响应