MassTransitPython
Python library to exchange messages between MassTransit RabbitMQ Client and Python application.
Install / Use
/learn @wieczorekmichal1/MassTransitPythonREADME
Description
Python library to exchange messages between MassTransit RabbitMQ Client and Python application. Implemented with pika RabbitMQ client.
Installation
Run the following to install:
pip install masstransitpython-byQ96
Usage
Simple receiver/sender model was implemented to show basic package usage.
Configuration
Default client configuration can be implemented as follows:
from pika import PlainCredentials
from masstransitpython import RabbitMQConfiguration
RABBITMQ_USERNAME = 'guest'
RABBITMQ_PASSWORD = 'guest'
RABBITMQ_HOST = 'localhost'
RABBITMQ_PORT = 5672
RABBITMQ_VIRTUAL_HOST = '/'
credentials = PlainCredentials(RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
conf = RabbitMQConfiguration(credentials,
queue='PythonServiceQueue',
host=RABBITMQ_HOST,
port=RABBITMQ_PORT,
virtual_host=RABBITMQ_VIRTUAL_HOST)
Receiver
Receiver must have an appropriately defined exchange name: [SOLUTION_NAME:DIRECTORY_NAME:MESSAGE_NAME]
from masstransitpython import RabbitMQReceiver
from json import loads
def handler(ch, method, properties, body):
msg = loads(body.decode())
print("Received message: %s" % msg["message"])
# define receiver
receiver = RabbitMQReceiver(conf, 'MassTransitService.Messages:SampleMessage')
receiver.add_on_message_callback(handler)
receiver.start_consuming()
Sender
from masstransitpython import RabbitMQSender
from json import loads
def send_message(body):
'''
:param body: Message received from MassTransit client
:return: None
'''
with RabbitMQSender(conf) as sender:
sender.set_exchange('MassTransitService.Messages:SampleMessage')
encoded_msg = MessageEncoder().encode(SampleMessage("Hello World!"))
response = sender.create_masstransit_response(loads(encoded_msg), body)
sender.publish(message=response)
Message
from json import JSONEncoder
class SampleMessage:
def __init__(self, name):
self.name = name
class MessageEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
Other
Full example avaliable in https://github.com/byQ96/MassTransitPython/example
Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
