rabbitmq使用方法(一)

  • Introduction

    RabbitMQ is a message broker. The principal idea is pretty simple: it accepts and forwards messages.

    RabbitMQ, and messaging in general, uses some jargon.

    • Producing means nothing more than sending. A program that sends messages is a producer. We'll draw it like that, with "P":

      rabbitmq使用方法(一)
    • A queue is the name for a mailbox. It lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can be stored only inside a queue. A queue is not bound by any limits, it can store as many messages as you like ‒ it's essentially an infinite buffer. Many producers can send messages that go to one queue, many consumers can try to receive data from one queue. A queue will be drawn as like that, with its name above it:

      rabbitmq使用方法(一)
    • Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages. On our drawings it's shown with "C":

      rabbitmq使用方法(一)

    Note that the producer, consumer, and broker do not have to reside on the same machine; indeed in most applications they don't.

    Hello World!

    (using the pika 0.9.8 Python client)

    Our "Hello world" won't be too complex ‒ let's send a message, receive it and print it on the screen. To do so we need two programs: one that sends a message and one that receives and prints it.

    Our overall design will look like:

    rabbitmq使用方法(一)

    Producer sends messages to the "hello" queue. The consumer receives messages from that queue.

  • Sending

    rabbitmq使用方法(一)

    Our first program send.py will send a single message to the queue. The first thing we need to do is to establish a connection with RabbitMQ server.

     #!/usr/bin/env python
    import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
    channel = connection.channel()

    We're connected now, to a broker on the local machine - hence the localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.

    Next, before sending we need to make sure the recipient queue exists. If we send a message to non-existing location, RabbitMQ will just trash the message. Let's create a queue to which the message will be delivered, let's name it hello:

     channel.queue_declare(queue='hello')

    At that point we're ready to send a message. Our first message will just contain a string Hello World! and we want to send it to our hello queue.

    In RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. But let's not get dragged down by the details ‒ you can read more aboutexchanges in the third part of this tutorial. All we need to know now is how to use a default exchange identified by an empty string. This exchange is special ‒ it allows us to specify exactly to which queue the message should go. The queue name needs to be specified in the routing_key parameter:

     channel.basic_publish(exchange='',
    routing_key='hello',
    body='Hello World!')
    print " [x] Sent 'Hello World!'"

    Before exiting the program we need to make sure the network buffers were flushed and our message was actually delivered to RabbitMQ. We can do it by gently closing the connection.

     connection.close()
    
    

    Receiving

    rabbitmq使用方法(一)

    Our second program receive.py will receive messages from the queue and print them on the screen.

    Again, first we need to connect to RabbitMQ server. The code responsible for connecting to Rabbit is the same as previously.

    The next step, just like before, is to make sure that the queue exists. Creating a queue using queue_declare is idempotent ‒ we can run the command as many times as we like, and only one will be created.

     channel.queue_declare(queue='hello')

    Receiving messages from the queue is more complex. It works by subscribing a callback function to a queue. Whenever we receive a message, this callback function is called by the Pika library. In our case this function will print on the screen the contents of the message.

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

    Next, we need to tell RabbitMQ that this particular callback function should receive messages from our hello queue:

     channel.basic_consume(callback,
    queue='hello',
    no_ack=True)

    For that command to succeed we must be sure that a queue which we want to subscribe to exists. Fortunately we're confident about that ‒ we've created a queue above ‒ usingqueue_declare.

    The no_ack parameter will be described later on.And finally, we enter a never-ending loop that waits for data and runs callbacks whenever necessary.

  •  print ' [*] Waiting for messages. To exit press CTRL+C'
    channel.start_consuming()

    Putting it all together

    Full code for send.py:

     #!/usr/bin/env python
    import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='',
    routing_key='hello',
    body='Hello World!')
    print " [x] Sent 'Hello World!'"
    connection.close()

    (send.py source)

    Full receive.py code:

     #!/usr/bin/env python
    import pika connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
    channel = connection.channel() channel.queue_declare(queue='hello') print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,) channel.basic_consume(callback,
    queue='hello',
    no_ack=True) channel.start_consuming()

    (receive.py source)

上一篇:Maven(五)使用Nexus搭建Maven私服


下一篇:用sparkR, 分析上亿条订单数据的脚本。