diff --git a/message-channel/.gitignore b/message-channel/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/message-channel/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/message-channel/etc/message-channel.png b/message-channel/etc/message-channel.png new file mode 100644 index 000000000..7db473281 Binary files /dev/null and b/message-channel/etc/message-channel.png differ diff --git a/message-channel/etc/message-channel.ucls b/message-channel/etc/message-channel.ucls new file mode 100644 index 000000000..3ef0ed4bc --- /dev/null +++ b/message-channel/etc/message-channel.ucls @@ -0,0 +1,320 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/message-channel/index.md b/message-channel/index.md new file mode 100644 index 000000000..b5fb1761a --- /dev/null +++ b/message-channel/index.md @@ -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) diff --git a/message-channel/pom.xml b/message-channel/pom.xml new file mode 100644 index 000000000..ef66f2401 --- /dev/null +++ b/message-channel/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.6.0 + + message-channel + + + org.apache.camel + camel-core + + + org.apache.camel + camel-stream + + + junit + junit + test + + + diff --git a/message-channel/src/main/java/com/iluwatar/message/channel/App.java b/message-channel/src/main/java/com/iluwatar/message/channel/App.java new file mode 100644 index 000000000..f9a334a8a --- /dev/null +++ b/message-channel/src/main/java/com/iluwatar/message/channel/App.java @@ -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. + *

+ * 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. + *

+ * 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(); + } +} diff --git a/message-channel/src/test/java/com/iluwatar/message/channel/AppTest.java b/message-channel/src/test/java/com/iluwatar/message/channel/AppTest.java new file mode 100644 index 000000000..fa3af1161 --- /dev/null +++ b/message-channel/src/test/java/com/iluwatar/message/channel/AppTest.java @@ -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); + } +} diff --git a/pom.xml b/pom.xml index 961d9b281..7b0d80bd8 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 3.1.0 0.7.2.201409121644 1.4 + 2.15.3 abstract-factory @@ -75,6 +76,7 @@ half-sync-half-async step-builder layers + message-channel @@ -104,6 +106,16 @@ commons-dbcp ${commons-dbcp.version} + + org.apache.camel + camel-core + ${camel.version} + + + org.apache.camel + camel-stream + ${camel.version} + junit junit