Spring Integration Scala DSL Samples – HelloWorld

Spring Integration is a world class framework for integration layers in any kind of JVM based applications. It has proven wildly successful in addressing integration concerns in large legacy systems, modern application development or a combination of both. Spring Integration stands as a reference implemention of the seminal book Enterprise Integration Patterns in which the authors lay the foundation for different patterns used in integrating enterprise systems. The main advantage Spring Integration brings to the table though is that it is built on top of the popular Spring Framework and its programming model which is familiar to a multitude of Java developers.

For about two decades, Java has been the prevalent programming language used on the JVM. However, as the JVM technology is maturing into its 3rd decade and does not show any sign of exhaustion yet, new languages are emerging strongly these days to compete with Java. There are a handful of popular languages such as Scala, Groovy, Clojure, JRuby, Jython etc. which are gaining a lot of traction on JVM.

Spring Integration provides DSL’s for both Scala and Groovy, i.e. you can write your applications entirely in pure Scala or Groovy, at the same time leverage the full power of Spring Integration which is natively written in Java.

Spring Integration has a samples project at github. It has lots and lots of examples written in Java. Here, I am trying to port some of those examples completely into Scala. In this process, I will show you how easy it is to write integration applications in Scala using the DSL.

Spring Integration Java samples project has a basic helloworld application. There are 41 total lines of code including the XML configuration. I am going to write this same application in Scala using the DSL. Here is how it looks:

import org.springframework.integration.dsl.{transform, handle}

object HelloWorld extends App {

  val messageFlow = transform { payload: String => "HelloWorldDemo: Hello " + payload } -->
      handle { payload: String => println(payload)}

    messageFlow.send("World")
}

What took 41 lines of code in Java and XML, only took 6 lines in scala using the Spring Integration Scala DSL. This is a great improvement. Approximately, an 85 percetange in code reduction.

We are initiating a message flow here — Message -> Channel -> Transformer -> Service Activator.

This flow accomplishes the same thing that the Java based helloworld application does. It sends an inbound message to a channel which is a Spring Integation channel implicitly created by the DSL. The transofrming end point listens on this inbound channel and transforming the message and puts it to an output channel. The service activator end point (initiated by the handle call above) listens on the transformer output channel and handles the payload, in this case just printing this out to the console.

Here is the equivalent XML/Java code for comparison:


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd">

	<channel id="inputChannel"/>

	<channel id="outputChannel">
		<queue capacity="10"/>
	</channel>

	<service-activator input-channel="inputChannel"
	                   output-channel="outputChannel"
	                   ref="helloService"
	                   method="sayHello"/>

	<beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>

</beans:beans>

package org.springframework.integration.samples.helloworld;

public class HelloService {

	public String sayHello(String name) {
		return "Hello " + name;
	}
}

package org.springframework.integration.samples.helloworld;

import org.apache.log4j.Logger;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;

public class HelloWorldApp {

	private static Logger logger = Logger.getLogger(HelloWorldApp.class);

	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class);
		MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
		inputChannel.send(new GenericMessage<String>("World"));
		logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
	}
}

The scala code presented here can be found at this github repository here.

Leave a comment