Class: Dakwak::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/dakwak/messaging/message.rb

Overview

Messages are the AMQP entities exchanged by communicators. All messages are sent through queues bound to certain channels. To exchange messages, see Dakwak::Communicator.

Messages contain a payload (which can be empty) and might contain the following properties:

  • meta.content_type

  • meta.content_encoding

  • meta.priority

  • meta.type

  • meta.delivery_mode

  • meta.app_id

  • meta.message_id

  • meta.user_id

  • meta.correlation_id

  • meta.headers

For a full reference, consult with the amqp-client Ruby gem documentation here: rdoc.info/github/ruby-amqp/amqp/master/frames

It is also helpful to review the supported message properties by libdakwak at: github.com/dakwak/libdakwak/blob/master/include/dakwak/messaging/message.hpp

Warning: When a property was not set by the sender, it evaluates to nil and NOT to an empty string or so.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, meta = {}, queue = "") ⇒ Message

Creates a new message with the given payload and meta attributes.



33
34
35
36
# File 'lib/dakwak/messaging/message.rb', line 33

def initialize(payload, meta = {}, queue = "")
  @payload, @meta, @queue = payload, meta, queue
  @meta[:headers] ||= {} if @meta.class == Hash
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



30
31
32
# File 'lib/dakwak/messaging/message.rb', line 30

def meta
  @meta
end

#payloadObject (readonly)

Returns the value of attribute payload.



30
31
32
# File 'lib/dakwak/messaging/message.rb', line 30

def payload
  @payload
end

#queueObject (readonly)

Returns the value of attribute queue.



30
31
32
# File 'lib/dakwak/messaging/message.rb', line 30

def queue
  @queue
end

Instance Method Details

#dumpObject



60
61
62
63
64
# File 'lib/dakwak/messaging/message.rb', line 60

def dump
  str = ""
  @meta.attributes.each_pair { |k,v| str << "#{k} => #{v}\n" }
  str
end

#expects_reply?Boolean

If the message_id property was set by the sender, then a reply is expected.

The address to send back to will be specified in this message’s meta.reply_to, and the meta.message_id should be used as the reply’s meta.correlation_id so the recepient can route the reply.

Returns:

  • (Boolean)


43
44
45
# File 'lib/dakwak/messaging/message.rb', line 43

def expects_reply?()
  !@meta.message_id.nil? && !@meta.headers["queue_name"].nil?
end

#prepare_reply(payload = "") ⇒ Object

Builds a message with the proper headers and attributes to mark it as a reply to the current one.

You can use the returned message, set its payload if needed, to publish the reply.



51
52
53
54
# File 'lib/dakwak/messaging/message.rb', line 51

def prepare_reply(payload = "")
  Message.new(payload, { :reply_to => @meta.app_id, :correlation_id => @meta.message_id },
    @meta.headers["queue_name"])
end

#to_sObject



56
57
58
# File 'lib/dakwak/messaging/message.rb', line 56

def to_s
  "Sender: '#{@meta.app_id}@#{(@meta.headers || {})["queue_name"]}' -- #{@payload[0..30]}"
end