Few extra extensions to Camel Scala support
Switch branches/tags
Pull request Compare This branch is 52 commits behind osinka:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.textile
pom.xml

README.textile

Extra Apache Camel methods for Scala

Despite the fact this project relies on camel-scala component from Apache Camel, it provides its own alternative way to write routes. Instead of using beta DSL from camel-scala, you use very limited choice of implicits (certainly you can always use more from “camel-scala”) to beautify ordinary Java DSL a little, but keep it mostly intact.

Basically this project provides:

  • few pimps for writing RouteBuilder
  • few Camel converters for Scala types and collections

RouteHelper

com.osinka.camel.scala.RouteBuilderHelper trait is provided to ease writing RouteBuilders. To start:

class MyRouteBuilder extends RouteBuilder with RouteBuilderHelper {
  override def configure {

Rich objects

“camel-scala” is a sub-project of Apache Camel and provides useful implicit conversions to enrich Exchange and Message objects. Unfortunately you will need to consult the project’s source code because javadoc/scaladoc is not published. Basically you can do the following and alike:

  • exchange.in = message instead of exchange.getIn.setBody(message)
  • val obj = exchange.in[Type] instead of val obj = exchange.getIn.getBody(classOf[Type])
  • val out: Any = exchange.out instead of val out: Any = exchange.getOut.getBody
  • val header = exchange.in("header") instead of val header = exchange.getIn.getHeader("header")

Processors

Many processors / filters in routes operate on only one part of Exchange, e.g. on “in” only. You can express such processor/filters with short wrappers like:

  • process(in(classOf[Int]) { 1+ }) will try to convert “in” message of the exchange to Int, increment by 1 and write it back to “in”. The is the same as process(in(classOf[Int]) { 1+ } .toIn) or more verbose process(in(classOf[Int]).by { 1+ }.toIn)
  • Otherwise, you can process “out”: process(out(classOf[Int]) {1+}) will write the result into “in”. To write it into “out”: process(out(classOf[Int]) {1+} .toOut)

You can make use of wonderful Scala "PartialFunction"s:

  • process(in(classOf[Int]) collect { case i if i % 2 == 0 => "even" })

Sometimes you just need a side-effect which does not depend on Exchange:

  • process{ cronJob.run }

Filters

Use the same DSL for filters:

  • filter(in(classOf[Int]) {0==})

You can use “PartialFunctions” for filters as well:

  • filter(in(classOf[Int]) collect { case i if i % 2 == 0 => true })

“Unit”

If your function returns Unit, you should not append .toIn or .toOut: I doubt you wanted to set the message’s body to Unit. You should not use such functions in filter as well. You will get runtime exception if you’d try to.

Otherwise it is absolutely fine to use functions with Unit return value: process(in(classOf[Int]) { x => println("x="+x) })

Converters

Few Camel type converters for Scala types:

  • Scala Symbol <=> String

Immutable collections conversions:

  • Scala Iterator <=> Java Iterator
  • Scala Iterator <=> Java Enumeration
  • Scala Iterable <=> Java Iterable
  • Scala Iterable <=> Java Collection
  • Scala List <=> Java Collection (copy occurs when converting from Java to Scala)
  • Scala List <=> Java List (copy occurs when converting from Java to Scala)
  • Scala Set => Java Set
  • Scala Map => Java Map
  • Scala Seq => Java List

Mutable collections conversions:

  • Scala Buffer <=> Java List
  • Scala Set <=> Java Set
  • Scala Map <=> Java Dictionary
  • Scala Map <=> Java Map
  • Scala ConcurrentMap <=> Java ConcurrentMap
  • Scala Seq => Java List

Option conversions:

  • Option => Iterable
  • Option => Iterable
  • Option => List
  • Option => Java List
  • Option => Java Collection
  • Option => Java Iterator
  • Option => Java Iterable