Friday, 1 July 2011

Using NetBeans 7.0 to create a new Apache Camel project

Let's get started to develop a simple Java app that uses Apache Camel. As Maven is integrated in NetBeans 7.0, using it for Apache Camel development is a breeze, but I believe you should be able to use any IDE or even command-line for this purpose. The steps are:

1. Fire up your NetBeans 7.0, and click File -> New Project from the Main Menu.

2. Select Maven from the Categories selection and Project from Archetype from the Projects selection, as shown in screenshot below:

 

3. Click on Next, and in the Maven Archetype wizard. Under the Archetypes from remote Maven Repositories, search for camel-archetype-java (The version I am using is 2.7.2. You might be using a never version), and click on the next button. If it doesn't work for you, follow step 4. Otherwise, skip to step 5.


4. If you cannot find camel-archetype-java, you can enter it manually. Click on the 'Add' button in the Maven Archetype wizard page. A Specify Archetype Details dialog will appear. Enter details provided below, as shown in screenshot that follows:

    Group Id: org.apache.camel.archetypes
    Artifact Id: camel-archetype-java
    Version: 2.7.2

    Leave the Repository text box blank. Once done, click on the Ok button.


5. Click next on completion and you will be brought to the Name and Location page. Enter details as screenshot below (your Project Location may be different from mine):


6. Once done, click on the Finish button. It can take some time, especially this is the first time you are using maven, as it will download all the relevant jars, etc.

7. Once done, you can open up the MyRouteBuilder.java file created. That is basically the Route whose rules will be executed. The fragment of route source code is in the configure function as below:

  from("file:src/data?noop=true")
    .choice()
    .when(xpath("/person/city = 'London'"))
      .to("file:target/messages/uk")
    .otherwise()
      .to("file:target/messages/others");

This basically instructs Apache Camel to:

a) monitor for files in src/data folder (which is relative to your project folder. In my case is: d:\java\messaging\myfirst\src\data. noop=true is an option passed to the file: consumer and is used to instruct Apache Camel not to delete the file after it has finished processing.

b) Execute a choice, and using XPATH selector, select the content of the xml files, looking for person -> city element. If it is London, place the contents to target/messages/uk, otherwise, target/messages/others, again relative to your project path.

To run, right-click on the MyRouteBuilder.java in the Projects window, and select Run File. Log messages will appear in the Output tab.

You can always click on the Files tab (which by default, is to the right of the  Projects tab in the left sidebar), and navigate to the folders mentioned above to look at where the message1.xml and message2.xml in src/data are placed into the destination target/messages/uk and target/messages/others folder.

Not bad for a start, right? This is a real simple way to work, especially coupled with the Spring Framework which takes care of instantiating MyRouteBuilder. In the next article, I would like to blog about how to create a cleaner Apache Camel Routes project that will not depend on Spring.

Apache Camel

One particularly interesting open-source project that caught my eyes is Apache Camel. It is an insanely simple and powerful integration framework. An excerpt of the intro from their website:
Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.
As I do a lot of coding in Java, and my company is mainly developing a PHP-based Content Management System, I am very keen to look for ways to provide a seamless integration and simplify PHP development by allowing most scheduled and batch jobs to be run via Java.

Using Apache Camel, I can easily fetch a file from a folder, and send it via email to a specific destination. It is not to say that PHP cannot do this, but using Java, I can easily allow 10 PHP web-applications to share the same Java engine. The advantage of this is, besides email, sending of SMS, XMPP message can be transparent to PHP as well, thus less config needed, as most of the one-time config can be centralized in the Java backend.

Apache Camel also allows you to easily communicate between servers by leveraging on JMS or sockets, so if you have hundreds of PHP applications strewn across 10 servers, you can have a simple Apache Camel app installed in all 10 servers that will just watch for existence of a file and dump it to a JMS queue, available for pickup by another set of servers that will then send out the Emails or SMS.

Sounds interesting? Continue following my blog as I will add new articles that slowly introduce a few great ways on how Apache Camel can be used. 

Saturday, 25 June 2011

The paths I have taken in Software Development

I first started programming in Apple Basic, before later moving on to DOS environment. I was never a fan of QuickBasic, but I loved Turbo Basic. Next I took a stab on Turbo Pascal, some Assembly language.

During my university years, I started doing web programming and a lot of C++ programming. I used to love Microsoft COM a lot, and I loved DirectX 3. The reference counting stuff is so interesting and at that time I thought that's the greatest gift anyone can bring to the Software Development world.

During my work environment, I started using Java. Initially I hated it so much as it is so slow, we even needed a third-party Symantec JIT to make it run at an acceptable pace. Along came Java 1.2, which makes more sense, and the last piece of puzzle that convinced me to the power of Java is when I was involved with the Java back-end team where they were able to run the same piece of software in HPUX, Solaris and Windows. It wasn't a bed of roses, there are thorns too, but compared to what the C++ team had to do for the cross-platform batch processing code to run across different platforms, it was far more elegant.

So I became an avid Java fan, even until today's Java 6, I am not that convinced that Java UI can make it big, I love doing back-end processing using Java. It is so powerful and expressive enough that many things that seem impossible or complicated in other languages are so beautiful in Java. You can embed a Javascript engine with little effort if you need to, you can have access of many high-quality networking components (think Netty, Mina, Aync Http Client), it is a beautiful environment to work with for systems integration.

In fact, in one of my favourite project, I was able to develop completely on Windows XP 32-bit, and the code was deployed to Solaris 64-bit with no issues at all, not even the need to re-compile! It was a transaction proxy that handles at least 20k transactions a day, and the automated garbage collection is so helpful to make it such a stable system. Touch wood, the system has been running for 3 years continuously without needing a single application or system restart! It really cemented my confidence in Java. It might not be a perfect language, but at least for some purposes, it is The Language.