Last active
February 24, 2023 14:27
-
-
Save noxdafox/ad1fb4c3769e06a888c3a542fc08c544 to your computer and use it in GitHub Desktop.
Usage example of RabbitMQ exchange deduplication plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pika | |
from hashlib import md5 | |
RABBITMQ_URL = 'amqp://guest:guest@localhost:5672/' | |
parameters = pika.URLParameters(RABBITMQ_URL) | |
connection = pika.BlockingConnection(parameters) | |
channel = connection.channel() | |
# Declare exchange, queue and bind them together | |
channel.exchange_declare( | |
exchange='test_exchange', | |
exchange_type='x-message-deduplication', | |
arguments=dict({'x-cache-size': '5'})) | |
channel.queue_declare(queue='test_queue') | |
channel.queue_bind('test_queue', 'test_exchange') | |
# The message will be deduplicated based on its content | |
# The MD5 digest of the content is used as deduplication header | |
message_body = 'hello world' | |
message_deduplication_header = md5(message_body.encode()).hexdigest() | |
# Only one message shall be routed to the queue | |
for _ in range(100): | |
channel.basic_publish( | |
exchange='test_exchange', | |
routing_key='', | |
body=message_body, | |
properties=pika.BasicProperties( | |
headers={'x-deduplication-header': message_deduplication_header})) | |
channel.close() | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there any example using queue level deduplication in kumbo?