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

标签 java spring spring-boot spring-integration spring-integration-aws

在 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')"/>

关于java - 将重命名的文件作为输入传递到出站适配器/网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52080366/

相关文章:

spring-boot - 如何启用 SAML2 AuthNRequest 签名

java - 使用 Jetty 11 设置 websocket

java - 从restTemplate 映射到Map<String, Integer>

java - Java 贪吃蛇游戏的类未编译

java - 即使 web.xml 不包含 Spring 的 ContextLoaderListener,它又如何运行?

java - 从持久存储中加载异常 session

spring-boot - 带有 log4j2 实现的 Slf4j 异常

spring-boot - 如何从远程计算机上的jar文件运行测试?

java - 为什么我的 Android 应用程序无法从存储中读取数据?

java - 获取网络中某个目录的文件