LinkedIn emplea cookies para mejorar la funcionalidad y el rendimiento de nuestro sitio web, así como para ofrecer publicidad relevante. Si continúas navegando por ese sitio web, aceptas el uso de cookies. Consulta nuestras Condiciones de uso y nuestra Política de privacidad para más información.
LinkedIn emplea cookies para mejorar la funcionalidad y el rendimiento de nuestro sitio web, así como para ofrecer publicidad relevante. Si continúas navegando por ese sitio web, aceptas el uso de cookies. Consulta nuestra Política de privacidad y nuestras Condiciones de uso para más información.
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.
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
● 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
Problem statement/Business Logic
Matching & Dispatching
Choice of ‘information
Complexity of subscriptions
Application Level Routing
Reliability & Sequencing
A state trackable message
queue that stores incoming
messages of the publisher
based on the current state of
No guarantee of message
Less control over ordering of
Current state of a subscriber
cannot be tracked hence
leading to redundant
To build a state trackable message
exchange system which replicates
a pub-sub system in a distributed
registered to Broker
Topics are created
along with their
listening to posts
messages related to
Broker routes the
messages relating to
● 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.
● 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.
Def __init__(self,Mediator mediator):
//dispatch Msg to the
● 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.
● 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
Thus, we create a RouterDecorator class to add new routing feature as per
the requirement of subscriber without modifying the class Router
//route messages to consumers
//initialise router object
Def route(consumers): //as used by broker
Def route_for_consumer(consumers): //used by consumer
// different implementation of route() for consumers
● 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
● SingleObject class provides a static
method to get its static instance to
our demo class will use SingleObject
class to get a SingleObjectobject.
● 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.
If cls.INSTANCE is not None:
//else initialise object
If cls.INSTANCE is None:
C = ConsumerAcceptor.getInstance()
Full documented code available at: