Class: Kanina::Message
- Inherits:
-
Object
- Object
- Kanina::Message
- Defined in:
- lib/kanina/message.rb
Overview
Kanina::Message
allows you to send messages to RabbitMQ, and have
your exchanges and bindings set up on the fly.
# app/messages/user_message.rb
class UserMessage < Kanina::Message
exchange "user.exchange"
end
= UserMessage.new("data")
.deliver
Messages are encapsulated in JSON format. If you use
Kanina::Subscription
to receive messages, they're automatically
parsed back out of JSON into native Ruby objects.
Instance Attribute Summary collapse
-
#data ⇒ Object
The actual contents of the message to be sent.
-
#routing_key ⇒ String
Routing_key.
Class Method Summary collapse
-
.channel ⇒ Bunny::Channel
Helper method to return the channel that Kanina::Server is talking on.
- .exchange(name = nil, type: :direct, durable: false) ⇒ Object
-
.persistent ⇒ Object
Sets outgoing messages to be persistent.
-
.routing_key(name = nil, durable: false) ⇒ Object
Set the queue to which messages at this exchange should be sent to.
-
.transient ⇒ Object
Sets outgoing messages to be transient (not persistent).
-
.verify_queue(routing_key, durable: false) ⇒ Bunny::Queue
Creates the queue, or grabs the queue that already exists.
Instance Method Summary collapse
-
#deliver ⇒ Object
Deliver the message to the specified exchange and/or queue.
-
#exchange ⇒ String
The exchange this message is going into.
-
#initialize(data = nil, routing_key: nil) ⇒ Message
constructor
Create a new message to be sent.
-
#json ⇒ String
The json-formatted data being sent.
-
#persistent ⇒ Boolean
Whether this message is persistent or not Currently only set at the class level.
Constructor Details
#initialize(data = nil, routing_key: nil) ⇒ Message
Create a new message to be sent.
100 101 102 103 104 |
# File 'lib/kanina/message.rb', line 100 def initialize(data = nil, routing_key: nil) @data = data || {} @routing_key = routing_key @persistent = persistent end |
Instance Attribute Details
#data ⇒ Object
The actual contents of the message to be sent.
90 91 92 |
# File 'lib/kanina/message.rb', line 90 def data @data end |
#routing_key ⇒ String
Returns routing_key.
113 114 115 |
# File 'lib/kanina/message.rb', line 113 def routing_key @routing_key || self.class.routing_key end |
Class Method Details
.channel ⇒ Bunny::Channel
Helper method to return the channel that Kanina::Server is talking on.
20 21 22 |
# File 'lib/kanina/message.rb', line 20 def channel Kanina::Server.channel or fail 'Kanina::Server.channel is not open' end |
.exchange ⇒ String .exchange(name, *opts) ⇒ String
30 31 32 33 34 35 36 37 |
# File 'lib/kanina/message.rb', line 30 def exchange(name = nil, type: :direct, durable: false) if name.present? @type = type @exchange = channel.exchange(name, type: type, durable: durable) else @exchange || channel.default_exchange end end |
.persistent ⇒ Object
Sets outgoing messages to be persistent. Note that messages must be
sent to a durable queue, or they will still be lost! By default, this is
true
.
66 67 68 |
# File 'lib/kanina/message.rb', line 66 def persistent @persistent.nil? ? @persistent = true : @persistent end |
.routing_key ⇒ String .routing_key(name) ⇒ String
Set the queue to which messages at this exchange should be sent to. Note that you'll probably want to use this at the exchange level, OR a binding at the queue level.
54 55 56 57 58 59 60 61 |
# File 'lib/kanina/message.rb', line 54 def routing_key(name = nil, durable: false) if name.present? verify_queue(name, durable: durable) @routing_key = name else @routing_key end end |
.transient ⇒ Object
Sets outgoing messages to be transient (not persistent). If the receiving queues are taken down, messages could be lost. This might be the desired functionality, if messages need to be timely and queues being destroyed aren't a big deal.
73 74 75 |
# File 'lib/kanina/message.rb', line 73 def transient @persistent = false end |
.verify_queue(routing_key, durable: false) ⇒ Bunny::Queue
Creates the queue, or grabs the queue that already exists. Note that if the queue already exists, it must exist with the same parameters (like durability), or the Bunny gem is very unhappy.
83 84 85 |
# File 'lib/kanina/message.rb', line 83 def verify_queue(routing_key, durable: false) Kanina::Server.channel.queue(routing_key, durable: durable) end |
Instance Method Details
#deliver ⇒ Object
Deliver the message to the specified exchange and/or queue.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/kanina/message.rb', line 129 def deliver if exchange.name == '' if routing_key.present? exchange.publish(json, routing_key: routing_key, persistent: @persistent) else fail 'Routing key must be set when using default exchange.' end else exchange.publish(json, routing_key: routing_key, persistent: @persistent) end end |
#exchange ⇒ String
The exchange this message is going into
108 109 110 |
# File 'lib/kanina/message.rb', line 108 def exchange self.class.exchange end |
#json ⇒ String
Returns The json-formatted data being sent.
124 125 126 |
# File 'lib/kanina/message.rb', line 124 def json data.present? ? data.to_json : {}.to_json end |
#persistent ⇒ Boolean
Returns whether this message is persistent or not Currently only set at the class level.
119 120 121 |
# File 'lib/kanina/message.rb', line 119 def persistent @persistent.nil? ? self.class.persistent : @persistent end |