SlideShare a Scribd company logo
Design Patterns
Distributed Publisher-Subscriber Network
Sushil PaneruRishabh Karajgi
Introduction to Pub-Sub systems
Publish-Subscribe Messaging
When multiple applications need to receive the same messages, Publish-Subscribe
Messaging is used. The central concept in a Publish-Subscribe messaging system is
the Topic. Multiple Publishers may send messages to a Topic, and all Subscribers
to that Topic receive all the messages sent to that Topic. This model, as shown in
Figure 1, is extremely useful when a group of applications want to notify each
other of a particular occurrence.
The point to note in Publish-Subscribe Messaging is that, there may be multiple
Senders and multiple Receivers.
Point-To-Point Messaging
When one process needs to send a message to another process, Point-To-Point
Messaging can be used. However, this may or may not be a one-way relationship.
The client to a Messaging system may only send messages, only receive messages,
or send and receive messages. At the same time, another client can also send
and/or receive messages. In the simplest case, one client is the Sender of the
message and the other client is the Receiver of the message.
There are two basic types of Point-to-Point Messaging systems. The first one involves
a client that directly sends a message to another client. The second and more
common implementation is based on the concept of a Message Queue. Such a
system is shown in Figure 2.
The point to note in Point-to-Point messaging is that, even though there may be
multiple Senders of messages, there is only a single Receiver for the messages.
Strength of PSA
● Provides abstraction for the publishers and subscribers
● Space decoupling, time decoupling and synchronization decoupling
● Adds modularity to a messaging system
● Allows to add new and break services
Advantage
● A major advantage is the simplicity and flexibility of decentralization
implementation as this enables the system to support a large number
of clients and huge amount of data transfers. Highly scalable
Weakness of PSA
● Potential loss of messages due to the events needed to be pruned
● Example: Publisher may produce a very large number of events and it
will be inefficient to publish all of them as events as and so there
should be a mechanism to prune the event before
publishing so as not to overwhelm the
systems.
Problem statement
Problem statement/Business Logic
Overview
Matching & Dispatching
Choice of ‘information
spaces’
Complexity of subscriptions
Performance
Distributed Control
Application Level Routing
Reliability & Sequencing
Context
A state trackable message
queue that stores incoming
messages of the publisher
based on the current state of
the subscriber
Problem statement
No guarantee of message
delivery.
Less control over ordering of
messages
Current state of a subscriber
cannot be tracked hence
leading to redundant
requests.
Solution
State-trackable messaging
queue
To build a state trackable message
exchange system which replicates
a pub-sub system in a distributed
fashion
Implementation
Why Python?
- Better Networking API i.e socket programming
- Better developer community support
- Better thread library
- Support both OOP and functional programming paradigms
Program flow
1
Publisher and
subscriber are
registered to Broker
2
Topics are created
along with their
respective message
queues
3
Subscriber starts
listening to posts
4
Publisher publishes
messages related to
topics
5
Broker routes the
messages relating to
corresponding topics
to subscribers
Design Patterns used:
Mediator Pattern
● Mediator pattern is used to reduce communication complexity between
multiple objects or classes.
● This pattern provides a mediator class which normally handles all the
communications between different classes and supports easy maintenance
of the code by loose coupling.
● Mediator pattern falls under behavioral pattern category.
Usage
● The Handler thread receives messages from Publisher
and the Dispatcher thread dispatches the received
messages to the concerned subscribers.
● So these threads needed a way to communicate with
themselves so a mediator was constructed by
considering all thread synchronisation problem.
Class Mediator
Def __init__(self):
Pipeline =
create_blocking_queue()
Def send_to_dispatcher(msg)
pipeline.put(msg)
Def receive_from_handler()
Return pipeline.pop()
Class Handler(Thread):
Def __init__(self,Mediator mediator):
Def run():
//do work
//receive message(msg)
from
Class Dispatcher(Thread):
Def __init_(self,Mediator
mediator)
Def run():
Msg =
mediator.receiver_from_handler()
//dispatch Msg to the
consumers
Decorator Pattern
● Decorator pattern allows a user to add new functionality to
an existing object without altering its structure. This type of
design pattern comes under structural pattern as this
pattern acts as a wrapper to existing class.
● This pattern creates a decorator class which wraps the
original class and provides additional functionality keeping
class methods signature intact.
Usage
● Router class includes the logic of routing the messages that broker has
received from publishers to subscribers.
● In order to distribute the load of routing from broker to subscriber, each
subscriber also has to route messages to other subscriber.
● Implementation logic of routing will be different in subscriber from what
was used in broker, hence we need to add new functionalities to router
class.
Thus, we create a RouterDecorator class to add new routing feature as per
the requirement of subscriber without modifying the class Router
Class Router:
Def __init__(self):
//construct object
Def route(consumers):
//route messages to consumers
Class RouterDecorator(Router):
Router router;
Def __init__(self)
//initialise router object
Def route(consumers): //as used by broker
router.route(consumers)
Def route_for_consumer(consumers): //used by consumer
// different implementation of route() for consumers
Singleton Pattern
● Singleton pattern is one of the simplest design patterns in Java. This type of
design pattern comes under creational pattern as this pattern provides one of
the best ways to create an object.
● This pattern involves a single class which is responsible to create an object while
making sure that only single object gets created. This class provides a way to
access its only object which can be accessed directly without need to instantiate
the object of the class.
● Create a SingleObject class.
SingleObject class have its constructor
as private and have a static instance
of itself.
● SingleObject class provides a static
method to get its static instance to
outside world.SingletonPatternDemo,
our demo class will use SingleObject
class to get a SingleObjectobject.
Usage
● Broker is always listening to specific ports where subscriber and
publisher receive and send messages respectively.
● We cannot instantiate classes like ConsumerAcceptor and Handler more
than once since they listen to only specific ports and, binding sockets to
ports that are already in use is an invalid operation.
● Hence, we use Singleton class to make sure that class
ConsumerAcceptor and Handler get instantiated only once.
Class ConsumerAcceptor(Thread):
INSTANCE=None
Def __init__(self)
If cls.INSTANCE is not None:
Raise ValueError(“Already
Instantiated”)
//else initialise object
Def getInstance():
If cls.INSTANCE is None:
cls.INSTANCE =
ConsumerAcceptor()
Return cls.INSTANCE
C = ConsumerAcceptor.getInstance()
THANK
YOU
Full documented code available at:
http://github.com/karajrish/pubsubmq

More Related Content

Similar to Design Patterns - Distributed Publisher-Subscriber Network

TrackStudio Overview
TrackStudio OverviewTrackStudio Overview
TrackStudio Overview
Maxim Kramarenko
 
Towards Improved Data Dissemination of Publish-Subscribe Systems
Towards Improved Data Dissemination of Publish-Subscribe SystemsTowards Improved Data Dissemination of Publish-Subscribe Systems
Towards Improved Data Dissemination of Publish-Subscribe SystemsSrinath Perera
 
Distributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communicationDistributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communication
MNM Jain Engineering College
 
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
Jitendra Bafna
 
MOM - Message Oriented Middleware
MOM - Message Oriented MiddlewareMOM - Message Oriented Middleware
MOM - Message Oriented Middleware
Peter R. Egli
 
MOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufMOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufGaurav Bhardwaj
 
Designing Distributed Systems
Designing Distributed SystemsDesigning Distributed Systems
Designing Distributed SystemsDhananjay Singh
 
Java Message Service
Java Message ServiceJava Message Service
Java Message Service
AMIT YADAV
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1
assinha
 
Chat application through client server management system project.pdf
Chat application through client server management system project.pdfChat application through client server management system project.pdf
Chat application through client server management system project.pdf
Kamal Acharya
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Services
kumar gaurav
 
Indirect communication is defined as communication between entities in a dist...
Indirect communication is defined as communication between entities in a dist...Indirect communication is defined as communication between entities in a dist...
Indirect communication is defined as communication between entities in a dist...
nandepovanhu
 

Similar to Design Patterns - Distributed Publisher-Subscriber Network (20)

TrackStudio Overview
TrackStudio OverviewTrackStudio Overview
TrackStudio Overview
 
Towards Improved Data Dissemination of Publish-Subscribe Systems
Towards Improved Data Dissemination of Publish-Subscribe SystemsTowards Improved Data Dissemination of Publish-Subscribe Systems
Towards Improved Data Dissemination of Publish-Subscribe Systems
 
Distributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communicationDistributed System-Multicast & Indirect communication
Distributed System-Multicast & Indirect communication
 
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
MuleSoft Surat Virtual Meetup#26 - Implementing Hybrid MuleSoft Runtime - Any...
 
MOM - Message Oriented Middleware
MOM - Message Oriented MiddlewareMOM - Message Oriented Middleware
MOM - Message Oriented Middleware
 
MOOC backbone using Netty and Protobuf
MOOC backbone using Netty and ProtobufMOOC backbone using Netty and Protobuf
MOOC backbone using Netty and Protobuf
 
Designing Distributed Systems
Designing Distributed SystemsDesigning Distributed Systems
Designing Distributed Systems
 
Java Message Service
Java Message ServiceJava Message Service
Java Message Service
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1
 
Chat application through client server management system project.pdf
Chat application through client server management system project.pdfChat application through client server management system project.pdf
Chat application through client server management system project.pdf
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Services
 
Indirect communication is defined as communication between entities in a dist...
Indirect communication is defined as communication between entities in a dist...Indirect communication is defined as communication between entities in a dist...
Indirect communication is defined as communication between entities in a dist...
 

Recently uploaded

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

Design Patterns - Distributed Publisher-Subscriber Network

  • 1. Design Patterns Distributed Publisher-Subscriber Network Sushil PaneruRishabh Karajgi
  • 3. Publish-Subscribe Messaging When multiple applications need to receive the same messages, Publish-Subscribe Messaging is used. The central concept in a Publish-Subscribe messaging system is the Topic. Multiple Publishers may send messages to a Topic, and all Subscribers to that Topic receive all the messages sent to that Topic. This model, as shown in Figure 1, is extremely useful when a group of applications want to notify each other of a particular occurrence. The point to note in Publish-Subscribe Messaging is that, there may be multiple Senders and multiple Receivers.
  • 4.
  • 5. Point-To-Point Messaging When one process needs to send a message to another process, Point-To-Point Messaging can be used. However, this may or may not be a one-way relationship. The client to a Messaging system may only send messages, only receive messages, or send and receive messages. At the same time, another client can also send and/or receive messages. In the simplest case, one client is the Sender of the message and the other client is the Receiver of the message. There are two basic types of Point-to-Point Messaging systems. The first one involves a client that directly sends a message to another client. The second and more common implementation is based on the concept of a Message Queue. Such a system is shown in Figure 2. The point to note in Point-to-Point messaging is that, even though there may be multiple Senders of messages, there is only a single Receiver for the messages.
  • 6.
  • 7. Strength of PSA ● Provides abstraction for the publishers and subscribers ● Space decoupling, time decoupling and synchronization decoupling ● Adds modularity to a messaging system ● Allows to add new and break services
  • 8. Advantage ● A major advantage is the simplicity and flexibility of decentralization implementation as this enables the system to support a large number of clients and huge amount of data transfers. Highly scalable
  • 9. Weakness of PSA ● Potential loss of messages due to the events needed to be pruned ● Example: Publisher may produce a very large number of events and it will be inefficient to publish all of them as events as and so there should be a mechanism to prune the event before publishing so as not to overwhelm the systems.
  • 11. Problem statement/Business Logic Overview Matching & Dispatching Choice of ‘information spaces’ Complexity of subscriptions Performance Distributed Control Application Level Routing Reliability & Sequencing Context A state trackable message queue that stores incoming messages of the publisher based on the current state of the subscriber Problem statement No guarantee of message delivery. Less control over ordering of messages Current state of a subscriber cannot be tracked hence leading to redundant requests.
  • 12. Solution State-trackable messaging queue To build a state trackable message exchange system which replicates a pub-sub system in a distributed fashion
  • 14. Why Python? - Better Networking API i.e socket programming - Better developer community support - Better thread library - Support both OOP and functional programming paradigms
  • 16. 1 Publisher and subscriber are registered to Broker 2 Topics are created along with their respective message queues 3 Subscriber starts listening to posts 4 Publisher publishes messages related to topics 5 Broker routes the messages relating to corresponding topics to subscribers
  • 18. ● Mediator pattern is used to reduce communication complexity between multiple objects or classes. ● This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling. ● Mediator pattern falls under behavioral pattern category.
  • 19. Usage ● The Handler thread receives messages from Publisher and the Dispatcher thread dispatches the received messages to the concerned subscribers. ● So these threads needed a way to communicate with themselves so a mediator was constructed by considering all thread synchronisation problem.
  • 20. Class Mediator Def __init__(self): Pipeline = create_blocking_queue() Def send_to_dispatcher(msg) pipeline.put(msg) Def receive_from_handler() Return pipeline.pop() Class Handler(Thread): Def __init__(self,Mediator mediator): Def run(): //do work //receive message(msg) from Class Dispatcher(Thread): Def __init_(self,Mediator mediator) Def run(): Msg = mediator.receiver_from_handler() //dispatch Msg to the consumers
  • 21.
  • 23. ● Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class. ● This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
  • 24.
  • 25. Usage ● Router class includes the logic of routing the messages that broker has received from publishers to subscribers. ● In order to distribute the load of routing from broker to subscriber, each subscriber also has to route messages to other subscriber. ● Implementation logic of routing will be different in subscriber from what was used in broker, hence we need to add new functionalities to router class. Thus, we create a RouterDecorator class to add new routing feature as per the requirement of subscriber without modifying the class Router
  • 26. Class Router: Def __init__(self): //construct object Def route(consumers): //route messages to consumers Class RouterDecorator(Router): Router router; Def __init__(self) //initialise router object Def route(consumers): //as used by broker router.route(consumers) Def route_for_consumer(consumers): //used by consumer // different implementation of route() for consumers
  • 27.
  • 29. ● Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. ● This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.
  • 30. ● Create a SingleObject class. SingleObject class have its constructor as private and have a static instance of itself. ● SingleObject class provides a static method to get its static instance to outside world.SingletonPatternDemo, our demo class will use SingleObject class to get a SingleObjectobject.
  • 31. Usage ● Broker is always listening to specific ports where subscriber and publisher receive and send messages respectively. ● We cannot instantiate classes like ConsumerAcceptor and Handler more than once since they listen to only specific ports and, binding sockets to ports that are already in use is an invalid operation. ● Hence, we use Singleton class to make sure that class ConsumerAcceptor and Handler get instantiated only once.
  • 32. Class ConsumerAcceptor(Thread): INSTANCE=None Def __init__(self) If cls.INSTANCE is not None: Raise ValueError(“Already Instantiated”) //else initialise object Def getInstance(): If cls.INSTANCE is None: cls.INSTANCE = ConsumerAcceptor() Return cls.INSTANCE C = ConsumerAcceptor.getInstance()
  • 33. THANK YOU Full documented code available at: http://github.com/karajrish/pubsubmq