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.

Parameters:

  • data (Hash) (defaults to: nil)

    The payload of the message

  • routing_key (String) (defaults to: nil)

    Optional, to change the queue the message is sent to.



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

#dataObject

The actual contents of the message to be sent.



90
91
92
# File 'lib/kanina/message.rb', line 90

def data
  @data
end

#routing_keyString

Returns routing_key.

Returns:

  • (String)

    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

.channelBunny::Channel

Helper method to return the channel that Kanina::Server is talking on.

Returns:

  • (Bunny::Channel)


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

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

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

Overloads:

  • .exchangeString

    Returns the set exchange, or the default exchange.

    Returns:

    • (String)

      the set exchange, or the default exchange.

  • .exchange(name, *opts) ⇒ String

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

    Parameters:

    • name (String)

      the name of the exchange

    Returns:

    • (String)

      the exchange



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

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

    Returns the routing key.

    Returns:

    • (String)

      the routing key

  • .routing_key(name) ⇒ String

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

    Parameters:

    • name (String)

      the routing key

    Returns:

    • (String)

      the routing key



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

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

Parameters:

  • routing_key (String)

    the routing key

  • durable (Boolean) (defaults to: false)

    whether the queue should be durable or not

Returns:

  • (Bunny::Queue)

    the queue



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

#deliverObject

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

#exchangeString

The exchange this message is going into

Returns:

  • (String)

    exchange



108
109
110
# File 'lib/kanina/message.rb', line 108

def exchange
  self.class.exchange
end

#jsonString

Returns The json-formatted data being sent.

Returns:

  • (String)

    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

#persistentBoolean

Returns whether this message is persistent or not Currently only set at the class level.

Returns:

  • (Boolean)

    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