11package com .iluwatar .publish .subscribe ;
22
33import org .apache .camel .CamelContext ;
4+ import org .apache .camel .ProducerTemplate ;
45import org .apache .camel .builder .RouteBuilder ;
56import org .apache .camel .impl .DefaultCamelContext ;
67
78/**
89 *
9- * When applications communicate with each other using a messaging system they first need to
10- * establish a communication channel that will carry the data. Message Channel decouples Message
11- * producers (publisher) and consumers (subscriber).
10+ * There are well-established patterns for implementing broadcasting. The Observer pattern describes
11+ * the need to decouple observers from their subject (that is, the originator of the event) so that
12+ * the subject can easily provide event notification to all interested observers no matter how many
13+ * observers there are (even none). The Publish-Subscribe pattern expands upon Observer by adding
14+ * the notion of an event channel for communicating event notifications.
1215 * <p>
13- * The sending application doesn't necessarily know what particular applications will end up
14- * retrieving it, but it can be assured that the application that retrieves the information is
15- * interested in that information. This is because the messaging system has different Message
16- * Channels for different types of information the applications want to communicate. When an
17- * application sends information, it doesn't randomly add the information to any channel available;
18- * it adds it to a channel whose specific purpose is to communicate that sort of information.
19- * Likewise, an application that wants to receive particular information doesn't pull info off some
20- * random channel; it selects what channel to get information from based on what type of information
21- * it wants.
16+ * A Publish-Subscribe Channel works like this: It has one input channel that splits into multiple
17+ * output channels, one for each subscriber. When an event is published into the channel, the
18+ * Publish-Subscribe Channel delivers a copy of the message to each of the output channels. Each
19+ * output end of the channel has only one subscriber, which is allowed to consume a message only
20+ * once. In this way, each subscriber gets the message only once, and consumed copies disappear from
21+ * their channels.
2222 * <p>
23- * In this example we use Apache Camel to establish different Message Channels. The first one reads
24- * from standard input and delivers messages to Direct endpoints (Publish; Broadcast). The other
25- * Message Channels are established from the Direct component to different Endpoints (Subscriber).
26- * No actual messages are sent, only the established routes are printed to standard output.
23+ * In this example we use Apache Camel to establish a Publish-Subscribe Channel from "direct-origin"
24+ * to "mock:foo", "mock:bar" and "stream:out".
2725 *
2826 */
2927public class App {
@@ -37,18 +35,16 @@ public class App {
3735 */
3836 public static void main (String [] args ) throws Exception {
3937 CamelContext context = new DefaultCamelContext ();
40-
4138 context .addRoutes (new RouteBuilder () {
42-
4339 @ Override
4440 public void configure () throws Exception {
45- from ("stream:in" ).multicast ().to ("direct:greetings1" , "direct:greetings2" ,
46- "direct:greetings3" );
41+ from ("direct:origin" ).multicast ().to ("mock:foo" , "mock:bar" , "stream:out" );
4742 }
4843 });
49-
44+ ProducerTemplate template = context . createProducerTemplate ();
5045 context .start ();
5146 context .getRoutes ().stream ().forEach ((r ) -> System .out .println (r ));
47+ template .sendBody ("direct:origin" , "Hello from origin" );
5248 context .stop ();
5349 }
5450}
0 commit comments