Sunday, 24 July 2011

Apache Camel: Email notification when files are placed in FTP folder

I love getting automated notification when events occur rather than having to perform manual checking. As an example is, instead of having to login to FTP and check whether files are uploaded to a particular FTP folder, it will indeed be much more elegant if an email notification can be sent to me automatically to inform me about that fact.

For this article, you can use the Apache Camel project created from my previous blog article. Also, let's enable Apache Camel logging so we can see debug output for ease of understanding what's going on. Verify if you already have in your project (should be in Other Sources -> src/main/resources -> <default package> folder under your project. If it is there, you might want to skip to the FTP and Email section.
To do so, first, ensure that you add dependency for slf4j-log4j12 for the appropriate version of slf4j you have (mine is 1.6.1). You can follow the instructions in my previous article to add the dependency (if you are like me, using NetBeans 7 and Maven).

Next you need to click on the Files tab in your NetBeans project, go to your project and expand to src\main, right-click and select New -> Folder. Create a folder named 'resources' (without quotes, of course).
After that, right-click on the resources folder you have just created, and select New -> Properties File. When the New Properties File dialog appear, enter log4j as the filename. The Folder should be src\main\resources (if you indeed right-clicked on the resources folder). As per screenshot below:

Enter following info into your file:

log4j.rootLogger=TRACE, out

log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
The file should be accessible from Projects tab in the following navigation under your Project folder: Other Sources -> src/main/resources -> <default package>

Next, add dependencies for both camel-mail and camel-ftp.When done, amend your routing code to:

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                    .setBody().simple("Hi, new file uploaded to ftp. Name: ${header.CamelFileName}")
                    .setHeader("subject").simple("Received filename: ${header.CamelFileName}")

The above example (fictitious account info), will check the test folder in for user: myuser. When found, it will send an email to my gmail account The sender is of the same email account. An email will be sent for every file found in the ftp account, so for testing purposes, please ensure that you do not have too many files in this folder.

You may also want to modify the call to Thread.sleep in to a longer time, e.g. 60 seconds, as below:


Next, build and run the project, and voila! You will start receiving emails when files are uploaded into the ftp folder being watched.

For this article, notice that I am using noop=true parameter for the ftp url. That will means the file will be left in that folder as it is. This is useful if you are running the program a few times and wants to be able to test without needing to re-upload files to the ftp folder.

For more options you may want to refer to the Camel FTP component documentation here.


No comments:

Post a Comment