-
Notifications
You must be signed in to change notification settings - Fork 33
Home
Welcome to Spring Integration Scala DSL wiki!
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)"}
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.
###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