Mule 3.5.0 - How to move a file to an 'Failed' directory and send email when an exception is thrown during processing

StackOverflow https://stackoverflow.com/questions/22117720

Question

I have Mule flow that is reading a file (csv) from an input directory and inserting records into a database. I'd like to know how to move this file to a 'failed' directory and send a notification email should an error occur in the flow. Currently my flow is as follows:

<flow name="mainFlow" doc:name="mainFlow" processingStrategy="synchronous">

    <file:inbound-endpoint path="src/test/input" moveToDirectory="src/test/backup" responseTimeout="10000" doc:name="input-file" connector-ref="inputFile"/>

    <READS FILE INTO DATABASE>        

    <catch-exception-strategy doc:name="Catch Exception Strategy">
        <file:outbound-endpoint path="src/test/error" doc:name="error-output-file"/>
    </catch-exception-strategy>

</flow>

Ideally, when the file is moved to the 'failed' directory, it has the same name as the original file with 'error' appended to beginning. I've not yet attempted the email notification as I'm unsure as to how to approach this. Any help/guidance would be greatly appreciated, thanks in advance!

Was it helpful?

Solution

The original file name is available in message properties, so you can use outputPattern="error#[header:originalFilename]" in the file outbound. If your method for reading the file content into database loses the original file content, you could set a variable to hold the content right after you have read the file, and then set the variable as payload before writing the into the error directory in the exception handler.

UPDATE: For the email part, you have several options: basically you just set your email message content as payload and then send it with Mule SMTP outbound, or some email service like Mailgun that offers a simple REST API. It is really quite simple if you just have a suitable email provider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top