Skip to content

Commit 41710cb

Browse files
authored
Merge pull request iluwatar#651 from codinghog/master
iluwatar#176 Added new pattern - EIP Wire Tap
2 parents dfb4329 + 04dd93f commit 41710cb

10 files changed

Lines changed: 260 additions & 1 deletion

File tree

eip-wire-tap/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout: pattern
3+
title: EIP Wire Tap
4+
folder: eip-wire-tap
5+
permalink: /patterns/eip-wire-tap/
6+
categories: Enterprise integration
7+
tags:
8+
- Java
9+
- Difficulty-Intermittent
10+
- Enterprise integration
11+
---
12+
13+
## Intent
14+
In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
15+
by intercepting the message and redirecting it to a different location like console, filesystem or the database.
16+
It is important that such functionality should not modify the original message and influence the processing path.
17+
18+
![alt text](./etc/wiretap.gif "Wire Tap")
19+
20+
## Applicability
21+
Use the Wire Tap pattern when
22+
23+
* You need to monitor messages flowing through the system
24+
* You need to redirect the same, unchanged message to two different endpoints/paths
25+
26+
## Credits
27+
28+
* [Gregor Hohpe, Bobby Woolf - Enterprise Integration Patterns](http://www.enterpriseintegrationpatterns.com/patterns/messaging/WireTap.html)
29+
* [Apache Camel - Documentation](http://camel.apache.org/wire-tap.html)

eip-wire-tap/etc/wiretap.gif

3 KB
Loading

eip-wire-tap/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-wire-tap</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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.iluwatar.eip.wiretap;
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+
* In most integration cases there is a need to monitor the messages flowing through the system. It is usually achieved
11+
* by intercepting the message and redirecting it to a different location like console, filesystem or the database.
12+
* It is important that such functionality should not modify the original message and influence the processing path.
13+
*
14+
* <p>
15+
* Wire Tap allows you to route messages to a separate location while they are being forwarded to the ultimate
16+
* destination. It basically consumes messages of the input channel and publishes the unmodified message to both
17+
* output channels.
18+
* </p>
19+
*/
20+
@SpringBootApplication
21+
public class App {
22+
23+
/**
24+
* Program entry point. It starts Spring Boot application and using Apache Camel it auto-configures routes.
25+
*
26+
* @param args command line args
27+
*/
28+
public static void main(String[] args) throws Exception {
29+
// Run Spring Boot application and obtain ApplicationContext
30+
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
31+
32+
// Get CamelContext from ApplicationContext
33+
CamelContext camelContext = (CamelContext) context.getBean("camelContext");
34+
35+
// Add a new routes that will handle endpoints form WireTapRoute class.
36+
camelContext.addRoutes(new RouteBuilder() {
37+
38+
@Override
39+
public void configure() throws Exception {
40+
from("{{endpoint}}").log("ENDPOINT: ${body}");
41+
from("{{wireTapEndpoint}}").log("WIRETAPPED ENDPOINT: ${body}");
42+
}
43+
44+
});
45+
46+
// Add producer that will send test message to an entry point in WireTapRoute
47+
camelContext.createProducerTemplate().sendBody("{{entry}}", "Test message");
48+
49+
SpringApplication.exit(context);
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar.eip.wiretap.routes;
2+
3+
import org.apache.camel.builder.RouteBuilder;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Sample wire tap 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+
* Wire Tap intercepts the message and sends it to <i>direct:wireTap</i>, which in turn forwards it to
12+
* <i>direct:wireTapEndpoint</i>.
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 WireTapRoute 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}}").wireTap("direct:wireTap").to("{{endpoint}}");
28+
29+
// Wire tap route
30+
from("direct:wireTap").log("Message: ${body}").to("{{wireTapEndpoint}}");
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
entry=direct:entry
2+
endpoint=direct:endpoint
3+
wireTapEndpoint=direct:wireTapEndpoint
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.eip.wiretap;
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.iluwatar.eip.wiretap.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>WireTapRoute</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 = WireTapRouteTest.class)
27+
@ActiveProfiles("test")
28+
@EnableAutoConfiguration
29+
@ComponentScan
30+
public class WireTapRouteTest {
31+
32+
@EndpointInject(uri = "{{entry}}")
33+
private ProducerTemplate entry;
34+
35+
@EndpointInject(uri = "{{endpoint}}")
36+
private MockEndpoint endpoint;
37+
38+
@EndpointInject(uri = "{{wireTapEndpoint}}")
39+
private MockEndpoint wireTapEndpoint;
40+
41+
/**
42+
* Test if both endpoints receive exactly one message containing the same, unchanged body.
43+
* @throws Exception in case of en exception during the test
44+
*/
45+
@Test
46+
@DirtiesContext
47+
public void testWireTap() throws Exception {
48+
entry.sendBody("TEST");
49+
50+
endpoint.expectedMessageCount(1);
51+
wireTapEndpoint.expectedMessageCount(1);
52+
53+
endpoint.assertIsSatisfied();
54+
wireTapEndpoint.assertIsSatisfied();
55+
56+
Message endpointIn = endpoint.getExchanges().get(0).getIn();
57+
Message wireTapEndpointIn = wireTapEndpoint.getExchanges().get(0).getIn();
58+
59+
assertEquals("TEST", endpointIn.getBody());
60+
assertEquals("TEST", wireTapEndpointIn.getBody());
61+
}
62+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
entry=direct:entry
2+
endpoint=mock:endpoint
3+
wireTapEndpoint=mock:wireTapEndpoint

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@
145145
<module>cqrs</module>
146146
<module>event-sourcing</module>
147147
<module>data-transfer-object</module>
148-
<module>throttling</module>
148+
<module>throttling</module>
149149
<module>partial-response</module>
150+
<module>eip-wire-tap</module>
150151
</modules>
151152

152153
<dependencyManagement>

0 commit comments

Comments
 (0)