routing.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 exchange types defined in the standard are ``direct``, ``topic``,
  21. ``fanout`` and ``headers``. Also non-standard exchange types are available
  22. as plugins to RabbitMQ, like the ``last-value-cache plug-in`` by Michael
  23. Bridgen.
  24. .. _`last-value-cache plug-in``:
  25. http://github.com/squaremo/rabbitmq-lvc-plugin
  26. Consumers and Producers
  27. -----------------------
  28. TODO
  29. Related API commands
  30. --------------------
  31. exchange.declare(exchange_name, type, passive, durable, auto_delete, internal)
  32. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. Declares an exchange by name.
  34. * ``passive`` means the exchange won't be created, but you can use this to
  35. check if the exchange already exists.
  36. * Durable exchanges are persistent. That is - they survive a broker restart.
  37. * ``auto_delete`` means the queue will be deleted by the broker when there
  38. are no more queues using it.
  39. * queue.declare(queue_name, passive, durable, exclusive, auto_delete)
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. Declares a queue by name.
  42. * exclusive queues can only be consumed from by the current connection.
  43. implies ``auto_delete``.
  44. queue.bind(queue_name, exchange_name, routing_key)
  45. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  46. Binds a queue to an exchange with a routing key.
  47. Unbound queues will not receive messages, so this is necessary.
  48. queue.delete(name, if_unused, if_empty)
  49. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. Deletes a queue and its binding.
  51. exchange.delete(name, if_unused)
  52. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  53. Deletes an exchange.
  54. **NOTE**: Declaring does not necessarily mean "create". When you declare you
  55. *assert* that the entity exists and that it's operable. There is no rule as to
  56. whom should initially create the exchange/queue/binding, whether consumer
  57. or producer. Usually the first one to need it will be the one to create it.
  58. Hands-on with the API
  59. ---------------------
  60. Celery comes with a tool called ``camqadm`` (short for celery AMQP admin).
  61. It's used for simple admnistration tasks like creating/deleting queues and
  62. exchanges, purging queues and sending messages. In short it's for simple
  63. command-line access to the AMQP API.
  64. You can write commands directly in the arguments to ``camqadm``, or just start
  65. with no arguments to start it in shell-mode::
  66. $ camqadm
  67. -> connecting to amqp://guest@localhost:5672/.
  68. -> connected.
  69. -->
  70. Here ``-->`` is the prompt. Type ``help`` for a list of commands, there's
  71. also autocomplete so you can start typing a command then hit ``tab`` to show a
  72. list of possible matches.
  73. Now let's create a queue we can send messages to::
  74. --> exchange.declare testexchange direct
  75. ok.
  76. --> queue.declare testqueue
  77. ok. queue:testqueue messages:0 consumers:0.
  78. --> queue.bind testqueue testexchange testkey
  79. ok.
  80. This created the direct exchange ``testexchange``, and a queue
  81. named ``testqueue``. The queue is bound to the exchange using
  82. the routing key ``testkey``.
  83. From now on all messages sent to the exchange ``testexchange`` with routing
  84. key ``testkey`` will be moved to this queue. We can send a message by
  85. using the ``basic.publish`` command::
  86. --> basic.publish "This is a message!" testexchange testkey
  87. ok.
  88. Now that the message is sent we can retrieve it again. We use the
  89. ``basic.get`` command here, which pops a single message off the queue,
  90. this command is not recommended for production as it implies polling, any
  91. real application would declare consumers instead.
  92. Pop a message off the queue::
  93. --> basic.get testqueue
  94. {'body': 'This is a message!',
  95. 'delivery_info': {'delivery_tag': 1,
  96. 'exchange': u'testexchange',
  97. 'message_count': 0,
  98. 'redelivered': False,
  99. 'routing_key': u'testkey'},
  100. 'properties': {}}
  101. AMQP uses acknowledgment to signify that a message has been received
  102. and processed successfully. The message is sent to the next receiver
  103. if it has not been acknowledged before the client connection is closed.
  104. Note the delivery tag listed in the structure above; Within a connection channel,
  105. every received message has a unique delivery tag,
  106. This tag is used to acknowledge the message. Note that
  107. delivery tags are not unique across connections, so in another client
  108. the delivery tag ``1`` might point to a different message than in this channel.
  109. You can acknowledge the message we received using ``basic.ack``::
  110. --> basic.ack 1
  111. ok.
  112. To clean up after our test session we should delete the entities we created::
  113. --> queue.delete testqueue
  114. ok. 0 messages deleted.
  115. --> exchange.delete testexchange
  116. ok.