Idan Fridman

About Idan Fridman

Idan is Software engineer with experience in Server side technologies. Idan is responsible for various infrastructure models in the software industry(Telecommunications, Finance).

Aggregating async results using Spring Integration

Hi, I came across an issue which has very nice solution using Spring Integration. Many times we have the need for a scenario of dispatching a message to unknown number of destinations. For this purpose we have the Topic methodology. But some times in addition we also want to receive answers from all destinations which received the message and aggregate them to to a single result answer. For this purpose we can use channels combined with Aggregator and ReleaseStrategy interfaces. In this post I wont concentrate on the “channels implementation”.

So let’s say we have a producer who sends it’s message to a Topic. Now we have a consumer which receives that message. Using gateway and a processor interface we can send that message in any type we want:

public interface Processor
    public void sendResponse(String response);

Consumer code:

@Override public void onMessage(Message message)
  String resultMessage = "";
    catch (Exception e)
         log.error("Error while processing message in channel consumer. errorMsg=" + e.getMessage(), e);

Now the message will be delivered to a channel(“In Channel”) We can add to this message an extra information(in case we have different message groups). After adding an extra information we dispatch that message to another channel (“Out channel”) Now here is the magic: We create two pojo’s that later will be bind to interfaces using the XML configuration. ReleaseStrategy:

public class ReleaseStrategy
	public boolean canRelease(List results)
            // check if all 5  subscribers sent responses
		if (results.size() == 5)

			return true;
		return false;


public class Aggregator
	public String aggregate(List results)
               String finalResult= "SUCCESS_RESULT";
		for (String result: results) {

			if (result.equals("ERROR_RESULT")) {
				finalResult= "ERROR_RESULT";

		return finalResult;

Basically what happens here is that after we return a “true” value via the canRelease method of ReleaseStrategy Interface the Aggregator will be able to receive the aggregated message and dispatch single result to the final destination(could be a Queue where another message consumer will get and process the result) Xml configuration:

< ?xml version="1.0" encoding="UTF-8"?>


Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

nine × 3 =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books