#114 Aggregator pattern; tests; description
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.iluwatar.eip.aggregator;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* Sometimes in enterprise systems there is a need to group incoming data in order to process it as a whole. For example
|
||||
* you may need to gather offers and after defined number of offers has been received you would like to choose the one
|
||||
* with the best parameters.
|
||||
*
|
||||
* <p>
|
||||
* Aggregator allows you to merge messages based on defined criteria and parameters. It gathers original messages,
|
||||
* applies aggregation strategy and upon fulfilling given criteria, releasing merged messages.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Run Spring Boot application and obtain ApplicationContext
|
||||
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
|
||||
|
||||
// Get CamelContext from ApplicationContext
|
||||
CamelContext camelContext = (CamelContext) context.getBean("camelContext");
|
||||
|
||||
// Add a new routes that will handle endpoints form SplitterRoute class.
|
||||
camelContext.addRoutes(new RouteBuilder() {
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
from("{{endpoint}}").log("ENDPOINT: ${body}");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Add producer that will send test message to an entry point in WireTapRoute
|
||||
String[] stringArray = {"Test item #1", "Test item #2", "Test item #3"};
|
||||
camelContext.createProducerTemplate().sendBody("{{entry}}", stringArray);
|
||||
|
||||
SpringApplication.exit(context);
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.iluwatar.eip.aggregator.routes;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.processor.aggregate.AggregationStrategy;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Sample aggregator route definition.
|
||||
*
|
||||
* <p>
|
||||
* It consumes messages out of the <i>direct:entry</i> entry point and forwards them to <i>direct:endpoint</i>.
|
||||
* Route accepts messages containing String as a body, it aggregates the messages based on the settings and forwards
|
||||
* them as CSV to the output chanel.
|
||||
*
|
||||
* Settings for the aggregation are: aggregate until 3 messages are bundled or wait 2000ms before sending bundled
|
||||
* messages further.
|
||||
* </p>
|
||||
*
|
||||
* In this example input/output endpoints names are stored in <i>application.properties</i> file.
|
||||
*/
|
||||
@Component
|
||||
public class AggregatorRoute extends RouteBuilder {
|
||||
|
||||
@Autowired
|
||||
private MessageAggregationStrategy aggregator;
|
||||
|
||||
/**
|
||||
* Configures the route
|
||||
* @throws Exception in case of exception during configuration
|
||||
*/
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
// Main route
|
||||
from("{{entry}}").aggregate(constant(true), aggregator).completionSize(3).completionInterval(2000).to("{{endpoint}}");
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.iluwatar.eip.aggregator.routes;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.processor.aggregate.AggregationStrategy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Aggregation strategy joining bodies of messages. If message is first one <i>oldMessage</i> is null. All changes are
|
||||
* made on IN messages.
|
||||
*/
|
||||
@Component
|
||||
public class MessageAggregationStrategy implements AggregationStrategy {
|
||||
|
||||
@Override
|
||||
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
|
||||
if (oldExchange == null) {
|
||||
return newExchange;
|
||||
}
|
||||
|
||||
String in1 = (String) oldExchange.getIn().getBody();
|
||||
String in2 = (String) newExchange.getIn().getBody();
|
||||
|
||||
oldExchange.getIn().setBody(in1 + ";" + in2);
|
||||
|
||||
return oldExchange;
|
||||
}
|
||||
}
|
2
eip-aggregator/src/main/resources/application.properties
Normal file
2
eip-aggregator/src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
entry=direct:entry
|
||||
endpoint=direct:endpoint
|
Reference in New Issue
Block a user