I really like the integration framework Apache Camel and I also like Scala a lot. This article shows the basics of this combination. It is NO introduction to Apache Camel or Scala. I created a Git project to use it as simple startup for Camel-Scala-Maven projects using just the basic Camel concepts and only a few complex Scala features (i.e. very „Java-friendly“).
Problems when Starting with this Combination
I had several problems finding good resources for starting when I first tried to combine them. I have a lot of Camel experience, but only basic Scala knowledge. I tried to start with some projects which where already available in the web (e.g. from the „Camel in Action“ book or other Git examples).
I could not get them ready to run in IntelliJ IDEA (and Eclipse Scala plugin is still not really stable, at least on my Mac). The examples either used way too many Scala features (and thus contained very complex source code for a Scala beginner), were too basic (here I mean the camel-scala archetype) or did not work because of problems with IDE support or version problems with Maven, Scala, and so on.
Nevertheless, it is also tough to create a Camel project (and even more a Camel Scala project) from scratch without Maven because of so many dependencies. Thus, I created my own „getting started“ project with current versions of Camel (2.7.1) and Scala (2.9). Of course, you can change the version in the maven configuration.
You can download the whole project from Github at https://github.com/megachucky/MavenCamelScala. If you wanna use my project you have to either use Intellij IDEA or uncomment some configuration stuff in the pom.xml file to use it within Eclipse.
Camel and Scala Concepts shown in this Example
This example just shows the most important Camel features for getting started (all in very simple forms) – this includes Routes, Processors, Beans, CamelContext, ErrorHandling and Testing. Besides, only some advanced Scala features are used. This project is easy to understand and extendable to play around with this combination. In the following, only some code snippets will be shown, please download the project at Git if you want to see everything.
You use the CamelContext as you do in Java.
Scala Routes extend the Java class „RouteBuilder“ as Java Routes do, but they also use several features of Scala. Shorter Scala syntax is implicitly used (no semicolons, dots and parantheses, no constructor). Implicit conversions are another advanced Scala feature. For example, you do not have to use a „from“-method as you have to in Java. Overwriting operators such as „+“ or creating new operators such as „–>“ is another Scala feature shown here.
Processors and Beans
Here you can reduce the lines of code a lot by just using Scala source code instead of Java source code. Nevertheless, you can still call other Java libraries as you did until now.
By the way: You could also use a closure to implement the „process“-method and then assign it to a variable.
You simply use the CamelTestSupport API from the Java DSL. Besides, you can mix in further traits. In this example, traits are used to include the testing framework ScalaTest. Traits are very powerful for adding more functionality!
In the Git source code, there is one more example, where traits are used to add default error handling to a route (therefore the route extends the „ErrorLog“ trait).
Advantages of using Camel combined with Scala
There are two main advantages. You can reduce the lines of code a lot. This reduces efforts in time to write code, but also in maintenance (code analysis and refactoring of code). You also have several powerful features which are offered by the Scala language, e.g. mixing in more than one trait.
Nevertheless, you can still use all of your Java libraries (as you have seen with the Camel classes which are used within the Scala code).
This article showed the basic Camel concepts realized with Scala. Be aware that not all Enterprise Integration Patterns (EIP) are implemented in Scala DSL yet, but this will happen soon, hopefully. The available documentation and code examples is also still not very comprehensive, maybe this article will help some people for getting started. Despite of these concerns, you should really try out this combination, because both Apache Camel and Scala have a promising future in the JVM environment! My final conclusion: Apache Camel combined with Scala is a very powerful combination.
Kai Wähner (Twitter: @KaiWaehner)