SlideShare uma empresa Scribd logo
1 de 33
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

Mais conteúdo relacionado

Semelhante a Design Patterns - Distributed Publisher-Subscriber Network

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 communicationMNM 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 MiddlewarePeter 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 ServiceAMIT YADAV
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1assinha
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Serviceskumar 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
 
Introduction to Microservices Patterns
Introduction to Microservices PatternsIntroduction to Microservices Patterns
Introduction to Microservices PatternsDimosthenis Botsaris
 

Semelhante a 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
 
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...
 
Introduction to Microservices Patterns
Introduction to Microservices PatternsIntroduction to Microservices Patterns
Introduction to Microservices Patterns
 

Último

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Último (20)

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

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