为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哔哔one 2021-08-19 18:40:48
在 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>();@Overridepublic 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;}@Overridepublic boolean isLockable(File file) {    return file.canWrite();}
查看完整描述

1 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

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

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

?


查看完整回答
反对 回复 2021-08-19
  • 1 回答
  • 0 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信