Jones
Jones

Reputation: 13

Apache camel dynamic file moving is not working in both from() & toD() endpoints

I am developing spring boot microservice using apache camel to integrate with external systems through sftp. I am trying to move a file to different folder in both inbound & outbound methods.

String fiscalIncomingFolder = "C:/Jones/EIATesting/StorageEIA/incoming"
String fiscalProcessingFolder = "C:/Jones/EIATesting/StorageEIA/fiscal_processing"

from("direct:fiscalFolder")
      .toD("file://" + fiscalIncomingFolder + "/" +"${header.fileNameToFiscalIncoming}?move="+fiscalProcessingFolder + "/${header.fileId}.r"+"&readLock=rename")

the header are like below in the exchange

header.fileNameToFiscalIncoming = 9f0fd432-ca8e-4f17-8ecc-e278c4c1399c.s
header.fileId = abcd1234

this code is to find a file<uuid.s> in the incoming folder and move it to the processing folder and rename it with <fileId>.r.

The code is executing without any issues but the file is not moving. I can see the endpoint details in the logs as below.

2024-06-21 20:47:18,496 DEBUG [Camel (camel-1) thread #1 - JmsConsumer[RECEIVED_FISCAL_FILE]] o.a.c.p.SendDynamicProcessor [SendDynamicProcessor.java:143] Optimising toD via SendDynamicAware component: file to use static uri: file://C:/Jones/EIATesting/StorageEIA/incoming/9f0fd432-ca8e-4f17-8ecc-e278c4c1399c.s?move=C:/Jones/EIATesting/StorageEIA/fiscal_processing/${header.fileId}.r&readLock=rename

the file location in the move part is encoded. is this causing the problem.? how to resolve it please.

Upvotes: 0

Views: 64

Answers (1)

Raphi1
Raphi1

Reputation: 573

You should specify the move parameter in the from("file:...") endpoint. There you can either use move or delete. As far as I know, you can not use it in the to("file:...") endpoint. Additionally you are just specifying the target in the to("file:..."). Also a common mistake is to include the actual filename as part of the target directory. You need to add the parameter fileName for that.

You can try something like this:

String fileNameToFiscalIncoming = "9f0fd432-ca8e-4f17-8ecc-e278c4c1399c.s";
String fileId = "abcd1234";

from("file://" + fiscalIncomingFolder + "?fileName=" + fileNameToFiscalIncoming + "&delete=true")
      .to("file://" + fiscalProcessingFolder + "?fileName=" + fileId + ".r")

With this from endpoint you are reading the specific file fileNameToFiscalIncoming only from the source directory. If it was processed correctly it's being deleted. We are writing the file with the new file called fileId.r to the destination fiscalProcessingFolder with the content of the source file.

If the fileNameToFiscalIncoming or fileId is dynamic, you need to add additional logic like reading everything from the source directory and filter the relevant files for further processing.

Check also the documentation https://camel.apache.org/components/4.4.x/file-component.html

Upvotes: 0

Related Questions