Code for this blog can be found on github at
https://github.com/galapagosfinch

Monday, March 19, 2012

Spring Integration's new Scala DSL

I don't usually mention Scala, and I don't get a chance to work in it anymore, but I thought I would mention that Spring Integration has released the first milestone of the Scala DSL, which makes using SI very easy and readable inside a Scala program.



The Spring Integration team has come up with a DSL where the prominent parts of the EIP become verbs in the DSL.  Take this example:

val messageFlow =
      filter.using{payload:String => payload == "hello"} -->
      transform.using { m: Message[String] => m.getPayload().toUpperCase() } -->
      handle.using { m: Message[_] => println(m) }

Notice the example's simple use of "filter", "transform", and "handle", with other verbs like "split", "aggregate", "enrich", "enrich.header", and "route" existing as well.  The DSL makes heavy use of the Pipes and Filters pattern so that flows are that much more readable.  I imagine you could create more complex flows by ending one flow with "someChannelName.send()" to feed messages into other channels.

val messageFlow =
      filter.using{payload:String => payload == "hello"} -->
      transform.using { m: Message[String] => m.getPayload().toUpperCase() } -->
      handle.using { m: Message[_] => nextFlow.send(m) }

Scala as a language can make for some very elegant code with minimal intrusion of the language itself.  Using it as a DSL makes a lot of sense.

You can find the the announcement at the SpringSource blog and the DSL Reference on the Spring website.

No comments:

Post a Comment