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

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

}


哔哔one
浏览 151回答 1
1回答

ibeautiful

<file:outbound-gateway id="fileMover">在链中之前的这个怎么样:<integration:transformer&nbsp;expression="new&nbsp;java.io.File(payload.absolutePath&nbsp;+&nbsp;'.lock')"/>?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java