After that the message hits the exchange, which in turn places it in the “rabbit” queue. Producer sends the message to a nameless_ exchange (more on exchanges comes later) with a routing key specifying the queue created beforehand. If it weren’t for this step, a queue could be non-existent, and therefore messages could get discarded immediately. Producer declares a queue, to make sure it exists when the message will be produced. GOT: 'Droplet explain what happens in this code, let’s go step by step:īoth consumer and producer are created and connected to the same RabbitMQ server, residing on localhost The output should look like: python rabbit_test.py Running the script should print the message that was sent by the script to the RabbitMQ queue, since the test program receives the message immediately afterwards. Received_message = consumer.wait(receive_promise) Receive_promise = consumer.basic_consume(queue='rabbit', no_ack=True) # start waiting for messages, also those sent before (!), on the queue named rabbit Send_promise = producer.basic_publish(exchange='', routing_key='rabbit', body='Droplet test!') Send_promise = producer.queue_declare(queue='rabbit') # declare queue (queue must exist before it is being used - otherwise messages sent to that queue will be discarded) Producer = puka.Client("amqp://localhost/")Ĭonsumer = puka.Client("amqp://localhost/") # declare send and receive clients, both connecting to the same server on local machine To test whether the message broker and puka works perfectly, and to get a grip on how the sending and receiving messages work in practice, create a sample python script named rabbit_test.py vim rabbit_test.pyĪnd paste the script contents: import puka Testing RabbitMQ and Puka with a Simple Example That means after each request (promise) puka will wait until it gets executed before going to the next step. While puka can work asynchronously, in our examples puka will be used as a synchronous library. It is a promise, which may be understood as a synchronous request to the AMQP server that guarantees execution (successful or not) of the request and on which the client waits until it is completed. There is one more, strictly related to puka python library, which was chosen as the library of choice for its clarity. How exactly depends on the exchange itself.Īll five terms will be used throughout this text. Queues bound to a certain exchange are served by the exchange. To use a real life metaphor, exchange is like a mailman: It handles messages so they get delivered to proper queues (mailboxes), from which consumers can gather them.īinding is a connection between queues and exchanges. It sends messages to an exchange, which - in turn - places the message to one or more queues, depending on the exchange used. The producer never sends a message directly to a queue. When a message hits a non-existent queue, it gets discarded.Įxchange is an entity that resides between producers and queues. When a message hits the existing queue, it waits there until consumed by a consumer accessing that particular queue. There is also no limitation as to how many producers can send a message to a queue, nor how many consumers can try to access it. There is no limitation to how many messages a single queue can hold. Queue is a buffer in which sent messages are stored and ready to be received. Producer is a party that sends messages, hence creating a message is producing.Ĭonsumer is a party that receives messages, hence receiving a message is consuming. Messaging introduces a few terms that describe basic principles of the message broker and its mechanics. Introduction to RabbitMQ and Its Terminology On RHEL based, like CentOS: yum install python-setuptools On Debian based distributions (including Ubuntu) it can be easily installed using: apt-get install python-pip Pip is not always bundled with Linux distributions. Puka can be quickly installed using pip – a Python package manager. Python has been chosen as a clean and easy to understand language for the sake of straightforward presentation, but since AMQP is a widely adopted protocol, any other programming language can be freely used to achieve similar goals. Puka Python LibraryĪll examples in this article are presented using Python language backed up with puka library handling the AMQP messaging protocol. How To Install and Manage RabbitMQ explains in detail how to get RabbitMQ working and is a good starting point for using this message broker. Working with RabbitMQ to send and receive messages is possible only after installing and configuring the software itself.
0 Comments
Leave a Reply. |