Skip to content

Commit 168c9cf

Browse files
authored
Merge pull request iluwatar#655 from codinghog/master
iluwatar#173 Added new pattern - EIP Splitter
2 parents 0312392 + dd828bc commit 168c9cf

10 files changed

Lines changed: 250 additions & 0 deletions

File tree

eip-splitter/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: pattern
3+
title: EIP Splitter
4+
folder: eip-splitter
5+
permalink: /patterns/eip-splitter/
6+
categories: Enterprise integration
7+
tags:
8+
- Java
9+
- Difficulty-Intermittent
10+
- Enterprise integration
11+
---
12+
13+
## Intent
14+
It is very common in integration systems that incoming messages consists of many items bundled together. For example
15+
an invoice document contains multiple invoice lines describing transaction (quantity, name of provided
16+
service/sold goods, price etc.). Such bundled messages may not be accepted by other systems. This is where splitter
17+
pattern comes in handy. It will take the whole document, split it based on given criteria and send individual
18+
items to the endpoint.
19+
20+
![alt text](./etc/sequencer.gif "Splitter")
21+
22+
## Applicability
23+
Use the Splitter pattern when
24+
25+
* You need to split received data into smaller pieces to process them individually
26+
* You need to control the size of data batches you are able to process
27+
28+
## Credits
29+
30+
* [Gregor Hohpe, Bobby Woolf - Enterprise Integration Patterns](http://www.enterpriseintegrationpatterns.com/patterns/messaging/Sequencer.html)
31+
* [Apache Camel - Documentation](http://camel.apache.org/splitter.html)
32+

eip-splitter/etc/sequencer.gif

2.24 KB
Loading

eip-splitter/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The MIT License
4+
Copyright (c) 2014-2016 Ilkka Seppälä
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
<artifactId>eip-splitter</artifactId>
26+
<parent>
27+
<groupId>com.iluwatar</groupId>
28+
<artifactId>java-design-patterns</artifactId>
29+
<version>1.18.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.apache.camel</groupId>
40+
<artifactId>camel-core</artifactId>
41+
<version>${camel.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.apache.camel</groupId>
46+
<artifactId>camel-spring-boot</artifactId>
47+
<version>${camel.version}</version>
48+
</dependency>
49+
50+
<!-- Testing -->
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.apache.camel</groupId>
58+
<artifactId>camel-test-spring</artifactId>
59+
<version>${camel.version}</version>
60+
</dependency>
61+
62+
</dependencies>
63+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.eip.splitter;
2+
3+
import org.apache.camel.CamelContext;
4+
import org.apache.camel.builder.RouteBuilder;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ConfigurableApplicationContext;
8+
9+
/**
10+
* It is very common in integration systems that incoming messages consists of many items bundled together. For example
11+
* an invoice document contains multiple invoice lines describing transaction (quantity, name of provided
12+
* service/sold goods, price etc.). Such bundled messages may not be accepted by other systems. This is where splitter
13+
* pattern comes in handy. It will take the whole document, split it based on given criteria and send individual
14+
* items to the endpoint.
15+
*
16+
* <p>
17+
* Splitter allows you to split messages based on defined criteria. It takes original message, process it and send
18+
* multiple parts to the output channel. It is not defined if it should keep the order of items though.
19+
* </p>
20+
*
21+
*/
22+
@SpringBootApplication
23+
public class App {
24+
25+
/**
26+
* Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
27+
*
28+
* @param args command line args
29+
*/
30+
public static void main(String[] args) throws Exception {
31+
// Run Spring Boot application and obtain ApplicationContext
32+
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
33+
34+
// Get CamelContext from ApplicationContext
35+
CamelContext camelContext = (CamelContext) context.getBean("camelContext");
36+
37+
// Add a new routes that will handle endpoints form SplitterRoute class.
38+
camelContext.addRoutes(new RouteBuilder() {
39+
40+
@Override
41+
public void configure() throws Exception {
42+
from("{{endpoint}}").log("ENDPOINT: ${body}");
43+
}
44+
45+
});
46+
47+
// Add producer that will send test message to an entry point in WireTapRoute
48+
String[] stringArray = {"Test item #1", "Test item #2", "Test item #3"};
49+
camelContext.createProducerTemplate().sendBody("{{entry}}", stringArray);
50+
51+
SpringApplication.exit(context);
52+
}
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.eip.splitter.routes;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Sample splitter route definition.
8+
*
9+
* <p>
10+
* It consumes messages out of the <i>direct:entry</i> entry point and forwards them to <i>direct:endpoint</i>.
11+
* Route accepts messages having body of array or collection of objects. Splitter component split message body and
12+
* forwards single objects to the endpoint.
13+
* </p>
14+
*
15+
* In this example input/output endpoints names are stored in <i>application.properties</i> file.
16+
*/
17+
@Component
18+
public class SplitterRoute extends RouteBuilder {
19+
20+
/**
21+
* Configures the route
22+
* @throws Exception in case of exception during configuration
23+
*/
24+
@Override
25+
public void configure() throws Exception {
26+
// Main route
27+
from("{{entry}}").split().body().to("{{endpoint}}");
28+
}
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
entry=direct:entry
2+
endpoint=direct:endpoint
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.eip.splitter;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Test for App class
7+
*/
8+
public class AppTest {
9+
10+
@Test
11+
public void testMain() throws Exception {
12+
String[] args = {};
13+
App.main(args);
14+
}
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.eip.splitter.routes;
2+
3+
import org.apache.camel.EndpointInject;
4+
import org.apache.camel.Message;
5+
import org.apache.camel.ProducerTemplate;
6+
import org.apache.camel.component.mock.MockEndpoint;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
10+
import org.springframework.boot.test.SpringApplicationConfiguration;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.test.annotation.DirtiesContext;
13+
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
/**
19+
* Test class for <i>SplitterRoute</i>.
20+
* <p>
21+
* In order for it to work we have to mock endpoints we want to read/write to. To mock those we need to substitute
22+
* original endpoint names to mocks.
23+
* </p>
24+
*/
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@SpringApplicationConfiguration(classes = SplitterRouteTest.class)
27+
@ActiveProfiles("test")
28+
@EnableAutoConfiguration
29+
@ComponentScan
30+
public class SplitterRouteTest {
31+
32+
@EndpointInject(uri = "{{entry}}")
33+
private ProducerTemplate entry;
34+
35+
@EndpointInject(uri = "{{endpoint}}")
36+
private MockEndpoint endpoint;
37+
38+
/**
39+
* Test if endpoint receives three separate messages.
40+
* @throws Exception in case of en exception during the test
41+
*/
42+
@Test
43+
@DirtiesContext
44+
public void testSplitter() throws Exception {
45+
46+
// Three items in one entry message
47+
entry.sendBody(new String[] {"TEST1", "TEST2", "TEST3"});
48+
49+
// Endpoint should have three different messages in the end order of the messages is not important
50+
endpoint.expectedMessageCount(3);
51+
endpoint.assertIsSatisfied();
52+
}
53+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
entry=direct:entry
2+
endpoint=mock:endpoint

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<module>throttling</module>
149149
<module>partial-response</module>
150150
<module>eip-wire-tap</module>
151+
<module>eip-splitter</module>
151152
</modules>
152153

153154
<dependencyManagement>

0 commit comments

Comments
 (0)