Generic Message Listeners and Publishers.
This module provides interfaces to work with any kind of message publisher and consumer.
This module also provides currently the following implementations:
- RabbitMQ Message Listener
- RabbbitMQ Message Publisher
Currently only rabbitmq support is implemented but in a future other infrastructure pieces could be supported too.
The infrastructure of this package rely on abstractions so it is highly decoupled. Apps code should depend only in MessageHandler, MessagePublisher and MessageListener.
pip install awesome_messages
from awesome_messages.domain.handler import MessageHandler class PrintRequestHandler(MessageHandler): def on_msg(self, message: dict): print(message)I'm using here the RabbitMQ listener implementation.
You can write your custom listener using MessageListener interface. if you write your own listener please make a pull request so that the rest of the community can use it too.
# Import infrastructure specific listener from awesome_messages.infra.rabbitmq.listener import RabbitMessageListener connection_string = "amqps://user:password@host/.." queue = "my_awesome_queue" # Declare our listener with our previously defined MessageHandler msg_listener = RabbitMessageListener( connection_string, queue, PrintRequestHandler() ) # Currently RabbitMQ listener is blocking try: msg_listener.start_listening() finally: msg_listener.stop_listening()I'm using here the RabbitMQ publisher implementation.
You can write your custom publisher using MessagePublisher interface. if you write your own publisher please make a pull request so that the rest of the community can use it too.
# Import infrastructure specific publisher from awesome_messages.infra.rabbitmq.publisher import RabbitMessagePublisher msg_publisher = RabbitMessagePublisher(connection_string, queue) try: # Publish a message msg_publisher.publish({"my msg": "my msg data"}) finally: msg_publisher.stop_publishing()The way this module is structured allows anyone to easily replace one implementation by another without changing the rest of the application code.
This module invites the developer to use dependency injection to build their app. Relying on defined interfaces rather than implementations allows changes in dependencies be possible without changing the code.
You can use a dependency injection library for configure your message handler, listener and publisher like the following one.
If this module has been useful to you, it would mean a lot to me if you could support my work. This way you help to keep this and other projects alive.
- Samuel López Saura - Linkedin Contact
