1

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.

IsidIoan
  • 403
  • 2
  • 5
  • 13

1 Answers1

1

Have you tried to just remove your error handler?

The file consumer options move and moveFailed should do what you want.

When the file consumer receives an Exception, it moves the file to the moveFailed location, otherwise it is moved to the standard move location.

Looking at your routes, this should work fine. direct routes are synchronous internal calls, so Exceptions are propagated across routes.

However, your error handler catches any Exceptions (Exception.class) and also handles them (handled(true)).

Therefore, the file consumer receives no more exceptions. It assumes the processing as successful and moves the files to the standard move location.

burki
  • 6,741
  • 1
  • 15
  • 31
  • Thanks a lot for your answer. If I remove the errorhandler due to the reliability feature of the file component the exchange is being resent causing an infinite loop and again the file it is not moved to the "moveFailed" location. I have tried the move and moveFailed attributes and in the exception handler I just logged the exception so to avoid the infinite loop but the file was kept moving in the "move" location and not in the "moveFailed". I believe the moveFailed works when an error occurs while e.x reading the file and not in the subsequent steps processing sterps of the route. – IsidIoan Jan 08 '21 at 10:38