doc.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2012, Sean Treadway, SoundCloud Ltd.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Source code and contact info at http://github.com/streadway/amqp
  5. /*
  6. AMQP 0.9.1 client with RabbitMQ extensions
  7. Understand the AMQP 0.9.1 messaging model by reviewing these links first. Much
  8. of the terminology in this library directly relates to AMQP concepts.
  9. Resources
  10. http://www.rabbitmq.com/tutorials/amqp-concepts.html
  11. http://www.rabbitmq.com/getstarted.html
  12. http://www.rabbitmq.com/amqp-0-9-1-reference.html
  13. Design
  14. Most other broker clients publish to queues, but in AMQP, clients publish
  15. Exchanges instead. AMQP is programmable, meaning that both the producers and
  16. consumers agree on the configuration of the broker, instead requiring an
  17. operator or system configuration that declares the logical topology in the
  18. broker. The routing between producers and consumer queues is via Bindings.
  19. These bindings form the logical topology of the broker.
  20. In this library, a message sent from publisher is called a "Publishing" and a
  21. message received to a consumer is called a "Delivery". The fields of
  22. Publishings and Deliveries are close but not exact mappings to the underlying
  23. wire format to maintain stronger types. Many other libraries will combine
  24. message properties with message headers. In this library, the message well
  25. known properties are strongly typed fields on the Publishings and Deliveries,
  26. whereas the user defined headers are in the Headers field.
  27. The method naming closely matches the protocol's method name with positional
  28. parameters mapping to named protocol message fields. The motivation here is to
  29. present a comprehensive view over all possible interactions with the server.
  30. Generally, methods that map to protocol methods of the "basic" class will be
  31. elided in this interface, and "select" methods of various channel mode selectors
  32. will be elided for example Channel.Confirm and Channel.Tx.
  33. The library is intentionally designed to be synchronous, where responses for
  34. each protocol message are required to be received in an RPC manner. Some
  35. methods have a noWait parameter like Channel.QueueDeclare, and some methods are
  36. asynchronous like Channel.Publish. The error values should still be checked for
  37. these methods as they will indicate IO failures like when the underlying
  38. connection closes.
  39. Asynchronous Events
  40. Clients of this library may be interested in receiving some of the protocol
  41. messages other than Deliveries like basic.ack methods while a channel is in
  42. confirm mode.
  43. The Notify* methods with Connection and Channel receivers model the pattern of
  44. asynchronous events like closes due to exceptions, or messages that are sent out
  45. of band from an RPC call like basic.ack or basic.flow.
  46. Any asynchronous events, including Deliveries and Publishings must always have
  47. a receiver until the corresponding chans are closed. Without asynchronous
  48. receivers, the sychronous methods will block.
  49. Use Case
  50. It's important as a client to an AMQP topology to ensure the state of the
  51. broker matches your expectations. For both publish and consume use cases,
  52. make sure you declare the queues, exchanges and bindings you expect to exist
  53. prior to calling Channel.Publish or Channel.Consume.
  54. // Connections start with amqp.Dial() typically from a command line argument
  55. // or environment variable.
  56. connection, err := amqp.Dial(os.Getenv("AMQP_URL"))
  57. // To cleanly shutdown by flushing kernel buffers, make sure to close and
  58. // wait for the response.
  59. defer connection.Close()
  60. // Most operations happen on a channel. If any error is returned on a
  61. // channel, the channel will no longer be valid, throw it away and try with
  62. // a different channel. If you use many channels, it's useful for the
  63. // server to
  64. channel, err := connection.Channel()
  65. // Declare your topology here, if it doesn't exist, it will be created, if
  66. // it existed already and is not what you expect, then that's considered an
  67. // error.
  68. // Use your connection on this topology with either Publish or Consume, or
  69. // inspect your queues with QueueInspect. It's unwise to mix Publish and
  70. // Consume to let TCP do its job well.
  71. SSL/TLS - Secure connections
  72. When Dial encounters an amqps:// scheme, it will use the zero value of a
  73. tls.Config. This will only perform server certificate and host verification.
  74. Use DialTLS when you wish to provide a client certificate (recommended),
  75. include a private certificate authority's certificate in the cert chain for
  76. server validity, or run insecure by not verifying the server certificate dial
  77. your own connection. DialTLS will use the provided tls.Config when it
  78. encounters an amqps:// scheme and will dial a plain connection when it
  79. encounters an amqp:// scheme.
  80. SSL/TLS in RabbitMQ is documented here: http://www.rabbitmq.com/ssl.html
  81. */
  82. package amqp