Skip to content

Commit 8519e13

Browse files
committed
Merge branch 'publish-subscribe' of https://github.com/JaXt0r/java-design-patterns into JaXt0r-publish-subscribe
Conflicts: pom.xml
2 parents ba3f583 + ac5c196 commit 8519e13

File tree

8 files changed

+333
-0
lines changed

8 files changed

+333
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<module>fluentinterface</module>
8686
<module>reactor</module>
8787
<module>caching</module>
88+
<module>publish-subscribe</module>
8889
</modules>
8990

9091
<dependencyManagement>

publish-subscribe/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
27.1 KB
Loading
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="org.apache.camel.builder.RouteBuilder" project="message-channel"
5+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
6+
corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="269" y="526"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
11+
<operations public="false" package="false" protected="false" private="false" static="false"/>
12+
</display>
13+
</class>
14+
<interface id="2" language="java" name="org.apache.camel.RoutesBuilder" project="message-channel"
15+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
16+
corner="BOTTOM_RIGHT">
17+
<position height="-1" width="-1" x="270" y="406"/>
18+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
19+
sort-features="false" accessors="true" visibility="true">
20+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
21+
<operations public="false" package="false" protected="false" private="false" static="false"/>
22+
</display>
23+
</interface>
24+
<class id="3" language="java" name="org.apache.camel.model.RouteDefinition" project="message-channel"
25+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
26+
corner="BOTTOM_RIGHT">
27+
<position height="-1" width="-1" x="574" y="421"/>
28+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
29+
sort-features="false" accessors="true" visibility="true">
30+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
31+
<operations public="false" package="false" protected="false" private="false" static="false"/>
32+
</display>
33+
</class>
34+
<class id="4" language="java" name="org.apache.camel.model.RoutesDefinition" project="message-channel"
35+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
36+
corner="BOTTOM_RIGHT">
37+
<position height="-1" width="-1" x="574" y="526"/>
38+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
39+
sort-features="false" accessors="true" visibility="true">
40+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
41+
<operations public="false" package="false" protected="false" private="false" static="false"/>
42+
</display>
43+
</class>
44+
<class id="5" language="java" name="org.apache.camel.model.MulticastDefinition" project="message-channel"
45+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
46+
corner="BOTTOM_RIGHT">
47+
<position height="-1" width="-1" x="574" y="68"/>
48+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
49+
sort-features="false" accessors="true" visibility="true">
50+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
51+
<operations public="false" package="false" protected="false" private="false" static="false"/>
52+
</display>
53+
</class>
54+
<class id="6" language="java" name="org.apache.camel.model.OutputDefinition" project="message-channel"
55+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
56+
corner="BOTTOM_RIGHT">
57+
<position height="-1" width="-1" x="576" y="164"/>
58+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
59+
sort-features="false" accessors="true" visibility="true">
60+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
61+
<operations public="false" package="false" protected="false" private="false" static="false"/>
62+
</display>
63+
</class>
64+
<class id="7" language="java" name="org.apache.camel.model.ProcessorDefinition" project="message-channel"
65+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
66+
corner="BOTTOM_RIGHT">
67+
<position height="-1" width="-1" x="575" y="284"/>
68+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
69+
sort-features="false" accessors="true" visibility="true">
70+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
71+
<operations public="false" package="false" protected="false" private="false" static="false"/>
72+
</display>
73+
</class>
74+
<interface id="8" language="java" name="org.apache.camel.CamelContext" project="message-channel"
75+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
76+
corner="BOTTOM_LEFT">
77+
<position height="-1" width="-1" x="1086" y="346"/>
78+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
79+
sort-features="false" accessors="true" visibility="true">
80+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
81+
<operations public="false" package="false" protected="false" private="false" static="false"/>
82+
</display>
83+
</interface>
84+
<interface id="9" language="java" name="org.apache.camel.model.ModelCamelContext" project="message-channel"
85+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
86+
corner="BOTTOM_LEFT">
87+
<position height="-1" width="-1" x="1086" y="526"/>
88+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
89+
sort-features="false" accessors="true" visibility="true">
90+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
91+
<operations public="false" package="false" protected="false" private="false" static="false"/>
92+
</display>
93+
</interface>
94+
<class id="10" language="java" name="org.apache.camel.impl.DefaultCamelContext" project="message-channel"
95+
file="D:/Users/mdaehner/.m2/repository/org/apache/camel/camel-core/2.15.3/camel-core-2.15.3.jar" binary="true"
96+
corner="BOTTOM_RIGHT">
97+
<position height="-1" width="-1" x="885" y="421"/>
98+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
99+
sort-features="false" accessors="true" visibility="true">
100+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
101+
<operations public="false" package="false" protected="false" private="false" static="false"/>
102+
</display>
103+
</class>
104+
<realization id="11">
105+
<end type="SOURCE" refId="10"/>
106+
<end type="TARGET" refId="9"/>
107+
</realization>
108+
<generalization id="12">
109+
<end type="SOURCE" refId="6"/>
110+
<end type="TARGET" refId="7"/>
111+
</generalization>
112+
<association id="13">
113+
<end type="SOURCE" refId="6" navigable="false">
114+
<attribute id="14" name="outputs">
115+
<position height="0" width="0" x="0" y="0"/>
116+
</attribute>
117+
<multiplicity id="15" minimum="0" maximum="2147483647">
118+
<position height="0" width="0" x="0" y="0"/>
119+
</multiplicity>
120+
</end>
121+
<end type="TARGET" refId="7" navigable="true"/>
122+
<display labels="true" multiplicity="true"/>
123+
</association>
124+
<realization id="16">
125+
<end type="SOURCE" refId="1"/>
126+
<end type="TARGET" refId="2"/>
127+
</realization>
128+
<generalization id="17">
129+
<end type="SOURCE" refId="3"/>
130+
<end type="TARGET" refId="7"/>
131+
</generalization>
132+
<association id="18">
133+
<end type="SOURCE" refId="4" navigable="false">
134+
<attribute id="19" name="camelContext">
135+
<position height="0" width="0" x="0" y="0"/>
136+
</attribute>
137+
<multiplicity id="20" minimum="0" maximum="1">
138+
<position height="0" width="0" x="0" y="0"/>
139+
</multiplicity>
140+
</end>
141+
<end type="TARGET" refId="9" navigable="true"/>
142+
<display labels="true" multiplicity="true"/>
143+
</association>
144+
<association id="21">
145+
<end type="SOURCE" refId="7" navigable="false">
146+
<attribute id="22" name="parent">
147+
<position height="0" width="0" x="0" y="0"/>
148+
</attribute>
149+
<multiplicity id="23" minimum="0" maximum="1">
150+
<position height="0" width="0" x="0" y="0"/>
151+
</multiplicity>
152+
</end>
153+
<end type="TARGET" refId="7" navigable="true"/>
154+
<display labels="true" multiplicity="true"/>
155+
</association>
156+
<association id="24">
157+
<end type="SOURCE" refId="1" navigable="false">
158+
<attribute id="25" name="routeCollection">
159+
<position height="0" width="0" x="0" y="0"/>
160+
</attribute>
161+
<multiplicity id="26" minimum="0" maximum="1">
162+
<position height="0" width="0" x="0" y="0"/>
163+
</multiplicity>
164+
</end>
165+
<end type="TARGET" refId="4" navigable="true"/>
166+
<display labels="true" multiplicity="true"/>
167+
</association>
168+
<association id="27">
169+
<end type="SOURCE" refId="10" navigable="false">
170+
<attribute id="28" name="routeDefinitions">
171+
<position height="0" width="0" x="0" y="0"/>
172+
</attribute>
173+
<multiplicity id="29" minimum="0" maximum="2147483647">
174+
<position height="0" width="0" x="0" y="0"/>
175+
</multiplicity>
176+
</end>
177+
<end type="TARGET" refId="3" navigable="true"/>
178+
<display labels="true" multiplicity="true"/>
179+
</association>
180+
<association id="30">
181+
<end type="SOURCE" refId="3" navigable="false">
182+
<attribute id="31" name="outputs">
183+
<position height="0" width="0" x="0" y="0"/>
184+
</attribute>
185+
<multiplicity id="32" minimum="0" maximum="2147483647">
186+
<position height="0" width="0" x="0" y="0"/>
187+
</multiplicity>
188+
</end>
189+
<end type="TARGET" refId="7" navigable="true"/>
190+
<display labels="true" multiplicity="true"/>
191+
</association>
192+
<generalization id="33">
193+
<end type="SOURCE" refId="9"/>
194+
<end type="TARGET" refId="8"/>
195+
</generalization>
196+
<association id="34">
197+
<end type="SOURCE" refId="4" navigable="false">
198+
<attribute id="35" name="routes">
199+
<position height="0" width="0" x="0" y="0"/>
200+
</attribute>
201+
<multiplicity id="36" minimum="0" maximum="2147483647">
202+
<position height="0" width="0" x="0" y="0"/>
203+
</multiplicity>
204+
</end>
205+
<end type="TARGET" refId="3" navigable="true"/>
206+
<display labels="true" multiplicity="true"/>
207+
</association>
208+
<generalization id="37">
209+
<end type="SOURCE" refId="5"/>
210+
<end type="TARGET" refId="6"/>
211+
</generalization>
212+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="false"
213+
sort-features="false" accessors="true" visibility="true">
214+
<attributes public="false" package="false" protected="false" private="false" static="true"/>
215+
<operations public="false" package="false" protected="false" private="false" static="false"/>
216+
</classifier-display>
217+
<association-display labels="true" multiplicity="true"/>
218+
</class-diagram>

publish-subscribe/index.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: pattern
3+
title: Publish Subscribe
4+
folder: publish-subscribe
5+
permalink: /patterns/publish-subscribe/
6+
categories: Integration
7+
tags: Java
8+
---
9+
10+
**Intent:** When applications communicate using a messaging system they do it by using logical addresses
11+
of the system, so called Publish Subscribe Channel. The publisher broadcasts a message to all registered Subscriber.
12+
13+
![alt text](./etc/publish-subscribe.png "Publish Subscribe Channel")
14+
15+
**Applicability:** Use the Publish Subscribe Channel pattern when
16+
17+
* two or more applications need to communicate using a messaging system for broadcasts.

publish-subscribe/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.iluwatar</groupId>
6+
<artifactId>java-design-patterns</artifactId>
7+
<version>1.7.0</version>
8+
</parent>
9+
<artifactId>publish-subscribe</artifactId>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.apache.camel</groupId>
13+
<artifactId>camel-core</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.apache.camel</groupId>
17+
<artifactId>camel-stream</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
</dependency>
23+
</dependencies>
24+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.iluwatar.publish.subscribe;
2+
3+
import org.apache.camel.CamelContext;
4+
import org.apache.camel.builder.RouteBuilder;
5+
import org.apache.camel.impl.DefaultCamelContext;
6+
7+
/**
8+
*
9+
* When applications communicate with each other using a messaging system
10+
* they first need to establish a communication channel that will carry the
11+
* data. Message Channel decouples Message producers (publisher) and consumers (subscriber).
12+
* <p>
13+
* The sending application doesn't necessarily know what particular applications
14+
* will end up retrieving it, but it can be assured that the application that
15+
* retrieves the information is interested in that information. This is because
16+
* the messaging system has different Message Channels for different types of
17+
* information the applications want to communicate. When an application sends
18+
* information, it doesn't randomly add the information to any channel available;
19+
* it adds it to a channel whose specific purpose is to communicate that sort of
20+
* information. Likewise, an application that wants to receive particular information
21+
* doesn't pull info off some random channel; it selects what channel to get information
22+
* from based on what type of information it wants.
23+
* <p>
24+
* In this example we use Apache Camel to establish different Message Channels. The first
25+
* one reads from standard input and delivers messages to Direct endpoints (Publish; Broadcast). The other Message
26+
* Channels are established from the Direct component to different Endpoints (Subscriber). No actual messages are sent,
27+
* only the established routes are printed to standard output.
28+
*
29+
*/
30+
public class App {
31+
32+
/**
33+
* Program entry point
34+
*
35+
* @param args command line args
36+
* @throws Exception
37+
*/
38+
public static void main(String[] args) throws Exception {
39+
CamelContext context = new DefaultCamelContext();
40+
41+
context.addRoutes(new RouteBuilder() {
42+
43+
@Override
44+
public void configure() throws Exception {
45+
from("stream:in")
46+
.multicast()
47+
.to("direct:greetings1", "direct:greetings2", "direct:greetings3");
48+
}
49+
});
50+
51+
context.start();
52+
context.getRoutes().stream().forEach((r) -> System.out.println(r));
53+
context.stop();
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar.publish.subscribe;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
*
7+
* Application test
8+
*
9+
*/
10+
public class AppTest {
11+
12+
@Test
13+
public void test() throws Exception {
14+
String[] args = {};
15+
App.main(args);
16+
}
17+
}

0 commit comments

Comments
 (0)