You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: publish-subscribe/README.md
+80-33Lines changed: 80 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,27 @@
1
1
---
2
2
title: "Publish-Subscribe Pattern in Java: Decoupling the solution with asynchronous communication"
3
-
shortTitle: Proxy
4
-
description: "Explore the Proxy design pattern in Java with detailed examples. Learn how it provides controlled access, facilitates lazy initialization, and ensures security. Ideal for developers looking to implement advanced Java techniques."
5
-
category: Structural
3
+
shortTitle: Publish-Subscribe
4
+
description: "Explore the Publish-Subscribe design pattern in Java with detailed examples. Learn how it helps to create loosely coupled, scalable, and flexible systems by allowing components to communicate asynchronously without knowing each other directly."
5
+
category: Behavioral
6
6
language: en
7
7
tag:
8
8
- Decoupling
9
-
- Encapsulation
9
+
- Event-driven
10
10
- Gang Of Four
11
-
- Lazy initialization
12
-
- Proxy
13
-
- Security
14
-
- Wrapping
11
+
- Publish/subscribe
15
12
---
16
13
17
14
## Intent of the Publish-Subscribe Design Pattern
18
15
19
-
The Publish-Subscriber design pattern is widely used in software architecture to transmit data between various components in a system.
16
+
The Publish-Subscribe design pattern is widely used in software architecture to transmit data between various components in a system.
20
17
It is a behavioral design pattern aimed at achieving loosely coupled communication between objects.
21
-
The primary intent is to allow a one-to-many dependency relationship where one object (the Publisher) notifies multiple other objects (the Subscribers) about changes or events,
22
-
without needing to know who or what the subscribers are.
18
+
The primary intent is to allow a one-to-many dependency relationship where one object (the Publisher) notifies multiple other objects (the Subscribers)
19
+
about changes or events, without needing to know who or what the subscribers are.
23
20
24
21
## Detailed Explanation of Publish-Subscribe Pattern with Real-World Examples
25
22
23
+
### Real-world example
24
+
26
25
- Messaging systems like Kafka, RabbitMQ, AWS SNS, JMS
27
26
-**Kafka** : publishes messages to topics and subscribers consumes them in real time for analytics, logs or other purposes.
28
27
-**RabbitMQ** : Uses exchanges as publisher and queues as subscribers to route messages
@@ -38,13 +37,32 @@ without needing to know who or what the subscribers are.
38
37
-**Publisher** : Writes a new blog post and publish to subscribers
39
38
-**Subscribers** : All the subscribers to the newsletter receive the email
40
39
40
+
### In plain words
41
+
42
+
The Publish-Subscribe design pattern allows senders (publishers) to broadcast messages to multiple receivers (subscribers) without knowing who they are,
43
+
enabling loose coupling and asynchronous communication in a system
44
+
45
+
### Wikipedia says
46
+
47
+
In software architecture, publish–subscribe or pub/sub is a messaging pattern where publishers categorize messages into classes that are received by subscribers.
48
+
This is contrasted to the typical messaging pattern model where publishers send messages directly to subscribers.
49
+
50
+
Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
51
+
52
+
Publish–subscribe is a sibling of the message queue paradigm, and is typically one part of a larger message-oriented middleware system.
53
+
Most messaging systems support both the pub/sub and message queue models in their API; e.g., Java Message Service (JMS).
54
+
55
+
### Architectural Diagram
56
+

57
+
41
58
## Programmatic Example of Publish-Subscribe Pattern in Java
42
59
43
60
First we need to identify the Event on which we need the pub-sub methods to trigger.
44
61
For example:
45
62
46
63
- Sending alerts based on the weather events such as earthquakes, floods and tornadoes
47
-
- Sending an email to different customer support accounts when a support ticket is created.
64
+
- Sending alerts based on the temperature
65
+
- Sending an email to different customer support emails when a support ticket is created.
48
66
49
67
The Message class below will hold the content of the message we need to pass between the publisher and the subscribers.
50
68
@@ -57,8 +75,10 @@ public record Message(Object content) {
57
75
The Topic class will have the topic **name** based on the event
58
76
59
77
- Weather events TopicName WEATHER
78
+
- Weather events TopicName TEMPERATURE
60
79
- Support ticket created TopicName CUSTOMER_SUPPORT
61
-
Also the Topic contains a list of subscribers that will listen to that topic
80
+
- Any other custom topic depending on use case
81
+
- Also, the Topic contains a list of subscribers that will listen to that topic
62
82
63
83
We can add or remove subscribers from the subscription to the topic
64
84
@@ -101,14 +121,17 @@ public class PublisherImpl implements Publisher {
101
121
Finally, we can Subscribers to the Topics we want to listen to.
102
122
103
123
- For WEATHER topic we will create _WeatherSubscriber_
124
+
-_WeatherSubscriber_ can also subscribe to TEMPERATURE topic
104
125
- For CUSTOMER_SUPPORT topic we will create _CustomerSupportSubscribe_
126
+
- Also to demonstrate the async behavior we will create a _DelayedWeatherSubscriber_ who has a 0.2 sec processing deplay
105
127
106
-
Both classes will have a _onMessage_ method which will take a Message input.
128
+
All classes will have a _onMessage_ method which will take a Message input.
107
129
108
130
- On message method will verify the content of the message is as expected
109
131
- After content is verified it will perform the operation based on the message
110
-
-_WeatherSubscriber_ will send a weather alert based on the _Message_
132
+
-_WeatherSubscriber_ will send a weather or temperature alert based on the _Message_
111
133
-_CustomerSupportSubscribe_will send an email based on the _Message_
134
+
-_DelayedWeatherSubscriber_ will send a weather alert based on the _Message_ after a delay
112
135
113
136
```java
114
137
publicinterfaceSubscriber {
@@ -119,42 +142,66 @@ public interface Subscriber {
119
142
And here is the invocation of the publisher and subscribers.
11:46:44.311 [main] INFO com.iluwatar.publish.subscribe.subscriber.CustomerSupportSubscriber - Subscriber: supportSub1 sent the email to: [email protected]
157
-
11:46:44.311 [main] INFO com.iluwatar.publish.subscribe.subscriber.CustomerSupportSubscriber - Subscriber: supportSub2 sent the email to: [email protected]
199
+
14:01:45.599 [ForkJoinPool.commonPool-worker-6] INFO com.iluwatar.publish.subscribe.subscriber.CustomerSupportSubscriber -- Customer Support Subscriber: 1416331388 sent the email to: [email protected]
14:01:45.599 [ForkJoinPool.commonPool-worker-5] INFO com.iluwatar.publish.subscribe.subscriber.CustomerSupportSubscriber -- Customer Support Subscriber: 1807508804 sent the email to: [email protected]
0 commit comments