Search code examples
springspring-integrationspring-el

Spring EL File.renameTo is not working on mounted directories


I have this bean working perfectly on linux and windows, but not moving files when the source is on a mounted file system (e.g. a network drive). I know that File.renameTo is not capable to do such move instead copy and delete should be done, but within a SpelExpression, how? I've also tried registering a custom method, but it did not work. Any suggestions?

@Bean
TransactionSynchronizationFactory transactionSynchronizationFactory() {
    ExpressionParser parser = new SpelExpressionParser();
    ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
    syncProcessor.setBeanFactory(applicationContext.getAutowireCapableBeanFactory());
    syncProcessor.setAfterCommitExpression(parser.parseExpression(
            "payload.renameTo(new java.io.File(@inboundProcessedDirectory.path " +
                    " + T(java.io.File).separator + payload.name))"));
    syncProcessor.setAfterRollbackExpression(parser.parseExpression(
            "payload.renameTo(new java.io.File(@inboundFailedDirectory.path " +
                    " + T(java.io.File).separator + payload.name))"));
    return new DefaultTransactionSynchronizationFactory(syncProcessor);
}

Solution

  • T(java.nio.file.Files).move(<oldPath>, <newPath>)

    You can use T(java.io.File).toPath() to convert a File to a Path.