Merge pull request #227 from iluwatar/message-channel

Message Channel EIP
This commit is contained in:
Ilkka Seppälä 2015-09-02 23:06:22 +03:00
commit e33d037c0f
8 changed files with 451 additions and 0 deletions

1
message-channel/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

View File

@ -0,0 +1,320 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="org.apache.camel.builder.RouteBuilder" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="920" y="458"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<interface id="2" language="java" name="org.apache.camel.CamelContext" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="674" y="458"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<class id="3" language="java" name="org.apache.camel.model.RouteDefinition" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="673" y="48"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<class id="4" language="java" name="org.apache.camel.model.ProcessorDefinition" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="673" y="-96"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<class id="5" language="java" name="org.apache.camel.model.OptionalIdentifiedDefinition" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="248" y="-96"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<interface id="6" language="java" name="org.apache.camel.NamedNode" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="249" y="0"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="7" language="java" name="org.apache.camel.model.Block" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="464" y="13"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="8" language="java" name="org.apache.camel.RoutesBuilder" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="920" y="590"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<class id="9" language="java" name="org.apache.camel.builder.BuilderSupport" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="920" y="297"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<interface id="10" language="java" name="org.apache.camel.SuspendableService" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="267" y="458"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="11" language="java" name="org.apache.camel.Service" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="267" y="297"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="12" language="java" name="org.apache.camel.RuntimeConfiguration" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="674" y="590"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<class id="13" language="java" name="org.apache.camel.impl.DefaultCamelContext" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="673" y="167"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<class id="14" language="java" name="org.apache.camel.support.ServiceSupport" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="265" y="91"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</class>
<interface id="15" language="java" name="org.apache.camel.StatefulService" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="5" y="188"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="16" language="java" name="org.apache.camel.ShutdownableService" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="265" y="188"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<interface id="17" language="java" name="org.apache.camel.model.ModelCamelContext" project="message-channel"
file="C:/Users/ilkka/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="674" y="297"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</display>
</interface>
<generalization id="18">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="5"/>
</generalization>
<generalization id="19">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="10"/>
</generalization>
<realization id="20">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="8"/>
</realization>
<association id="21">
<end type="SOURCE" refId="9" navigable="false">
<attribute id="22" name="context">
<position height="0" width="0" x="-583" y="-276"/>
</attribute>
<multiplicity id="23" minimum="0" maximum="1">
<position height="0" width="0" x="-583" y="-276"/>
</multiplicity>
</end>
<end type="TARGET" refId="17" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="24">
<end type="SOURCE" refId="4" navigable="false">
<attribute id="25" name="blocks">
<position height="0" width="0" x="145" y="-462"/>
</attribute>
<multiplicity id="26" minimum="0" maximum="2147483647">
<position height="0" width="0" x="145" y="-462"/>
</multiplicity>
</end>
<end type="TARGET" refId="7" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="27">
<end type="SOURCE" refId="13" navigable="false">
<attribute id="28" name="servicesToClose">
<position height="0" width="0" x="-409" y="37"/>
</attribute>
<multiplicity id="29" minimum="0" maximum="2147483647">
<position height="0" width="0" x="-409" y="37"/>
</multiplicity>
</end>
<end type="TARGET" refId="11" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="30">
<end type="SOURCE" refId="13"/>
<end type="TARGET" refId="17"/>
</realization>
<realization id="31">
<end type="SOURCE" refId="14"/>
<end type="TARGET" refId="15"/>
</realization>
<generalization id="32">
<end type="SOURCE" refId="17"/>
<end type="TARGET" refId="2"/>
</generalization>
<generalization id="33">
<end type="SOURCE" refId="15"/>
<end type="TARGET" refId="10"/>
</generalization>
<generalization id="34">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="9"/>
</generalization>
<generalization id="35">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</generalization>
<generalization id="36">
<end type="SOURCE" refId="16"/>
<end type="TARGET" refId="11"/>
</generalization>
<generalization id="37">
<end type="SOURCE" refId="15"/>
<end type="TARGET" refId="16"/>
</generalization>
<generalization id="38">
<end type="SOURCE" refId="10"/>
<end type="TARGET" refId="11"/>
</generalization>
<realization id="39">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="7"/>
</realization>
<association id="40">
<end type="SOURCE" refId="4" navigable="false">
<attribute id="41" name="parent">
<position height="0" width="0" x="329" y="-461"/>
</attribute>
<multiplicity id="42" minimum="0" maximum="1">
<position height="0" width="0" x="329" y="-461"/>
</multiplicity>
</end>
<end type="TARGET" refId="4" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="43">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="6"/>
</realization>
<generalization id="44">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="12"/>
</generalization>
<generalization id="45">
<end type="SOURCE" refId="13"/>
<end type="TARGET" refId="14"/>
</generalization>
<association id="46">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="47" name="outputs">
<position height="0" width="0" x="329" y="-461"/>
</attribute>
<multiplicity id="48" minimum="0" maximum="2147483647">
<position height="0" width="0" x="329" y="-461"/>
</multiplicity>
</end>
<end type="TARGET" refId="4" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="49">
<end type="SOURCE" refId="13" navigable="false">
<attribute id="50" name="routeDefinitions">
<position height="0" width="0" x="291" y="-218"/>
</attribute>
<multiplicity id="51" minimum="0" maximum="2147483647">
<position height="0" width="0" x="291" y="-218"/>
</multiplicity>
</end>
<end type="TARGET" refId="3" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="52">
<end type="SOURCE" refId="13"/>
<end type="TARGET" refId="10"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="false" package="false" protected="false" private="false" static="true"/>
<operations public="false" package="false" protected="false" private="false" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

21
message-channel/index.md Normal file
View File

@ -0,0 +1,21 @@
---
layout: pattern
title: Message Channel
folder: message-channel
permalink: /patterns/message-channel/
categories: Integration
tags: Java
---
**Intent:** When two applications communicate using a messaging system they do it by using logical addresses
of the system, so called Message Channels.
![alt text](./etc/message-channel.png "Message Channel")
**Applicability:** Use the Message Channel pattern when
* two or more applications need to communicate using a messaging system
**Real world examples:**
* [akka-camel](http://doc.akka.io/docs/akka/snapshot/scala/camel.html)

27
message-channel/pom.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.6.0</version>
</parent>
<artifactId>message-channel</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,53 @@
package com.iluwatar.message.channel;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
/**
*
* When two applications communicate with each other using a messaging system
* they first need to establish a communication channel that will carry the
* data. Message Channel decouples Message producers and consumers.
* <p>
* The sending application doesn't necessarily know what particular application
* will end up retrieving it, but it can be assured that the application that
* retrieves the information is interested in that information. This is because
* the messaging system has different Message Channels for different types of
* information the applications want to communicate. When an application sends
* information, it doesn't randomly add the information to any channel available;
* it adds it to a channel whose specific purpose is to communicate that sort of
* information. Likewise, an application that wants to receive particular information
* doesn't pull info off some random channel; it selects what channel to get information
* from based on what type of information it wants.
* <p>
* In this example we use Apache Camel to establish two different Message Channels. The first
* one reads from standard input and delivers messages to Direct endpoint. The second Message
* Channel is established from the Direct component to console output. No actual messages are sent,
* only the established routes are printed to standard output.
*
*/
public class App {
/**
* Program entry point
* @param args command line args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("stream:in").to("direct:greetings");
from("direct:greetings").to("stream:out");
}
});
context.start();
context.getRoutes().stream().forEach((r) -> System.out.println(r));
context.stop();
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar.message.channel;
import org.junit.Test;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() throws Exception {
String[] args = {};
App.main(args);
}
}

12
pom.xml
View File

@ -17,6 +17,7 @@
<coveralls.version>3.1.0</coveralls.version>
<jacoco.version>0.7.2.201409121644</jacoco.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<camel.version>2.15.3</camel.version>
</properties>
<modules>
<module>abstract-factory</module>
@ -75,6 +76,7 @@
<module>half-sync-half-async</module>
<module>step-builder</module>
<module>layers</module>
<module>message-channel</module>
</modules>
<dependencyManagement>
@ -104,6 +106,16 @@
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>