Class: Kanina::Message

Inherits:
Object
  • Object
show all
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

message = UserMessage.new("data")
message.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, routing_key: nil) ⇒ Message

Create a new message to be sent.


97
98
99
100
101
# File 'lib/kanina/message.rb', line 97

def initialize(data = nil, routing_key: nil)
  @data = data || {}
  @routing_key = routing_key
  @persistent = persistent
end

Instance Attribute Details

#dataObject

The actual contents of the message to be sent.


87
88
89
# File 'lib/kanina/message.rb', line 87

def data
  @data
end

#routing_keyString


110
111
112
# File 'lib/kanina/message.rb', line 110

def routing_key
  @routing_key || self.class.routing_key
end

Class Method Details

.channelObject

Helper method to return the channel opened by Kanina::Server.


19
20
21
# File 'lib/kanina/message.rb', line 19

def channel
  Kanina::Server.channel or fail 'Kanina::Server.channel is not open'
end

.exchangeString .exchange(name, *opts) ⇒ String

Overloads:

  • .exchange(name, *opts) ⇒ String

    Set the name and type of the exchange messages should be sent to.


30
31
32
33
34
35
36
37
# File 'lib/kanina/message.rb', line 30

def exchange(name = nil, type: :direct)
  if name.present?
    @type = type
    @exchange = channel.exchange(name, type: type)
  else
    @exchange || channel.default_exchange
  end
end

.persistentObject

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_keyString .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.

Overloads:

  • .routing_key(name) ⇒ String

    Verifies that the queue exists or is created, and sets the routing key to it.


54
55
56
57
58
59
60
61
# File 'lib/kanina/message.rb', line 54

def routing_key(name = nil)
  if name.present?
    verify_queue(name)
    @routing_key = name
  else
    @routing_key
  end
end

.transientObject

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) ⇒ Object

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.


80
81
82
# File 'lib/kanina/message.rb', line 80

def verify_queue(routing_key)
  Kanina::Server.channel.queue(routing_key)
end

Instance Method Details

#deliverObject

Deliver the message to the specified exchange and/or queue.


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/kanina/message.rb', line 126

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)
  end
end

#exchangeString

The exchange this message is going into


105
106
107
# File 'lib/kanina/message.rb', line 105

def exchange
  self.class.exchange
end

#jsonString


121
122
123
# File 'lib/kanina/message.rb', line 121

def json
  data.present? ? data.to_json : {}.to_json
end

#persistentBoolean


116
117
118
# File 'lib/kanina/message.rb', line 116

def persistent
  @persistent.nil? ? self.class.persistent : @persistent
end