Categories: EAIJava / JEE

Apache Camel and Scala: A Powerful Combination

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.

CamelContext

You use the CamelContext as you do in Java.

Routes

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.

Testing

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).

Conclusion

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.

Best regards,

Kai Wähner (Twitter: @KaiWaehner)

Kai Waehner

builds cloud-native event streaming infrastructures for real-time data processing and analytics

View Comments

  • Can't tell you how much I look forward to using your demo as a baseline but straight out of the box doing maven build in IntelliJ got two errors:

    [ERROR] error: error while loading AnnotatedElement, class file 'C:\Java\jdk1.8.0_131\jre\lib\rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
    [INFO] (bad constant pool tag 15 at byte 2713)
    [ERROR] error: error while loading CharSequence, class file 'C:\Java\jdk1.8.0_131\jre\lib\rt.jar(java/lang/CharSequence.class)' is broken
    [INFO] (bad constant pool tag 15 at byte 1501)
    [ERROR] two errors found

    Any clarification would be very much appreciated

  • Oh sorry, I did not write any Scala code for a few years now. Cannot help anymore with these kind of issues. Hope you will find out by yourself or a forum.

Recent Posts

How Siemens Healthineers Leverages Data Streaming with Apache Kafka and Flink in Manufacturing and Healthcare

Siemens Healthineers, a global leader in medical technology, delivers solutions that improve patient outcomes and…

1 day ago

My Road to Lufthansa HON Circle Status in 2025

Discover my journey to achieving Lufthansa HON Circle (Miles & More) status in 2025. Learn…

5 days ago

The Data Streaming Landscape 2025

Data streaming is a new software category. It has grown from niche adoption to becoming…

2 weeks ago

Top Trends for Data Streaming with Apache Kafka and Flink in 2025

Apache Kafka and Apache Flink are leading open-source frameworks for data streaming that serve as…

2 weeks ago

Data Streaming in Healthcare and Pharma: Use Cases and Insights from Cardinal Health

This blog delves into Cardinal Health’s journey, exploring how its event-driven architecture and data streaming…

3 weeks ago

A New Era in Dynamic Pricing: Real-Time Data Streaming with Apache Kafka and Flink

In the age of digitization, the concept of pricing is no longer fixed or manual.…

1 month ago