How to handle exceptions and error in apache camel route

825 Views Asked by At

I have a file watcher route and azure blob container route. I would like to handle/throw exceptions and errors. Can someone assist me with some examples ?

I would like to handle cases like if there is any exception while connecting to blob container or while processing files from the directory.

@Override
  public void configure() {


    onException(Exception.class).handled(true)
        .setHeader(AppConstants.FILENAME , simple("${headers.camelFileName}"))
        .setHeader(AppConstants.EXCEPTION_INFO, simple("General Exception"))
        .setHeader(AppConstants.EXCEPTION_LOG, simple("${exception}"))
        .log("${exception} Exception occured while processing the file:::${in.headers.camelFileName}")
        .to("file://error")
        .end();

    onException(BlobStorageException.class).handled(true)
        .setHeader(AppConstants.CONTAINERNAME , simple("${headers.CamelAzureStorageBlobContainerName}"))
        .setHeader(AppConstants.EXCEPTION_INFO, simple("Communication Exception"))
        .setHeader(AppConstants.EXCEPTION_LOG, simple("${exception}"))
        .log("${exception} Exception occured while Connecting to Azure Blob :::${in.headers.camelFileName}")
        .to("file://error")
        .end();


  from("file-watch:hello?events=CREATE&antInclude=**/*.csv&recursive=true")
        .routeId("fileWatch")
        .log("File Consumed Name: ${header.CamelFileName}")
        .to("direct:uploadFileToBlob")
        .end();


 from("direct:uploadFileToBlob")
        .routeId("uploadFile")
        .log(LoggingLevel.INFO,"Container Name: ${header.CamelAzureStorageBlobContainerName}")
        .toD("azure-storage-blob://jdawmsdevsa/${header.CamelAzureStorageBlobContainerName}?blobName=${header.CamelFileName}&operation=uploadBlockBlob&serviceClient=#serviceClient")
        .log(LoggingLevel.INFO,"${header.CamelFileName} Uploaded to ${header.CamelAzureStorageBlobContainerName} Container Successfully")
        .end();

}

So far, I have handled the above General Exception and BlobStorage connectivity exceptions.Any suggestion to further enhance error and exception cases ?

1

There are 1 best solutions below

0
Pasi Österman On

Route scope exceptions

Instead of using global scope for all exceptions you could use route specific scope instead which allows you to handle exceptions that are for example specific to your uploadFile route.

Redelivery options

With connection errors you could also configure redelivery policy to make the route more robust against minor connectivity issues.