I am using camel to read from .csv files some records, tranform them with bindy and save them in db. However , when an error occurs for example when unmanshalling or when persisting a row from the file I would like to stop the processing of the file and move it a different directory. My route is the following and I am using spring boot camel 3.7 version
onException(Exception.class)
.handled(true)
.log(LoggingLevel.INFO, "${file:name}")
.log("IOException occurred due: ${exception.message}")
.useOriginalMessage()
.toD("file://".concat(targetErrorLocation).concat("/${file:name}"))
.end();
from(buildPathUrl())
.transacted()
.log(LoggingLevel.INFO, "${file:name}")
.choice()
.when(header("CamelFileName").contains("ORDER"))
.log(LoggingLevel.INFO, "Order file")
.to("direct:orderRoute")
.when(header("CamelFileName").contains("TRANSACTIONS"))
.log(LoggingLevel.INFO, "Transaction file")
.to("direct:transactionRoute")
.when(header("CamelFileName").contains("BATCH"))
.log(LoggingLevel.INFO, "Shipment batch file")
.to("direct:shipBatchRoute")
.otherwise()
.log(LoggingLevel.INFO, "Shipment file")
.to("direct:shipmentRoute");
from("direct:orderRoute")
.log(LoggingLevel.INFO, "${body}")
.unmarshal(orderCsvDataFormat)
.log(LoggingLevel.INFO, "${file:name}")
.split(body())
.streaming()
.shareUnitOfWork()
.log(LoggingLevel.INFO, "${body}")
.to("bean:ordersService?method=persistOrder")
.end();
private String buildPathUrl() {
StringBuilder stringBuilder = new StringBuilder("file://");
stringBuilder.append(sourceLocation)
.append("?move=")
.append(targetLocation)
.append("/")
.append("${file:name}")
/* .append(AMPERSAND)
.append("bridgeErrorHandler=true")*/
.append(AMPERSAND)
.append("moveFailed=error/${file:name}")
/*.append(AMPERSAND)
.append("delete=true")*/;
return stringBuilder.toString();
}
So far when an exception occurs the processing stops but the file does not move to an error directory but it is moved to the directory where also the succesful processed files have been moved? I would appreciate any help. Thanks in advance.