Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.
olegz edited this page Jun 15, 2011 · 114 revisions

Welcome to Spring Integration Scala DSL wiki!

This page shows a few working samples that should give you an idea of what the EIP and Spring Integration DSL may look like. At this point its mainly to gather feedback and ideas therefore the state of the code and the DSL may change rather quickly, but we'll try to keep this page as up-to-date as we can.

Checkout project

git clone git://github.com/SpringSource/spring-integration-scala.git
  • To work in Eclipse it is recoomended to install Scala IDE plug-in (http://www.assembla.com/wiki/show/scala-ide)
  • Import project into Eclipse as Maven project (Unfortunately this will force Maven plugin to modify .classpath file and remove entries to src/main/scala and src/test/scala). Simply do this:

cd spring-integration-scala

git checkout .classpath

Sample Usage

Simple Message exchange between Point-to-Point channel (DirectChannel) and Service Activator

var integrationContext = SpringIntegrationContext()

val inputChannel = channel("inputChannel")

integrationContext <= {
  inputChannel >>
  activate("myService"){m:Message[String] => {println(m.getPayload)}}
}
   
integrationContext.init

inputChannel.send(new GenericMessage("==> Hello from Scala"))

In the above example ServiceActivator is provided with Scala function which simply prints the payload of the Message. You can also use Spring's Expression Language (SpEL). The equivalent SpEL-based Service Activator configuration would look like this:

activate("myService"){"T(java.lang.System).out.println(payload)"}

Compare with XML

Simple Message exchange between asynchronous Point-to-Point channel (DirectChannel) and Service Activator

var integrationContext = SpringIntegrationContext()

val inputChannel = channel("inputChannel", executor(Executors.newFixedThreadPool(10)))

integrationContext <= {
	inputChannel >>
	activate("myService"){m:Message[String] => {println(m.getPayload)}}
}
   
integrationContext.init

inputChannel.send(new GenericMessage("==> Hello from Scala"))

The only difference between this sample and the previous one is that the Message Dispatcher used by the inputChannel is injected with an Executor.

Simple Message exchange with Queue Channel (channel which will buffer the messages until they are retrieved via explicit receive call)

var integrationContext = SpringIntegrationContext()

val inputChannel = channel("inputChannel")
val outputChannel = queue_channel("outputChannel", queue(5))

integrationContext <= {
	inputChannel >>
	activate("myService"){m:Message[String] => {m.getPayload.toUpperCase()}} >>
	outputChannel
}
   
integrationContext.init

inputChannel.send(new GenericMessage("==> Hello from Scala"))
var outputMessage = outputChannel.receive
println("Output Message: " + outputMessage)

In the above sample outputChannel is configured as QueueChannel with queue capacity of 5. Service Activator produces an output Message and sends it to the outputChannel. The Message is received from the outputChannel by calling receive method

Simple Message exchange with Queue Channel and Polling Consumer

var integrationContext = SpringIntegrationContext()

val inputChannel = channel("inputChannel", executor())
val outputChannel = queue_channel("outputChannel", queue(5))
val resultChannel = queue_channel("resultChannel", queue())

integrationContext <= {
	inputChannel >>
	activate("myService"){m:Message[String] => {m.getPayload.toUpperCase()}} >>
	outputChannel >>
	transform("myTransformer", poller(1000, 5)){"'### ' + payload.toLowerCase() + ' ###'"} >>
	resultChannel
}
   
integrationContext.init

inputChannel.send(new GenericMessage("==> Hello from Scala"))
var outputMessage = resultChannel.recieve
println("Output Message: " + outputMessage)

In the above sample the Message Trasformer ("myTransformer") is a Polling Consumer since its input channel is QueueChannel. It is configured with poller to poll every second and get as many as 5 messages per poll. If poller configuration is not explicitly provided, the default poller will be created.

Compare with XML

###Simple Message exchange with Publish-Subscribe-Channel

var integrationContext = SpringIntegrationContext()

val inputChannel = pub_sub_channel("inputChannel")
val middleChannel = queue_channel("middleChannel", queue(5))
val outputChannel = queue_channel("outputChannel", queue(5))

integrationContext <= {
	inputChannel >> ( 
	    
        transform("firstXfmr"){"'From Transformer: ' + payload.toUpperCase()"} >> 
        middleChannel >>
        transform(){m:Message[String] => {m.getPayload().asInstanceOf[String].toUpperCase()}} >> 
        outputChannel,
        
        activate(){m:Message[String] => {println("From Service Activator: " + m)}}
    )
}
   
integrationContext.init

inputChannel.send(new GenericMessage("==> Hello from Scala"))
var outputMessage = outputChannel.recieve
println("Output Message: " + outputMessage)

In the above sample inputChannel is a publish-subscribe-channel which takes array of subscriber configurations. The first subscriber configuration begins with transform("firstXfmr")... and the second with activate().... The Message sent to an inputChannel will be distributed to both subscribers.

Clone this wiki locally