Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.

Message router

olegz edited this page Jun 5, 2012 · 7 revisions

EIP reference for Message Router can be found here - http://www.eaipatterns.com/MessageRouter.html

Continuation from Message Router could be expressed using '-->' operator. However since any router essentially provides a conditional split/fork in the flow, continuation from the router identifies a default Message route when non of the conditions are met.

We currently support several types of Message Routers

Root DSL element for all Message Routers is route

Header Value Router

val messageFlow =
	    route.onValueOfHeader("someHeaderName") (
	
	      when("foo") then 
	      	handle{m:Message[_] => println("Header is 'foo': " + m)}
	      ,
	      when("bar") then
	        handle{m:Message[_] => println("Header is 'bar': " + m)}
	    ) --> 
            handle{m:Message[_] => println("Header is not set: " + m)}

Payload Type Router

val messageFlow =
	    route.onPayloadType(
	
	      when(classOf[String]) then 
	      	handle{m:Message[_] => println("Payload is String: " + m)}
	      ,
	      when(classOf[Int]) then
	        handle{m:Message[_] => println("Payload is Int: " + m)}
	    ) --> 
            handle{m:Message[_] => println("Payload is: " + m.getPayload())}

Custom Router

val messageFlow =
	    route{m:Message[String] => m.getPayload}(
	
	      when("Hello") then 
	      	handle{m:Message[_] => println("Payload is Hello: " + m)}
	      ,
	      when("Bye") then
	        handle{m:Message[_] => println("Payload is Bye: " + m)}
	    ) --> 
            Channel("Hi") -->
            handle{m:Message[_] => println("Payload is: " + m.getPayload())}

If you need to add any extra properties to any Message Router you may do so using additionalAttributes method which accepts named parameters.

For example:

val messageRouterA = route. { m: Message[_] => m } additionalAttributes(name="myRouter") //infix

val messageFilterB = route { m: Message[_] => m }.additionalAttributes(name="myRouter") 

will produce a Message Router named 'myRouter'.

Properties that could be set:

name - component name

Router's apply method signature is:

def apply(function:Function1[_,String])

That is done to guard from Functions that return anything other than String to satisfy the Messaging Router contract


[Back to Core Messaging Patterns] (https://github.com/SpringSource/spring-integration-dsl-scala/wiki/Core-Messaging-Patterns)