Camel development series – Part 1

Hello again!

It has been a while since I wrote so I thought I’d start this year with some basic Camel development series. The main reason is that I feel one aspect lacking in the documentation is a good step by step introduction for some basic mapping. How do you go from Csv (comma separated format) to Json format? After all mapping is a big part of integration. They can be very simple from field to field mapping to extremely complicated business logic. Although personally adding any business logic to your integration code is very risky since now you have another place to keep track of your business domain.

Anyway, I am not sure how many parts this will be but we will start with basics and then add additional mapping complexity as well as error handling, and logging until we get a somewhat reasonable and stable integration. For this I am  hoping to add to the series once a week. Feel free to add suggestions or thoughts in the comments section!

Hello world

So let’s get started with the basics. All the code for this example can be found here https://github.com/SoucianceEqdamRashti/Integration/tree/master/HelloWorld

We want to build an extremely simple HTTP listener on a specific URL and port and returns a simple string when you call it. Nothing more complicated than that.

For my development environment I am using Eclipse Mars and importing the camel archetypes from Maven. I am also using Camel version 2.16.1. You can off course use another IDE if you prefer. That should not matter.

The first thing is my pom file and specifically my dependencies. Here is what I have imported:

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.16.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-blueprint</artifactId>
      <version>2.16.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-restlet</artifactId>
      <version>2.16.1</version>
      <type>bundle</type>
    </dependency>

As you can see most of it is standard camel core. The extra dependency is camel-restlet and we will use that for the HTTP communication.

So how do we start our camelcontext and create a route? Well there is the Java DSL and the blueprint dsl. One thing to bear in mind is where you want to deploy this. That determines a lot how you choose your dsl. Since I mainly work with Karaf I will select blueprint to create my beans and start the routebuilder but I will use the java dsl for writing the routes and all other code. This way, I get the best from both worlds.

This means when you start from scratch you should base your maven project on the archetype camel-blueprint.

So here is how my blueprint.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

  <bean id="HelloWorld" class="org.souciance.integration.helloworld.HelloWorld">      
 </bean>
  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <routeBuilder ref="HelloWorld" />    
    </camelContext>
</blueprint>

The main part is the bean injection where we are injecting the class HelloWorld as bean into the Camel registry. The actual class name is HelloWorld and the prefix is the package name.

Since we want to write the main code in the java dsl we need to refer to it.
Hence we use the routeBuilder tag and refer to the bean ”HelloWorld” declared earlier.

That is all the blueprint we need.

In my HelloWorld class I have written the following code:

package org.souciance.integration.helloworld;

import org.apache.camel.builder.RouteBuilder;

/**
 * A bean which we use in the route
 */
public class HelloWorld extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		// TODO Auto-generated method stub
		
		from("restlet:http://localhost:5000/hello")
		.log("request received")
		.setBody().simple("Hello to you too")
		.end();
		
	}
}

The main thing to pick up is that we are extending RouteBuilder and overriding the method configure() where all the action takes place. Inside the method we write our dsl code.

The actual dsl code is very simple. We listen on the url http://localhost:5000/hello, then we write a simple log text, and finally we set the response body to ”Hello to you too”. As you can see we are building an expression using the simple language to putting data in the exchange body. The simple expression language is very powerful and we will come back to it in more detail in other parts of the series.

That is all the code necessary.

How do you run it?

Well before deploying it you want to test that it works. The way I do in eclipse is the following:
1. Right click on the project.
2. Select Run As –> Maven Build
3. In the new build configuration as my goal I write camel:run.
4. If you no tests you can select skip tests.

Then simply run it with that build configuration.

If you run it and do a http get on that url you should see this in the console:

INFO: Starting the internal [HTTP/1.1] server on port 5000
[         Blueprint Extender: 1] BlueprintCamelContext          INFO  Route: route1 started and consuming from: Endpoint[http://localhost:5000/hello?restletMethods=GET]
[         Blueprint Extender: 1] BlueprintCamelContext          INFO  Total 1 routes, of which 1 is started.
[         Blueprint Extender: 1] BlueprintCamelContext          INFO  Apache Camel 2.16.1 (CamelContext: camel-1) started in 0.413 seconds
[              Restlet-26556173] route1                         INFO  request received
jan 31, 2016 1:32:04 EM org.restlet.engine.log.LogFilter afterHandle
INFO: 2016-01-31	13:32:04	0:0:0:0:0:0:0:1	-	-	5000	GET	/hello	-	200	16	0	16	http://localhost:5000	Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0	-

The log shows that we have started the camelcontext, listening on that url, then we receive a HTTP request, we write the log text ”request received” and return the response.

This is a simple example how to get started with Camel.

In the next series we will look more into mapping. Feel free to comment if you have any thoughts or questions.

Annonser

En reaktion på ”Camel development series – Part 1

Kommentera

Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

WordPress.com Logo

Du kommenterar med ditt WordPress.com-konto. Logga ut / Ändra )

Twitter-bild

Du kommenterar med ditt Twitter-konto. Logga ut / Ändra )

Facebook-foto

Du kommenterar med ditt Facebook-konto. Logga ut / Ändra )

Google+ photo

Du kommenterar med ditt Google+-konto. Logga ut / Ändra )

Ansluter till %s