将重命名的文件作为输入传递给出站适配器/网关 [英] Passing renamed file as input to Outbound adapter/gateway

查看:45
本文介绍了将重命名的文件作为输入传递给出站适配器/网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In spring-boot-integration app, wrote a custom locker to rename the original file before locking (fileToLock.getAbsolutePath() + ".lock") and expected lock on file so that any other instance will not be able to process the same file .

当文件重命名时,它对原始文件和附加文件的内容进行了处理,使用filename.lock创建了内容,原始文件也存在,大小为0 kb,没有内容.

When file is renaming, its coping the contents from original file and additional file is created with filename.lock with contents and original file also existing with size 0 kb without content.

出站网关以原始文件为输入,没有内容,路由到目标路径.

Outbound-gateway taking the original file as input which is without content and routing it to target path.

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

Would like to know how to rename the original file, or how to pass the renamed file filename.lock as input to Outbound gateway/adapter.

 <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();
    }
}
}

推荐答案

那之前那个 链中的这个怎么样:

How about this before that <file:outbound-gateway id="fileMover"> in the chain:

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

?

这篇关于将重命名的文件作为输入传递给出站适配器/网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆