Git Product home page Git Product logo

camel-scala-extra's Introduction

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

camel-scala-extra's People

Contributors

alaz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.