Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ def callback(ch, method, properties, body)

Just want to know the meaning of the parameters in worker.py file:

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)

What do ch, method, and properties mean?

like image 968
shashank Avatar asked Dec 10 '15 12:12

shashank


People also ask

What is callback function in RabbitMQ?

Callback queueA client sends a request message and a server replies with a response message. In order to receive a response the client needs to send a 'callback' queue address with the request.

How do I stop eating Pika?

You will have to cancel the consumer in your on_request method. You can also use this method to consume messages which allows an inactivity_timeout to be set, where you could then cancel your consumer.

What is ACK and NACK in RabbitMQ?

ack is used for positive acknowledgements. basic. nack is used for negative acknowledgements (note: this is a RabbitMQ extension to AMQP 0-9-1) basic.

What is Pika in RabbitMQ?

Pika is a Python implementation of the AMQP 0-9-1 protocol for RabbitMQ. This tutorial guides you through installing Pika, declaring a queue, setting up a publisher to send messages to the broker's default exchange, and setting up a consumer to recieve messages from the queue.


1 Answers

ch

"ch" is the "channel" over which the communication is happening.

Think of a RabbitMQ connection in two parts:

  • the TCP/IP connection
  • channels within the connection

the actual TCP/IP connection is expensive to create, so you only want one connection per process instance.

A channel is where the work is done with RabbitMQ. a channel exists within an connection, and you need to have the channel reference so you can ack/nack messages, etc.

method

i think "method" is meta information regarding the message delivery

when you want to acknowledge the message - tell RabbitMQ that you are done processing it - you need both the channel and the delivery tag. the delivery tag comes from the method parameter.

i'm not sure why this is called "method" - perhaps it is related to the AMQP spec, where the "method" is meta-data about which AMQP method was executed?

properties

the "properties" of the message are user-defined properties on the message. you can set any arbitrary key / value pair that you want in these properties, and possibly get things like routing key used (though this may come from "method")

properties are often uses for bits of data that your code needs to have, but aren't part of the actual message body.

for example, if you had a re-sequencer process to make sure messages are processed in order, the "properties" would probably contain the message's sequence number.

like image 156
Derick Bailey Avatar answered Oct 22 '22 20:10

Derick Bailey