Wednesday 6 July 2011

Using NetBeans 7.0 to create an Apache Camel project without Spring dependency

NOTE: This article was rewritten to fix the issue of unable to create a new project based on the apache camel quickstart archetype.

Despite the conveniences offered by the Spring Framework, there are times I would rather live without it, especially for simple applications that I need to keep as lightweight as possible. To do this, the steps are below:

1) Create a new Project in NetBeans 7.0, and choose Maven -> Java Application, and click Next.

2) In Step 2 of Project Wizard, enter your project name, and amend the other settings such as Project Location, Group Id, as you deemed fit.

Once done, click Finish, and wait for the Project setup to be completed.

3) Expand your Project from the Project Tab, right-click on Dependencies folder  and select Add Dependency...



4) In the Add Dependency dialog box, enter camel-core in the Query textbox under the Search tab. Once the Search Results appear, expand org.apache.camel: camel-core folder and click on the version you require, or the latest version available (currently latest is version 2.8.0) [bundle]. Central or Local should not matter, as you should only have Local if you have downloaded it before (either directly in your project or indirectly in other projects with camel-core dependency).


     Click on the Add button.

5) Now expand the Source Packages in your project and you should be able to find App.java. Modify the main class to the code below:

    public static void main( String[] args ) throws Exception
    {
        CamelContext context = new DefaultCamelContext();

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("file:src/data?noop=true").
                    choice().
                        when(xpath("/person/city = 'London'")).to("file:target/messages/uk").
                        otherwise().to("file:target/messages/others");
            }
        });

        // start the route and let it do its work
        System.out.println("Starting");
        context.start();
        Thread.sleep(20000);
        System.out.println("Done");
        // stop the CamelContext
        context.stop();
    }

    You will need to add the necessary dependencies.

6) You can now run the application, which will read any files in your src/data folder, and parse the xml, and move the file to either the target/messages/uk or target/messages/others folder depending on whether the person -> city node contains the value 'London' or not.

    If you followed my previous blog entry 'Using NetBeans 7.0 to create a new project using Apache Camel', you should have sample message1.xml and message2.xml under your src/data folder, which you can use for testing purposes.

    
All folders here are relative to your project folder. Until my next blog, Good Luck and Good Bye!