diff --git a/pom.xml b/pom.xml index 4419fb98a..731447d8e 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,7 @@ fluentinterface reactor caching + publish-subscribe diff --git a/publish-subscribe/.gitignore b/publish-subscribe/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/publish-subscribe/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/publish-subscribe/etc/publish-subscribe.png b/publish-subscribe/etc/publish-subscribe.png new file mode 100644 index 000000000..99867da66 Binary files /dev/null and b/publish-subscribe/etc/publish-subscribe.png differ diff --git a/publish-subscribe/etc/publish-subscribe.ucls b/publish-subscribe/etc/publish-subscribe.ucls new file mode 100644 index 000000000..1b121506e --- /dev/null +++ b/publish-subscribe/etc/publish-subscribe.ucls @@ -0,0 +1,218 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/publish-subscribe/index.md b/publish-subscribe/index.md new file mode 100644 index 000000000..b60954ec5 --- /dev/null +++ b/publish-subscribe/index.md @@ -0,0 +1,16 @@ +--- +layout: pattern +title: Publish Subscribe +folder: publish-subscribe +permalink: /patterns/publish-subscribe/ +categories: Integration +tags: Java +--- + +**Intent:** Broadcast messages from sender to all the interested receivers. + +![alt text](./etc/publish-subscribe.png "Publish Subscribe Channel") + +**Applicability:** Use the Publish Subscribe Channel pattern when + +* two or more applications need to communicate using a messaging system for broadcasts. diff --git a/publish-subscribe/pom.xml b/publish-subscribe/pom.xml new file mode 100644 index 000000000..07d704719 --- /dev/null +++ b/publish-subscribe/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.8.0-SNAPSHOT + + publish-subscribe + + + org.apache.camel + camel-core + + + org.apache.camel + camel-stream + + + junit + junit + + + \ No newline at end of file diff --git a/publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java b/publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java new file mode 100644 index 000000000..30f982ed1 --- /dev/null +++ b/publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java @@ -0,0 +1,50 @@ +package com.iluwatar.publish.subscribe; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; + +/** + * + * There are well-established patterns for implementing broadcasting. The Observer pattern describes + * the need to decouple observers from their subject (that is, the originator of the event) so that + * the subject can easily provide event notification to all interested observers no matter how many + * observers there are (even none). The Publish-Subscribe pattern expands upon Observer by adding + * the notion of an event channel for communicating event notifications. + *

+ * A Publish-Subscribe Channel works like this: It has one input channel that splits into multiple + * output channels, one for each subscriber. When an event is published into the channel, the + * Publish-Subscribe Channel delivers a copy of the message to each of the output channels. Each + * output end of the channel has only one subscriber, which is allowed to consume a message only + * once. In this way, each subscriber gets the message only once, and consumed copies disappear from + * their channels. + *

+ * In this example we use Apache Camel to establish a Publish-Subscribe Channel from "direct-origin" + * to "mock:foo", "mock:bar" and "stream:out". + * + */ +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("direct:origin").multicast().to("mock:foo", "mock:bar", "stream:out"); + } + }); + ProducerTemplate template = context.createProducerTemplate(); + context.start(); + context.getRoutes().stream().forEach((r) -> System.out.println(r)); + template.sendBody("direct:origin", "Hello from origin"); + context.stop(); + } +} diff --git a/publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java b/publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java new file mode 100644 index 000000000..bc8b0153e --- /dev/null +++ b/publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java @@ -0,0 +1,17 @@ +package com.iluwatar.publish.subscribe; + +import org.junit.Test; + +/** + * + * Application test + * + */ +public class AppTest { + + @Test + public void test() throws Exception { + String[] args = {}; + App.main(args); + } +}