routing.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ===============
  2. Routing Tasks
  3. ===============
  4. **NOTE** This document refers to functionality only available in brokers
  5. using AMQP. Other brokers may implement some functionality, see their
  6. respective documenation for more information, or contact the `mailinglist`_.
  7. .. _`mailinglist`: http://groups.google.com/group/celery-users
  8. AMQP Primer
  9. ===========
  10. Exchanges, queues and routing keys.
  11. -----------------------------------
  12. TODO Mindblowing one-line simple explanation here. TODO
  13. The steps required to send and receive messages are:
  14. 1. Create an exchange
  15. 2. Create a queue
  16. 3. Bind the queue to the exchange.
  17. Exchange type
  18. -------------
  19. The exchange type defines how the messages are routed through the exchange.
  20. The exchanges defined in the standard is ``direct``, ``topic``, ``fanout`` and
  21. ``headers``. Also non-standard exchange types available as plugins to RabbitMQ, like
  22. the last value cache plug-in.
  23. Consumers and Producers
  24. -----------------------
  25. TODO
  26. Related API commands
  27. -------------------------
  28. * exchange.declare(exchange_name, type, passive, durable, auto_delete, internal)
  29. Declares an exchange by name.
  30. * ``passive`` means the exchange won't be created, but you can use this to
  31. check if the exchange already exists.
  32. * Durable exchanges are persistent. I.e. they survive a broker restart.
  33. * ``auto_delete`` means the queue will be deleted by the broker when there
  34. are no more queues using it.
  35. * queue.declare(queue_name, passive, durable, exclusive, auto_delete)
  36. Declares a queue by name.
  37. * exclusive queues can only be consumed from by the current connection.
  38. implies ``auto_delete``.
  39. * queue.bind(queue_name, exchange_name, routing_key)
  40. Binds a queue to an exchange with a routing key.
  41. Unbound queues will not receive messages, so this is necessary.
  42. * queue.delete(name, if_unused, if_empty)
  43. Deletes a queue and its binding.
  44. * exchange.delete(name, if_unused)
  45. Deletes an exchange.
  46. **NOTE**: Declaring does not necessarily mean "create". When you declare you
  47. *assert* that the entity exists and it is operable. There is no rule as to
  48. whom should initially create the exchange/queue/binding, whether consumer
  49. or producer. Usually the first one to need it will create it.
  50. Hands-on with the API
  51. ---------------------
  52. Celery comes with a tool called ``camqadm`` (short for celery AMQP admin).
  53. It's used for simple admnistration tasks like deleting queues/exchanges,
  54. purging queues and creating queue entities. In short it's for simple command
  55. line access to the AMQP API.
  56. You can write commands directly in the arguments to ``camqadm``, or just start
  57. with no arguments which makes it start in shell-mode::
  58. $ camqadm
  59. -> connecting to amqp://guest@localhost:5672/.
  60. -> connected.
  61. -->
  62. Here, ``-->`` is the prompt. Type ``help`` for a list of commands, there's
  63. also autocomplete so you can start typing a command then hit ``tab`` to show a
  64. list of possible matches.
  65. Now let's create a queue we can send messages to::
  66. --> exchange.declare testexchange direct
  67. ok.
  68. --> queue.declare testqueue
  69. ok. queue:testqueue messages:0 consumers:0.
  70. --> queue.bind testqueue testexchange testkey
  71. ok.
  72. Messages are sent with a routing key, to an exchange. This is done using
  73. the ``basic.publish`` command::
  74. --> basic.publish "This is a message!" testexchange testkey
  75. ok.
  76. Now that the message is sent we can retrieve it again, we use the
  77. ``basic.get`` command here, which pops a single message off the queue,
  78. this command is not recommended for production as it implies polling, any
  79. real application would declare consumers instead::
  80. --> basic.get testqueue
  81. {'body': 'This is a message!',
  82. 'delivery_info': {'delivery_tag': 1,
  83. 'exchange': u'testexchange',
  84. 'message_count': 0,
  85. 'redelivered': False,
  86. 'routing_key': u'testkey'},
  87. 'properties': {}}
  88. AMQP uses acknowledgment to signify a message has been received and processed
  89. successfully. The message is sent to the next receiver if the client
  90. connection is closed, and it has not yet been acknowledged.
  91. Note the delivery tag listed in the structure above; Within a connection channel,
  92. every received message has a unique delivery tag,
  93. This tag is used to acknowledge the message. Note that
  94. delivery tags are not unique across connections, so in another client
  95. the delivery tag ``1`` might point to a different message than in our channel.
  96. You can acknowledge the message we received using ``basic.ack``::
  97. --> basic.ack 1
  98. ok.
  99. To clean up after ourselves we should delete the entities we just created::
  100. --> queue.delete testqueue
  101. ok. 0 messages deleted.
  102. --> exchange.delete testexchange
  103. ok.