Scala

Apache Camel – A little Scala DSL example

So we have a Scala DSL in Apache Camel for many years now, and I guess its about time I wrote a little blog entry about this (has been on my todo list for a while).

So the Scala DSL is of course using the Scala programming language which has many bells and whistles over plain Java. However the uptake of the Scala DSL is not very high, as the Java and XML DSL’s is good enough for most people.

Anyway I guess one of the nice thing about the Scala DSL would be using closures as expressions and predicates etc. So let’s do a little example using the Filter EIP pattern and use a closure as the filter predicate.

When using the Scala DSL you should use the org.apache.camel.scala.dsl.builder.RouteBuilder, which is the Scala empowered DSL.

So in the following we have a FilterRoute class where we use the Scala DSL, when we define the createMyFilterRoute function.

class FilterRoute {
  def createMyFilterRoute = new RouteBuilder {
    from("direct:start")
      .filter(_.in("gold") == "true")
        .to("mock:gold")
  }
}

As you can see from the code above, inside the scope of RouteBuilder we have the Scala DSL at our disposal. Then we use the Filter EIP which accepts a function with the Exchange as parameter, which gets defaulted into the _ symbol. The result of that function is the evaluated as a predicate, using the Scala powerful (but a bit scary) type system with implicit type converter and case matching etc.

The in(“gold”) is a function on a Exchange wrapped we have in the Scala DSL which adds additional methods on the Camel Exchange (RichExchange), the in is a function that look up a header.

To unit test this route I wanted to use the existing and powerful camel-test module. This module offers the CamelTestSupport class you can extend for your unit tests.

So the unit tests can be almost like in Java, but you would need to add the trait org.apache.camel.scala.dsl.RouteBuilderSupport which helps bridge the Scala RouteBuilder with the Java RouteBuilder, that the CamelTestSupport expects and uses. The code below shows an unit test example.

class FilterRouteTest extends CamelTestSupport with RouteBuilderSupport {

  override def createRouteBuilder() = new FilterRoute().createMyFilterRoute

  @Test
  def testFilterRouteGold() {
    getMockEndpoint("mock:gold").expectedMessageCount(1)
    template.sendBodyAndHeader("direct:start", "Hello World", "gold", "true")
    assertMockEndpointsSatisfied()
  }

  @Test
  def testFilterRouteNotGold() {
    getMockEndpoint("mock:gold").expectedMessageCount(0)
    template.sendBodyAndHeader("direct:start", "Hello World", "gold", "false")
    assertMockEndpointsSatisfied()
  }

}

As you can see we use the RouteBuilderSupport trait, and then override the createRouteBuilder function to return the Scala DSL empowered RouteBuilder we created previously. The rest of the code is standard and plain Java code with JUnit @Test annotations.

This example is provided in the source code of the camel-scala module, as part of a unit test.

If you are a Scala fan and interested in Camel as well, then the Camel community could use people who are dedicated to Scala and help with the Scala DSL. The Camel team is often busy with other issues in our lives, so we only have a bit time to have fun and play with Scala.

Reference: Apache Camel – A little Scala DSL example from our JCG partner Claus Ibsen at the Claus Ibsen riding the Apache Camel blog.

Claus Ibsen

Claus Ibsen is a principal software engineer from Red Hat. Claus is working full time as Apache Camel committer. And is author of the "Camel in Action" book.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button