Announcement. “camel-scala-extra” code has been merged into Apache Camel
camel-scala
component due to release as version 2.11, see ticket This effectively means camel-scala-extra’s end of life.
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
com.osinka.camel.scala.RouteBuilderHelper
trait is provided to ease writing RouteBuilders. To start:
class MyRouteBuilder extends RouteBuilder with RouteBuilderHelper {
override def configure {
or just import them anywhere you’d like, e.g. your test suite:
import com.osinka.camel.scala.Implicits._
“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 ofexchange.getIn.setBody(message)
val obj = exchange.in[Type]
instead ofval obj = exchange.getIn.getBody(classOf[Type])
val out: Any = exchange.out
instead ofval out: Any = exchange.getOut.getBody
val header = exchange.in("header")
instead ofval header = exchange.getIn.getHeader("header")
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 toInt
, increment by 1 and write it back to “in”. The is the same asprocess(in(classOf[Int]) { 1+ } .toIn)
or more verboseprocess(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 }
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 })
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) })
Few Camel type converters for Scala types:
- Scala
Symbol
<=>String
Immutable collections conversions:
- Scala
Iterator
<=> JavaIterator
- Scala
Iterator
<=> JavaEnumeration
- Scala
Iterable
<=> JavaIterable
- Scala
Iterable
<=> JavaCollection
- Scala
List
<=> JavaCollection
(copy occurs when converting from Java to Scala) - Scala
List
<=> JavaList
(copy occurs when converting from Java to Scala) - Scala
Set
=> JavaSet
- Scala
Map
=> JavaMap
- Scala
Seq
=> JavaList
Mutable collections conversions:
- Scala
Buffer
<=> JavaList
- Scala
Set
<=> JavaSet
- Scala
Map
<=> JavaDictionary
- Scala
Map
<=> JavaMap
- Scala
ConcurrentMap
<=> JavaConcurrentMap
- Scala
Seq
=> JavaList
Option conversions:
Option
=>Iterable
Option
=>Iterable
Option
=>List
Option
=> JavaList
Option
=> JavaCollection
Option
=> JavaIterator
Option
=> JavaIterable