IsidIoan
IsidIoan

Reputation: 411

Camel Reading file and on exception move file to error folder

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.

Upvotes: 1

Views: 1383

Answers (1)

burki
burki

Reputation: 7005

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.

Upvotes: 1

Related Questions