{scrollbar}

On this page, we are going to INLINE

use the Camel Java DSL for the first time to specify a route

.

And what about XML?

This tutorial uses the Java DSL, but Camel also supports a Spring XML-based syntax for specifying routes and will probably support things like a Groovy DSL in the future as well. In fact, the Maven archetype already generates an example of the XML syntax if you want to use that instead.

Using Eclipse

If you want to use an IDE like Eclipse for editing the Java source files, have a look at Using Eclipse with Maven for more information on how to do so. The code completion feature of the IDE will make it really easy to develop new routes.

Configuring camel-context.xml to use the Java DSL

We will be using the Java DSL in this tutorial, but Camel also supports a Spring XML-based syntax for specifying routes and will probably support things like a Groovy DSL in the future as well. If you look at the src/main/resources/camel-context.xml file inside the tutorial-camel-su project, you will see how the Maven archetype already generates an example of the XML syntax. We change this file to point to the package that will contain our RouteBuilder classes. When the SU is started, all RouteBuilder classes will be located and the routes in there will be activated.

xml <beans ...> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <package>org.apache.servicemix.tutorial.camel</package> </camelContext> </beans>

Coding the Route

Now, find the sample org.apache.servicemix.tutorial.camel.MyRouteBuilder class the Maven archetype has generated for us in the default src/main/java folder and add this route to send a message every 5 seconds to the configure() method.

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("timer://tutorial?fixedRate=true&delay=3000&period=10000") // 1 .setBody(constant("Hello world!")) // 2 .to("log:tutorial"); // 3 } }

What will this route do?

As you can see, the route is a mix of very specific Java methods (that make up the DSL) and URIs. Now what will this route do once we deploy it on ServiceMix? You can probably make a fair guess by just looking at the code, but let's examine it in a bit more detail:

  1. Every route in Camel starts with from(...), which takes a URI for parameter. In this case, the URI specified refers to a built-in Camel component, the timer component:
    • the timer://tutorial tells Camel to use it's built-in timer component and create a timer named tutorial
    • the fixedRate=true&delay=3000&period=10000 part after the ? in the URI are the parameters; in this case, they instruct the timer component to first wait 3 seconds and then send an exchange every 10 seconds
  2. By default, the timer component sends empty message exchanges. Because we hadn't used the mandatory Hello world! in any of our previous tutorials, we are going to use it here. *setBody(constant("Hello world!") will set the message body to the String value "Hello world!". As you can see, message exchanges in Camel can contain any data type (XML, Object, ...), including a simple String.
  3. Finally, we add a to(...) to the route, specifying where the message should be send to. We are going to use another built-in component here, the log uses Jakarta Commons Logging to log the message exchange being send to it. This URI creates a logger named tutorial.

Deploy the SA

Now, do a mvn install in your root project folder to build the SU and SA again. Afterwards, deploy the SA to ServiceMix by either:

  • copying the SA archive to the <SERVICEMIX_HOME>/hotdeploy folder
  • executing mvn jbi:projectDeploy on your SA project folder to use the Maven JBI plugin to deploy it for you

If you look at the ServiceMix console logging, you should see

INFO - Timer-4 - AutoDeploymentService - Directory: hotdeploy: Archive changed: processing tutorial-camel-sa-1.0-SNAPSHOT.jar ... INFO - Timer-4 - ServiceAssemblyLifeCycle - Starting service assembly: tutorial-camel-sa INFO - Timer-4 - ServiceUnitLifeCycle - Initializing service unit: tutorial-camel-su INFO - Timer-4 - ServiceUnitLifeCycle - Starting service unit: tutorial-camel-su INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - Timer-4 - AutoDeploymentService - Directory: hotdeploy: Finished installation of archive: tutorial-camel-sa-1.0-SNAPSHOT.jar INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] ... and one additional line here for every 10 seconds you wait

We have just created our first Camel route and deployed it to ServiceMix. Things will really start to get interesting when we are going to look at how we can send messages to other services that are exposed on the ESB from within our Camel route. But before we can do that, we will have to add a SU with some services defined in there.

Things to remember

  • Camel routes can currently be specified using XML or Java DSL, with other options (like Groovy) currently being under development.
  • By using <package/> in the camel-context.xml file, we will automatically activate all RouteBuilder implementations in that package.
  • In Java DSL, a Camel route consists of a set of easy to read (and use) methods calls. URIs are used to define the endpoints and their parameters.

Further reading



{scrollbar}
  • No labels