how to move file from inbound remote directory to another remote directory using transactionSynchronizationFactory

43 Views Asked by At

I have a requirement is to read file from a FTP remote directory insert payload values into database and then move file from remote directory /in to remote directory /archive of same FTP server.

I am using spring integration @InboundChannelAdapter and able to invoke ExpressionEvaluatingTransactionSynchronizationProcessor setAfterCommitExpression. I am struggling to find a way to construct a spel parse expression to move from inbound remote directory to another.

2

There are 2 best solutions below

0
Artem Bilan On

The setAfterCommitExpression is executed against Message as a root object. That message is exactly what FtpInboundFileSynchronizingMessageSource is producing. The payload of this message is an FTPFile. There are also some additional headers:

            messageBuilder.setHeader(FileHeaders.REMOTE_HOST_PORT, uri.getHost() + ':' + uri.getPort())
                    .setHeader(FileHeaders.REMOTE_DIRECTORY, uri.getPath())
                    .setHeader(FileHeaders.REMOTE_FILE, uri.getFragment());

So, you may call some service from that expression like @myFtpMoveService.moveRemoteFile(#root) and accept in that method a Message with all the info you needed to determine the remote file, its name and directory.

0
riteshmaurya On

I created a Messaging Gateway

@MessagingGateway
public interface FtpGateway {
  @Gateway(requestChannel = "ftpChannel")
  void sendFile(File file);
}

and @ServiceActivator

 @Bean
  @ServiceActivator(inputChannel = "ftpChannel")
  public MessageHandler ftpFileTransferMessageHandler() {
    // MessageHandler config..
  }

and provided @Gateway method "@ftpGateway.sendFileToArchive(payload)") as SPEL to the setAfterCommitExpression in TransactionSynchronizationFactory

  spelParser.parseExpression("@ftpGateway.sendFileToArchive(payload)"));