Method: Bunny::Exchange#publish

Defined in:
lib/bunny/exchange08.rb

#publish(data, opts = {}) ⇒ Object

=== DESCRIPTION:

Publishes a message to a specific exchange. The message will be routed to queues as defined by the exchange configuration and distributed to any active consumers when the transaction, if any, is committed.

==== OPTIONS:

  • :key => ‘routing_key’ – Specifies the routing key for the message. The routing key is used for routing messages depending on the exchange configuration.
  • :content_type => ‘content/type’ – Specifies the content type to use for the message.
  • :mandatory => true or false (default) – Tells the server how to react if the message cannot be routed to a queue. If set to true, the server will return an unroutable message with a Return method. If this flag is zero, the server silently drops the message.
  • :immediate => true or false (default) – Tells the server how to react if the message cannot be routed to a queue consumer immediately. If set to true, the server will return an undeliverable message with a Return method. If set to false, the server will queue the message, but with no guarantee that it will ever be consumed.
  • :persistent => true or false (default) – Tells the server whether to persist the message If set to true, the message will be persisted to disk and not lost if the server restarts. If set to false, the message will not be persisted across server restart. Setting to true incurs a performance penalty as there is an extra cost associated with disk access.

==== RETURNS:

nil



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bunny/exchange08.rb', line 140

def publish(data, opts = {})
  opts = opts.dup
  out = []

  # Set up options
  routing_key = opts.delete(:key) || key
  mandatory = opts.delete(:mandatory)
  immediate = opts.delete(:immediate)
  delivery_mode = opts.delete(:persistent) ? 2 : 1
  content_type = opts.delete(:content_type) || 'application/octet-stream'

  out << Qrack::Protocol::Basic::Publish.new({ :exchange => name,
                                               :routing_key => routing_key,
                                               :mandatory => mandatory,
                                               :immediate => immediate })
  data = data.to_s
  out << Qrack::Protocol::Header.new(
    Qrack::Protocol::Basic,
    data.bytesize, {
      :content_type  => content_type,
      :delivery_mode => delivery_mode,
      :priority      => 0
    }.merge(opts)
  )
  out << Qrack::Transport::Body.new(data)

  client.send_frame(*out)
end