-
Notifications
You must be signed in to change notification settings - Fork 33
Home
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 (SI) 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.
The idea for the style of DSL you'll see below was inspired from the simple way of documenting pipes-and-filters style flows
pipe >> filter >> pipe >> filter >> ...
One should always assume the Message navigating through pipes and filters and at the moment we are experimenting with using verb representation of EIP patterns. For example instead of transformer we use transform.
channel("A") >> transform() >> channel("B")
In the above it should read Transform Message received from the "A" channel and send it to "B" channel. The bit shift operator denotes the next component in the Message flow that will be passed a Message. For those familiar with XML style of SI configuration the above configuration would look like this:
<int:channel id="A"/>
<int:transformer input-channel="A" output-channel="B" .../>
<int:channel id="B"/>
git clone git://github.com/SpringSource/spring-integration-scala.git
- Build at command-line via Maven.
- To work in Eclipse:
- It is recommended that you have the Scala IDE plugin (http://www.scala-ide.org/) installed into Eclipse. The project as checked-in has the Scala nature set, as recognized by the Scala plugin.
- Your Eclipse install should also include m2Eclipse, the Maven plugin for Eclipse.
- Import the project into Eclipse as Maven project (Import | Maven | Existing Maven Project), not as a normal Eclipse project. You
- If the Scala IDE plugin offers to add the Scala libraries to the project, you should not accept this; the libraries should be there brought in by Maven anyway.
Look at the demo.DslDemo for samples described in this page
Simple Message exchange between Point-to-Point channel (DirectChannel) and Service Activator
var integrationContext = SpringIntegrationContext()
val inputChannel = channel()
integrationContext <= {
inputChannel >>
activate.using { { 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.using("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.withExecutor(Executors.newFixedThreadPool(10))
integrationContext <= {
inputChannel >>
activate.withName("myService").using { { 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.withName("inputChannel")
val outputChannel = channel.withName("outputChannel").andQueue(5)
integrationContext <= {
inputChannel >>
activate.withName("myService").using { 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.withName("inputChannel").andExecutor
val middleChannel = channel.withName("middleChannel").andQueue(5)
val resultChannel = channel.withQueue.andName("resultChannel")
integrationContext <= {
inputChannel >>
activate.using { m: Message[String] => { m.getPayload.toUpperCase() } } >>
middleChannel >>
transform.using { "'### ' + payload.toLowerCase() + ' ###'" } >>
resultChannel
}
integrationContext.init
inputChannel.send(new GenericMessage("==> Hello from Scala"))
var outputMessage = resultChannel.receive
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.withName("inputChannel")
val middleChannel = channel.withName("middleChannel").andQueue(5)
val resultChannel = channel.withName("resultChannel").andQueue
integrationContext <= {
inputChannel >> (
transform.withName("xfmrA").using { "'From Transformer: ' + payload.toUpperCase()" } >>
middleChannel >>
transform.withName("xfmrB").using { m: Message[String] => { m.getPayload().asInstanceOf[String].toUpperCase() } } >>
resultChannel,
activate.using { m: Message[String] => { println("From Service Activator: " + m) } }
)
}
integrationContext.init
inputChannel.send(new GenericMessage("==> Hello from Scala"))
var outputMessage = resultChannel.receive
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.withName("xfmrA")... and the second with activate.using .... The Message sent to an inputChannel will be distributed to both subscribers.