在 spring-boot-integration 應(yīng)用程序中,編寫(xiě)了一個(gè)自定義儲(chǔ)物柜以在鎖定之前重命名原始文件(fileToLock.getAbsolutePath() + ".lock")和預(yù)期鎖定文件,以便任何其他實(shí)例將無(wú)法處理相同的文件.當(dāng)文件重命名時(shí),它對(duì)原始文件和附加文件的內(nèi)容進(jìn)行處理,并使用帶有內(nèi)容的 filename.lock 創(chuàng)建,并且原始文件也存在大小為 0 kb 的無(wú)內(nèi)容。出站網(wǎng)關(guān)將原始文件作為沒(méi)有內(nèi)容的輸入并將其路由到目標(biāo)路徑。想知道如何重命名原始文件,或者如何將重命名的文件 filename.lock 作為輸入傳遞給出站網(wǎng)關(guān)/適配器。 <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貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
<file:outbound-gateway id="fileMover">
在鏈中之前的這個(gè)怎么樣:
<integration:transformer expression="new java.io.File(payload.absolutePath + '.lock')"/>
?
添加回答
舉報(bào)
0/150
提交
取消