Skip to main content

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

Release files for masstransitpython-byQ96 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for masstransitpython-byQ96 0.0.5
File Interpreter ABI Platform
masstransitpython_byQ96-0.0.5-py3-none-any.whl Python 3 none any Details

Release files / masstransitpython_byQ96-0.0.5-py3-none-any.whl

Download URL masstransitpython_byQ96-0.0.5-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abe0614579b3ff008ee38eca82200fa3e736c4b8257c5a1954cdfca0595a9a95
BLAKE2b-256 checksum
How to use checksums
6a10453ebf61febc164939795cb13c4d9aaf2fec53327c3f9c8023244a28e8d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

Release history Release notifications | RSS feed

This release

0.0.5 This release

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page